Skip to content

gh-162350: Use AC vectorcall for cell.__new__ to speed up types.CellType - #162422

Open
tanloong wants to merge 1 commit into
python:mainfrom
tanloong:celltype_vectorcall
Open

tanloong wants to merge 1 commit into
python:mainfrom
tanloong:celltype_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written cell_new with Argument Clinic + Vectorcall. Observed more than 10% performance boost.

Benchmark (default options):

+--------------------+--------+-----------------------+
| Benchmark          | main   | patch                 |
+====================+========+=======================+
| CellType(42)       | 122 ns | 91.7 ns: 1.33x faster |
+--------------------+--------+-----------------------+
| types.CellType(42) | 131 ns | 102 ns: 1.28x faster  |
+--------------------+--------+-----------------------+
| CellType()         | 103 ns | 83.9 ns: 1.22x faster |
+--------------------+--------+-----------------------+
| Geometric mean     | (ref)  | 1.28x faster          |
+--------------------+--------+-----------------------+
Details
import pyperf

SETUP = """
import sys
import types
from types import (CellType, FunctionType, MappingProxyType,
                   FrameLocalsProxyType)

def f(self):
    pass

class C:
    def m(self):
        pass

obj = C()
code = f.__code__
gns = globals()
d = {}
frame = sys._getframe()
"""

CASES = [
    ("CellType(42)", "CellType(42)"),
    ("types.CellType(42)", "types.CellType(42)"),
    ("CellType()", "CellType()"),
]


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

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