Skip to content

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

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

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

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

1. memoryview

Add @vectorcall to memoryview.__new__. There is no change of exception text for bad arguments.

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| memoryview(b)                  | 170 ns | 143 ns: 1.19x faster   |
| builtins.memoryview(b)         | 179 ns | 149 ns: 1.20x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.20x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import builtins
b = b'abc'
memoryview = builtins.memoryview
"""

CASES = [
    ('memoryview(b)', 'memoryview(b)'),
    ('builtins.memoryview(b)', 'builtins.memoryview(b)')
]

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

2. 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)

3. 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)

4. 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
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.

1 participant