Skip to content

gh-162350: Add AC vectorcall for property, sentinel, and typing - #162440

Open
tanloong wants to merge 7 commits into
python:mainfrom
tanloong:memoryview_vectorcall
Open

tanloong wants to merge 7 commits into
python:mainfrom
tanloong:memoryview_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

1. property

Add @vectorcall to property.__init__ (the constructor's argument parsing lives there; tp_new is PyType_GenericNew). There is no change of exception text for bad arguments.

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| property()                     | 121 ns | 108 ns: 1.12x faster   |
| property(f)                    | 168 ns | 135 ns: 1.24x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.18x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import builtins
def f(self): pass
property = builtins.property
"""

CASES = [
    ('property()', 'property()'),
    ('property(f)', 'property(f)')
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

2. sentinel

Add @vectorcall to sentinel.__new__. The keyword-only repr parameter means AC routes calls through a helper, but the vectorcall still avoids constructing an argument tuple.

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| sentinel('x')                  | 158 ns | 116 ns: 1.37x faster   |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import builtins
sentinel = builtins.sentinel
"""

CASES = [
    ("sentinel('x')", "sentinel('x')"),
    ("builtins.sentinel('x')", "builtins.sentinel('x')")
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

3. typing

Add @vectorcall to the TypeAliasType, ParamSpecArgs and ParamSpecKwargs constructors. No change of exception text for bad arguments.

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| TypeAliasType('T', int)        | 160 ns | 124 ns: 1.29x faster   |
| ParamSpecArgs(ps)              | 139 ns | 103 ns: 1.36x faster   |
| ParamSpecKwargs(ps)            | 133 ns | 102 ns: 1.30x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.32x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import typing
from typing import TypeAliasType, ParamSpecArgs, ParamSpecKwargs
ps = typing.ParamSpec('P')
"""

CASES = [
    ("TypeAliasType('T', int)", "TypeAliasType('T', int)"),
    ('ParamSpecArgs(ps)', 'ParamSpecArgs(ps)'),
    ('ParamSpecKwargs(ps)', 'ParamSpecKwargs(ps)')
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

@tanloong
tanloong marked this pull request as draft November 22, 2026 15:47
@tanloong tanloong changed the title gh-162350: Add AC vectorcall for memoryview gh-162350: Add AC vectorcall for memoryview, property, sentinel, and typing Nov 22, 2026
@tanloong
tanloong marked this pull request as ready for review November 22, 2026 16:01
@cmaloney

Copy link
Copy Markdown
Contributor

memoryview needs to happen separately from these other ones, it is used in different contexts and is a much more clear win.

for sentinel and ParamSpec will want ot make sure the C code that often calls them bypasses argument parsing when appropriate like we've done for a couple other types. May also make sense to make the objects/types their public names (ex. TypeAliasType) so error messages are clearer.

@tanloong tanloong changed the title gh-162350: Add AC vectorcall for memoryview, property, sentinel, and typing gh-162350: Add AC vectorcall for property, sentinel, and typing Nov 23, 2026
@tanloong

Copy link
Copy Markdown
Contributor Author

Separated memoryview in #162471.

Checked that there is already no extra argument parsing. For sentinel the call chain is sentinel_new/sentinel_vectorcall --> sentinel_new_helper (argument parsing) --> sentinel_new_impl --> sentinel_new_with_module. The argument parsing happens in sentinel_new_helper(). PySentinel_New() calls the lower sentinel_new_with_module(), thus bypasses argument parsing. Similary, for ParamSpec the paramspec_args() and paramspec_kwargs() calls lower paramspecattr_new(), bypassing argument parsing in paramspecargs_new_helper().

Updated to use public names, before:

TypeError: typealias() missing required argument 'name' (pos 1)
TypeError: paramspeckwargs() missing required argument 'origin' (pos 1)
TypeError: paramspecargs() missing required argument 'origin' (pos 1)

After:

TypeError: TypeAliasType() missing required argument 'name' (pos 1)
TypeError: ParamSpecKwargs() missing required argument 'origin' (pos 1)
TypeError: ParamSpecArgs() missing required argument 'origin' (pos 1)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants