Skip to content

Commit 9de76f2

Browse files
authored
gh-162350: Add AC vectorcall for function.__new__ to speed up types.FunctionType (#162433)
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 | +-------------------------------+--------+----------------------+ ``` ```python 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) ```
1 parent 39af026 commit 9de76f2

2 files changed

Lines changed: 72 additions & 6 deletions

File tree

‎Objects/clinic/funcobject.c.h‎

Lines changed: 69 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/funcobject.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ static PyGetSetDef func_getsetlist[] = {
10961096
*/
10971097

10981098
/*[clinic input]
1099+
@vectorcall
10991100
@classmethod
11001101
function.__new__ as func_new
11011102
code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
@@ -1118,7 +1119,7 @@ static PyObject *
11181119
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
11191120
PyObject *name, PyObject *defaults, PyObject *closure,
11201121
PyObject *kwdefaults)
1121-
/*[clinic end generated code: output=de72f4c22ac57144 input=20c9c9f04ad2d3f2]*/
1122+
/*[clinic end generated code: output=de72f4c22ac57144 input=264767a5dc845c90]*/
11221123
{
11231124
PyFunctionObject *newfunc;
11241125
Py_ssize_t nclosure;
@@ -1326,6 +1327,7 @@ PyTypeObject PyFunction_Type = {
13261327
0, /* tp_init */
13271328
0, /* tp_alloc */
13281329
func_new, /* tp_new */
1330+
.tp_vectorcall = func_vectorcall,
13291331
};
13301332

13311333

0 commit comments

Comments
 (0)