Skip to content

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

Merged
asvetlov merged 6 commits into
python:mainfrom
tanloong:celltype_vectorcall
Nov 22, 2026
Merged

asvetlov merged 6 commits 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)
…s AC-generated signature 'cell(contents=None, /)' which is supported by 'inspect.signature'
Comment thread Objects/cellobject.c Resolved
…t now has AC-generated signature 'cell(contents=None, /)' which is supported by 'inspect.signature'"

This reverts commit c2d3ea6.
Comment thread Objects/cellobject.c Resolved
Comment thread Objects/cellobject.c Resolved
@asvetlov
asvetlov merged commit f80a0fa into python:main Nov 22, 2026
58 checks passed
@tanloong
tanloong deleted the celltype_vectorcall branch November 22, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants