Skip to content

Commit 0296c20

Browse files
committed
Replace a call to PyTuple_New with _PyTuple_FromArraySteal
PyTuple_New will zero out the tuple before returning to the caller, and a surprising amount of time can be saved by not doing this zeroing. One option is to add a non-zeroing version of PyTuple_New, which I did in python#96446, but there was resistance to the unsafety of it. Fortunately it looks like most of the tuple-zeroing happens directly from the BUILD_TUPLE opcode in the interpreter, which already has the arguments in an appropriate array, so we can just convert this to _PyTuple_FromArraySteal This seems to result in a ~0.2% speedup on macrobenchmarks.
1 parent 7b01ce7 commit 0296c20

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

Python/ceval.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,13 +2647,10 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
26472647
}
26482648

26492649
TARGET(BUILD_TUPLE) {
2650-
PyObject *tup = PyTuple_New(oparg);
2650+
STACK_SHRINK(oparg);
2651+
PyObject *tup = _PyTuple_FromArraySteal(stack_pointer, oparg);
26512652
if (tup == NULL)
26522653
goto error;
2653-
while (--oparg >= 0) {
2654-
PyObject *item = POP();
2655-
PyTuple_SET_ITEM(tup, oparg, item);
2656-
}
26572654
PUSH(tup);
26582655
DISPATCH();
26592656
}

0 commit comments

Comments
 (0)