Skip to content

gh-162350: Add AC vectorcall for function.__new__ to speed up types.FunctionType - #162433

Merged
cmaloney merged 1 commit into
python:mainfrom
tanloong:functiontype_vectorcall
Nov 22, 2026
Merged

cmaloney merged 1 commit into
python:mainfrom
tanloong:functiontype_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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

Benchmark:

+-------------------------------+--------+----------------------+
| Benchmark                     | main   | patch                |
+===============================+========+======================+
| FunctionType(code, gns)       | 196 ns | 150 ns: 1.31x faster |
+-------------------------------+--------+----------------------+
| types.FunctionType(code, gns) | 193 ns | 153 ns: 1.26x faster |
+-------------------------------+--------+----------------------+
| Geometric mean                | (ref)  | 1.28x faster         |
+-------------------------------+--------+----------------------+
Details
import pyperf

SETUP = """
import sys
import types
from types import FunctionType

def f(self):
    pass

class C:
    def m(self):
        pass

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

CASES = [
    ("FunctionType(code, gns)", "FunctionType(code, gns)"),
    ("types.FunctionType(code, gns)", "types.FunctionType(code, gns)"),
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)
@cmaloney
cmaloney merged commit 9de76f2 into python:main Nov 22, 2026
62 checks passed
@tanloong
tanloong deleted the functiontype_vectorcall branch November 23, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants