Skip to content

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

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

tanloong wants to merge 5 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
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