gh-87613: Argument Clinic vectorcall decorator#145381
gh-87613: Argument Clinic vectorcall decorator#145381cmaloney wants to merge 4 commits intopython:mainfrom
Conversation
Add `@vectorcall` as a decorator to Argument Clinic (AC) which generates a new [Vectorcall Protocol](https://docs.python.org/3/c-api/call.html#the-vectorcall-protocol) argument parsing C function named `{}_vectorcall`. This is only supported for `__new__` and `__init__` currently to simplify implementation. The generated code has similar or better performance to existing hand-written cases for `list`, `float`, `str`, `tuple`, `enumerate`, `reversed`, and `int`. Using the decorator added vectorcall to `bytearray` and construction got 1.09x faster. For more details see the comments in pythongh-87613. The `@vectorcall` decorator has two options: - **zero_arg={C_FUNC}**: Some types, like `int`, can be called with zero arguments and return an immortal object in that case. Adding a shortcut is needed to match existing hand-written performance; provides an over 10% performance change for those cases. - **exact_only**: If the type is not an exact match delegate to the existing non-vectorcall implementation. NEeded for `str` to get matching performance while ensuring correct behavior. Implementation details: - Adds support for the new decorator with arguments in the AC DSL Parser - Move keyword argument parsing generation from inline to a function so both vectorcall, `vc_`, and existing can share code generation. - Adds an `emit` helper to simplify code a bit from existing AC cases Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
corona10
left a comment
There was a problem hiding this comment.
Could you replace current hand-written with your new DSL.
Let's see how handle them.
|
I have commits to do that in my draft branch (https://github.com/python/cpython/compare/main...cmaloney:cpython:ac_vectorcall_v1?expand=0); can pull them into this branch if that would be easier / better to review. This generally produces code that is as fast or faster than the hand-written ones currently (full benchmarking in: #87613 (comment)) |
|
Added commits moving |
Add
@vectorcallas a decorator to Argument Clinic (AC) which emits a Vectorcall Protocol argument parsing C function named{type}_vectorcall. This is only supported for__new__and__init__currently to simplify implementation.The generated code has similar or better performance to existing hand-written cases for
list,float,str,tuple,enumerate,reversed, andint. Using the decorator onbytearray, which has no handwritten case, construction got 1.09x faster. For more benchmark details see #87613 (comment).The
@vectorcalldecorator has two options:zero_arg={C_FUNC}: Some types, likeint, can be called with zero arguments and return an immortal object in that case. Adding a shortcut is needed to match existing hand-written performance; provides an over 10% performance change for those cases.exact_only: If the type is not an exact match delegate to the existing non-vectorcall implementation. Needed forstrto get matching performance while ensuring correct behavior.Implementation details:
vc_, and existing can share code generation.emithelper to simplify code a bit from existing AC casesCo-Authored-By: Claude Opus 4.6 noreply@anthropic.com