diff options
author | L. A. F. Pereira <l.pereira@microsoft.com> | 2023-01-03 18:49:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-03 18:49:49 (GMT) |
commit | e6d44407827490a5345e8393fbdc78fd6c14f5b1 (patch) | |
tree | 2f8537e266b484373b806fa7755d9edf40120ca4 /Python | |
parent | b3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19 (diff) | |
download | cpython-e6d44407827490a5345e8393fbdc78fd6c14f5b1.zip cpython-e6d44407827490a5345e8393fbdc78fd6c14f5b1.tar.gz cpython-e6d44407827490a5345e8393fbdc78fd6c14f5b1.tar.bz2 |
gh-100146: Steal references from stack when building a list (#100147)
When executing the BUILD_LIST opcode, steal the references from the stack,
in a manner similar to the BUILD_TUPLE opcode. Implement this by offloading
the logic to a new private API, _PyList_FromArraySteal(), that works similarly
to _PyTuple_FromArraySteal().
This way, instead of performing multiple stack pointer adjustments while the
list is being initialized, the stack is adjusted only once and a fast memory
copy operation is performed in one fell swoop.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bytecodes.c | 7 | ||||
-rw-r--r-- | Python/generated_cases.c.h | 7 |
2 files changed, 4 insertions, 10 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c index e1c73ab..839fac3 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1390,13 +1390,10 @@ dummy_func( // stack effect: (__array[oparg] -- __0) inst(BUILD_LIST) { - PyObject *list = PyList_New(oparg); + STACK_SHRINK(oparg); + PyObject *list = _PyList_FromArraySteal(stack_pointer, oparg); if (list == NULL) goto error; - while (--oparg >= 0) { - PyObject *item = POP(); - PyList_SET_ITEM(list, oparg, item); - } PUSH(list); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 1179bdf..ed89e90 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1608,13 +1608,10 @@ } TARGET(BUILD_LIST) { - PyObject *list = PyList_New(oparg); + STACK_SHRINK(oparg); + PyObject *list = _PyList_FromArraySteal(stack_pointer, oparg); if (list == NULL) goto error; - while (--oparg >= 0) { - PyObject *item = POP(); - PyList_SET_ITEM(list, oparg, item); - } PUSH(list); DISPATCH(); } |