Skip to content

CALL_FUNCTION_EX argument passing routine can be improved for vectorcalls #95757

Open

Description

@Fidget-Spinner

While working on #95126, I noticed that CALL_FUNCTION_EX for vectorcall Python functions is extremely wasteful. These are its steps:

  1. CALL_FUNCTION_EX calls do_call_core with a args tuple and kwargs dict.
  2. Eventually it's detected that the function supports vectorcall, and it calls _PyVectorcall_Call. This calls _PyStack_UnpackDict, which unpacks the args tuple and kwargs dict into a C array.
  3. This C array is passed to the vectorcall function. Eventually calling _PyEval_Vector.
  4. _PyEval_Vector calls _PyEvalFramePushAndInit, which sees that there's complex args, and recreates the tuple and args dict in initialize_locals from the C array.
  5. Frame is pushed and evaluated, then popped. End.

We shouldn't even be converting the arguments to a C array just to vectorcall. We should instead directly transfer ownership of args tuple and kwds dict because that would be significantly faster in this case (and save us the tuple and dict recreation dance).

CC @markshannon and @brandtbucher .

Linked PRs

Activity

  1. added
    type-feature
    performance
    on Aug 7, 2022
  2. brandtbucher commented on Aug 7, 2022

    @brandtbucher
    Member

    Not too familiar with the vectorcall sequence (and don’t have a computer right now), but just for my own understanding: would what you’re proposing (not copying the kwargs) break this?

    def f(**kwargs):
        kwargs["spam"] = False
    
    kwargs = {"spam": True}
    f(**kwargs)
    assert kwargs["spam"]

    If not, then what you’re proposing sounds reasonable to me!

  3. Fidget-Spinner commented on Aug 7, 2022

    @Fidget-Spinner
    MemberAuthor

    I suspect what I'm suggesting will break what you described. So I'm dropping this.

  4. TeamSpen210 commented on Aug 7, 2022

    @TeamSpen210

    Actually it looks like that’s handled in the bytecode - it’ll always build an empty dict, then call update() with the arg. So it’ll already be a copy by the point CALL_FUNCTION_EX does anything.

  5. Fidget-Spinner commented on Aug 11, 2022

    @Fidget-Spinner
    MemberAuthor

    The vectorcall protocol copies that copy one more time @TeamSpen210 .

    Actually on further consideration, we could have a lazy dict, where it only copies on write/modification. That should gain some speedups. However the complexity might not be worth it.

  6. TeamSpen210 commented on Aug 11, 2022

    @TeamSpen210

    The additional copy vectorcall does is quite wasteful yes, but since the bytecode has done a copy by that point, we know it's safe to pass directly though to the frame - it can't affect the caller.

    I'm not sure a lazy dict is really necessary though. Unlike the vectorcall copy from an array, the bytecode copy calls update() which in these cases can just memcpy the dict data.

  7. added
    interpreter-core
    on Nov 27, 2023
  8. cmaloney commented on Dec 4, 2026

    @cmaloney
    Contributor

    related: gh-86199 and open PR GH-156384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coreperformancetype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions