Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_list.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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) {
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
Original file line number Diff line number Diff line change
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.