Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-100146: Steal references from stack when building a list #100147

Merged
merged 1 commit into from Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_list.h
Expand Up @@ -75,6 +75,8 @@ typedef struct {
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} _PyListIterObject;

extern PyObject *_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n);

#ifdef __cplusplus
}
#endif
Expand Down
@@ -0,0 +1,4 @@
Improve ``BUILD_LIST`` opcode so that it works similarly to the
``BUILD_TUPLE`` opcode, by stealing references from the stack rather than
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Improve ``BUILD_LIST`` opcode so that it works similarly to the
``BUILD_TUPLE`` opcode, by stealing references from the stack rather than
Improve :opcode:`BUILD_LIST` so that it works similarly to
:opcode:`BUILD_TUPLE`, by stealing references from the stack rather than

repeatedly using stack operations to set list elements. Implementation
details are in a new private API :c:func:`_PyList_FromArraySteal`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
details are in a new private API :c:func:`_PyList_FromArraySteal`.
details are in a new private API :c:func:`!_PyList_FromArraySteal`.

No need to link to private functions.

21 changes: 21 additions & 0 deletions Objects/listobject.c
Expand Up @@ -2557,6 +2557,27 @@ PyList_AsTuple(PyObject *v)
return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
}

PyObject *
_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
{
if (n == 0) {
lpereira marked this conversation as resolved.
Show resolved Hide resolved
return PyList_New(0);
}

PyListObject *list = (PyListObject *)PyList_New(n);
if (list == NULL) {
for (Py_ssize_t i = 0; i < n; i++) {
Py_DECREF(src[i]);
}
return NULL;
}

PyObject **dst = list->ob_item;
memcpy(dst, src, n * sizeof(PyObject *));

return (PyObject *)list;
}

/*[clinic input]
list.index

Expand Down
7 changes: 2 additions & 5 deletions Python/bytecodes.c
Expand Up @@ -1441,13 +1441,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);
}

Expand Down
7 changes: 2 additions & 5 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.