Skip to content

Specialize FASTCALL for functions with positional-only parameters #73650

Closed

Description

@serhiy-storchaka
BPO 29464
Nosy @rhettinger, @scoder, @vstinner, @skrah, @serhiy-storchaka, @timgraham
PRs
  • bpo-29464: Rename METH_FASTCALL to METH_FASTCALL|METH_KEYWORDS and make #1955
  • bpo-29464: Remove _PyArg_NoStackKeywords(). #2641
  • Files
  • fastcall-no-keywords.patch
  • fastcall-no-keywords-2.patch
  • fastcall-no-keywords-3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2017-07-10.12:51:42.598>
    created_at = <Date 2017-02-06.15:22:45.800>
    labels = ['interpreter-core', '3.7', 'performance']
    title = 'Specialize FASTCALL for functions with positional-only parameters'
    updated_at = <Date 2017-07-13.06:47:00.521>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-07-13.06:47:00.521>
    actor = 'vstinner'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2017-07-10.12:51:42.598>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-02-06.15:22:45.800>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['46544', '46558', '46699']
    hgrepos = []
    issue_num = 29464
    keywords = ['patch']
    message_count = 38.0
    messages = ['287143', '287146', '287150', '287151', '287152', '287155', '287156', '287176', '287199', '287231', '287238', '287253', '287259', '288992', '295169', '295170', '295174', '295176', '295177', '295178', '295785', '295789', '295791', '296819', '296901', '297136', '297606', '297748', '298030', '298031', '298061', '298227', '298230', '298232', '298258', '298264', '298265', '298268']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'scoder', 'vstinner', 'skrah', 'serhiy.storchaka', 'Tim.Graham']
    pr_nums = ['1955', '2641']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue29464'
    versions = ['Python 3.7']

    Activity

    1. serhiy-storchaka commented on Feb 6, 2017

      @serhiy-storchaka
      MemberAuthor

      Proposed patch renames METH_FASTCALL to METH_FASTCALL|METH_KEYWORDS and makes bare METH_FASTCALL be used for functions with positional-only parameters. This eliminates small cost that these functions pay for handling empty keywords: calling _PyStack_UnpackDict() and _PyArg_NoStackKeywords(), passing kwnames. This also can slightly reduce stack consumption.

    2. added
      3.7 (EOL)
      interpreter-core
      performance
      on Feb 6, 2017
    3. vstinner commented on Feb 6, 2017

      @vstinner
      Member

      Proposed patch renames METH_FASTCALL to METH_FASTCALL|METH_KEYWORDS and makes bare METH_FASTCALL be used for functions with positional-only parameters.

      While I tried to keep everything related to FASTCALL private, it seems like Cython uses some FASTCALL features. I don't know which ones exactly. Well, if only one project in the world uses FASTCALL, we can help them to support such backward incompatible change ;-)

      This eliminates small cost that these functions pay for handling empty keywords: calling _PyStack_UnpackDict() and _PyArg_NoStackKeywords(), passing kwnames.

      My idea when I designed FASTCALL was to move code to parse arguments in the function body rather than in _PyCFunction_FastCallKeywords(), and to have a single calling function METH_FASTCALL, rather than two (METH_FASTCALL and METH_FASTCALL|METH_KEYWORDS).

      The long term plan is also to support passing arguments by keyword in more functions. IMHO many functions don't accept keywords for technical reasons, but once we converted a module, function or type to Argument Clinic, it becomes trivial to accept keywords. If most functions accept keywords, I'm not sure that having a special case for positional-only is still worth it. But this plan was before I had discussions on supporting keywords in unicode methods. In fact, it's deliberate to not accept keywords in many functions or methods.

      Well, when I see your patch, I see that it removes a lot of code. So it's likely to be a good idea :-)

      This also can slightly reduce stack consumption.

      You mean the removal of the "PyObject *kwnames" for METH_FASTCALL (positional arguments only)? Do you have an idea of the stack usage? Try maybe testcapi_stacksize.patch of the issue bpo-28870? It would help to take a decision on this change.

    4. serhiy-storchaka commented on Feb 6, 2017

      @serhiy-storchaka
      MemberAuthor

      We can avoid breaking backward compatibility and introduce new call method METH_FASTCALL_NO_KEYWORDS. But combining existing flags looks better to me. FASTCALL is not a part of stable ABI.

      I still didn't do any benchmarking or stack usage measurements.

    5. vstinner commented on Feb 6, 2017

      @vstinner
      Member

      I still didn't do any benchmarking or stack usage measurements.

      I'm running benchmarks on the speed-python server.

    6. skrah commented on Feb 6, 2017

      skrahmannequin
      Mannequin

      Adding Stefan Behnel, perhaps Cython doesn't need backwards compatibility.

    7. vstinner commented on Feb 6, 2017

      @vstinner
      Member

      I measured the stack consumption, it's not better. But I created the issue bpo-29465 "Add _PyObject_FastCall() to reduce stack consumption" which would allow to reduce the stack consumption with this patch.

    8. vstinner commented on Feb 6, 2017

      @vstinner
      Member

      We can avoid breaking backward compatibility and introduce new call method METH_FASTCALL_NO_KEYWORDS. But combining existing flags looks better to me. FASTCALL is not a part of stable ABI.

      If we decide that having two FASTCALL calling convention, I prefer what you proposed: METH_FASTCALL (pos only) and METH_FASTCALL|METH_KEYWORDS (pos+kw). As you wrote, I like reusing the existing METH_KEYWORDS flag, it reduces the surprises if someone ports existing code using METH_VARARGS and METH_VARARGS|METH_KEYWORDS.

    9. vstinner commented on Feb 6, 2017

      @vstinner
      Member

      Hum, benchmark results don't seem good. There is probably a performance bug somewhere. I should investigate further to analyze these results. M aybe combined with the issue bpo-29465, results will be better.

      haypo@speed-python$ python3 -m perf compare_to ~/benchmarks/2017-02-06_07-15-default-e06af4027546.json fastcall-no-keywords_ref_e06af4027546.json -G --min-speed=5

      Slower (19):

      • unpickle_list: 6.36 us +- 0.11 us -> 8.08 us +- 0.14 us: 1.27x slower (+27%)
      • pickle_list: 7.53 us +- 0.41 us -> 8.62 us +- 0.49 us: 1.14x slower (+14%)
      • crypto_pyaes: 199 ms +- 2 ms -> 226 ms +- 2 ms: 1.13x slower (+13%)
      • pickle: 22.1 us +- 0.3 us -> 24.9 us +- 0.3 us: 1.12x slower (+12%)
      • nbody: 233 ms +- 2 ms -> 260 ms +- 2 ms: 1.12x slower (+12%)
      • xml_etree_iterparse: 179 ms +- 4 ms -> 198 ms +- 5 ms: 1.11x slower (+11%)
      • telco: 14.7 ms +- 0.3 ms -> 16.3 ms +- 0.5 ms: 1.11x slower (+11%)
      • pickle_dict: 56.6 us +- 4.3 us -> 62.7 us +- 4.7 us: 1.11x slower (+11%)
      • pidigits: 291 ms +- 1 ms -> 319 ms +- 1 ms: 1.10x slower (+10%)
      • scimark_fft: 662 ms +- 10 ms -> 717 ms +- 8 ms: 1.08x slower (+8%)
      • scimark_monte_carlo: 207 ms +- 4 ms -> 224 ms +- 6 ms: 1.08x slower (+8%)
      • regex_v8: 43.7 ms +- 0.6 ms -> 47.0 ms +- 0.3 ms: 1.08x slower (+8%)
      • float: 238 ms +- 3 ms -> 254 ms +- 4 ms: 1.07x slower (+7%)
      • xml_etree_parse: 242 ms +- 5 ms -> 257 ms +- 9 ms: 1.06x slower (+6%)
      • raytrace: 1.04 sec +- 0.01 sec -> 1.11 sec +- 0.01 sec: 1.06x slower (+6%)
      • unpickle: 30.0 us +- 0.3 us -> 31.8 us +- 0.3 us: 1.06x slower (+6%)
      • go: 493 ms +- 7 ms -> 520 ms +- 6 ms: 1.05x slower (+5%)
      • scimark_sparse_mat_mult: 8.24 ms +- 0.14 ms -> 8.69 ms +- 0.14 ms: 1.05x slower (+5%)
      • chaos: 234 ms +- 2 ms -> 246 ms +- 4 ms: 1.05x slower (+5%)

      Faster (2):

      • chameleon: 21.9 ms +- 0.2 ms -> 20.7 ms +- 0.3 ms: 1.06x faster (-5%)
      • sympy_expand: 949 ms +- 12 ms -> 899 ms +- 11 ms: 1.06x faster (-5%)

      Benchmark hidden because not significant (43): (...)

    10. scoder commented on Feb 7, 2017

      @scoder
      Contributor

      Thanks for asking. Cython doesn't use METH_FASTCALL yet, so this doesn't introduce any problems.

      Generally speaking, if Cython generated user code stops working with a new CPython version, we expect people to regenerate their code with the newest Cython version to fix it, so this kind of internal incompatibility is usually reparable by adapting Cython appropriately.

    11. serhiy-storchaka commented on Feb 7, 2017

      @serhiy-storchaka
      MemberAuthor

      There was a bug in previous patch. The signature of generated by Argument Clinic functions was not changed. Updated patch fixes this bug and also fixes the use of fast call in deque methods.

    12. vstinner commented on Feb 7, 2017

      @vstinner
      Member

      Ok, so I looked again at your change: fastcall-no-keywords-2.patch LGTM,
      I like the idea!

      Since your patch is huge, I expect that it will be a pain to rebase it. I
      suggest you to push it as soon as possible, to avoid conflicts. I will rework
      my issue bpo-29465 patch once this issue is done.

      According to the number of modified functions and files, not accepting keyword
      arguments is very common, and so it is annoying to have to call
      _PyArg_NoStackKeywords() in each function. Avoiding the "PyObject *kwnames" can
      also reduces the stack consumption per call, especially in long call stacks.

      At least my assumption that almost no function will accept only positional
      arguments is plain wrong :-) In practice, it's more the opposite!

      I checked quickly fastcall-no-keywords-2.patch stack usage and performance.
      It seems non significant, but I'm not surprised, it isn't really the purpose
      of the change. IMHO the purpose of the change is more to simplify the code,
      avoid to duplicate _PyArg_NoStackKeywords() everywhere, and only check
      keyword arguments at one place.

      For stack consumption and performances, I got good results on my issue bpo-29465
      which adds a new _PyObject_FastCall() function. IMHO this function fits well
      with your new METH_FASTCALL (no keyword argument)! If we combine both changes,
      we can get something very good ;-)

      @Stefan Behnel: Thank you for you reply. So the backward compatibility was a
      fake issue, and we are free to modify METH_FASTCALL. Anyway, *if* someone uses
      METH_FASTCALL, IMHO it will be easy to update his/her code and add #ifdef on
      the Python version if needed. Again, as Serhiy wrote, METH_FASTCALL is out of
      the stable ABI, so we are safe on our warranties.

    13. serhiy-storchaka commented on Feb 7, 2017

      @serhiy-storchaka
      MemberAuthor

      The major part of the patch is generated by Argument Clinic. It is not a pain to rebase it. I prefer to delay pushing the patch until we prove its usefulness, because the cost of this change is not zero. Is it a stopper for bpo-29465?

    14. 16 remaining items

    15. vstinner commented on Jul 5, 2017

      @vstinner
      Member

      New changeset 8207c17 by Victor Stinner in branch 'master':
      Revert "bpo-30822: Fix testing of datetime module." (bpo-2588)
      8207c17

    16. serhiy-storchaka commented on Jul 10, 2017

      @serhiy-storchaka
      MemberAuthor

      Josay noticed that _PyArg_NoStackKeywords() is not used anymore except for one place (_hashopenssl.c).

      Seems this is my oversign. Generated constructors in _hashopenssl.c use the METH_FASTCALL | METH_KEYWORDS calling method, but check that no keyword arguments are passed. They can now just use the METH_FASTCALL calling method. And _PyArg_NoStackKeywords() can be excluded from public header.

    17. vstinner commented on Jul 10, 2017

      @vstinner
      Member

      Let's remove this function which became useless.

    18. serhiy-storchaka commented on Jul 10, 2017

      @serhiy-storchaka
      MemberAuthor

      Removed _PyArg_NoStackKeywords() (7e60192).

    19. timgraham commented on Jul 12, 2017

      timgrahammannequin
      Mannequin

      Hi, I observed an error while trying to install numpy after 6969eaf.

      gcc: numpy/random/mtrand/mtrand.c
      numpy/random/mtrand/mtrand.c: In function ‘__Pyx_PyCFunction_FastCall’:
      numpy/random/mtrand/mtrand.c:44374:5: error: too many arguments to function ‘(struct PyObject * ()(struct PyObject *, struct PyObject **, Py_ssize_t))meth’
      return (
      ((__Pyx_PyCFunctionFast)meth)) (self, args, nargs, NULL);
      ^

      Is this a bug in Python or does it need to be fixed in numpy?

    20. serhiy-storchaka commented on Jul 12, 2017

      @serhiy-storchaka
      MemberAuthor

      I think you need to update Cython to a version that supports new FASTCALL call convention in 3.7. If the support of FASTCALL in Cython is optional try to disable it.

    21. timgraham commented on Jul 12, 2017

      timgrahammannequin
      Mannequin

      Thanks, I'm not sure what that means exactly but I added the note to numpy/numpy#9391. Perhaps a note in the Python release notes is warranted?

    22. scoder commented on Jul 13, 2017

      @scoder
      Contributor

      For future reference, this change is supported by Cython 0.26 (which is currently close to release).

    23. serhiy-storchaka commented on Jul 13, 2017

      @serhiy-storchaka
      MemberAuthor

      Great!

      Are all uses of internal CPython details optional? I would disable them by default for alpha versions of CPython.

    24. scoder commented on Jul 13, 2017

      @scoder
      Contributor

      Are all uses of internal CPython details optional?

      Well, what classifies as a "CPython detail" sometimes just becomes clear when other implementations don't have it. ;-)

      But yes, the C code that Cython generates selects alternative implementations based on some more or less generic C defines, e.g. CYTHON_COMPILING_IN_CPYTHON or CYTHON_USE_PYLONG_INTERNALS or CYTHON_ASSUME_SAFE_MACROS. We enable them based on the Python implementation and even users can disable them at need. Well, and then there are obviously tons of PY_VERSION_HEX specific special cases, such as this one.

      I would disable them by default for alpha versions of CPython.

      I don't see why. We track the CPython development via travis-CI and alpha versions make us aware of changes that we need to incorporate (or sometimes fight against ;-) ). These internals very rarely change across CPython releases (e.g. the PyLong type didn't really change since 3.0/2.7.0), and this one is really an exception. Excluding alpha versions would probably reduce our response time to these changes.

    25. vstinner commented on Jul 13, 2017

      @vstinner
      Member

      When I worked on FASTCALL, I wasn't sure that the whole project was worth
      it. Now I am more confident that it makes function calls faster:
      https://haypo.github.io/fastcall-microbenchmarks.html

      Stefan, Serhiy: would it be a good idea to make _PyObject_FastCall() and
      _PyObject_FastCallKeywords(), or also _PyObject_FastCallDict(), public? I
      expect complains from PyPy who will have to support METH_FASTCALL as well
      :-)

    26. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.7 (EOL)interpreter-coreperformance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions