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

bpo-39829: Optimize __len__() being called twice in the list() constructor #31816

Merged
merged 15 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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: 1 addition & 1 deletion Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ William Park
Claude Paroz
Heikki Partanen
Harri Pasanen
Jeremiah Gabriel Pascual
Gaël Pasgrimaud
Feanil Patel
Ashish Nitin Patil
Expand Down Expand Up @@ -1863,7 +1864,6 @@ Kurt Vile
Norman Vine
Pauli Virtanen
Frank Visser
Jeremiah Vivian (Pascual)
Johannes Vogel
Michael Vogt
Radu Voicilas
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed the ``__len__()`` call when initializing a list and moved initializing to ``list_extend``. Patch by Jeremiah Pascual.
29 changes: 12 additions & 17 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ list_extend(PyListObject *self, PyObject *iterable)
PyObject *it; /* iter(v) */
Py_ssize_t m; /* size of self */
Py_ssize_t n; /* guess for size of iterable */
Py_ssize_t mn; /* m + n */
Py_ssize_t i;
PyObject *(*iternext)(PyObject *);

Expand All @@ -889,7 +888,13 @@ list_extend(PyListObject *self, PyObject *iterable)
/* It should not be possible to allocate a list large enough to cause
an overflow on any relevant platform */
assert(m < PY_SSIZE_T_MAX - n);
if (list_resize(self, m + n) < 0) {
if (self->ob_item == NULL) {
if (list_preallocate_exact(self, n) < 0) {
return NULL;
}
Py_SET_SIZE(self, n);
}
else if (list_resize(self, m + n) < 0) {
Py_DECREF(iterable);
return NULL;
}
Expand Down Expand Up @@ -928,10 +933,13 @@ list_extend(PyListObject *self, PyObject *iterable)
* eventually run out of memory during the loop.
*/
}
else if (self->ob_item == NULL) {
if (n && list_preallocate_exact(self, n) < 0)
goto error;
}
else {
mn = m + n;
/* Make room. */
if (list_resize(self, mn) < 0)
if (list_resize(self, m + n) < 0)
goto error;
/* Make the list sane again. */
Py_SET_SIZE(self, m);
Expand Down Expand Up @@ -2814,19 +2822,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
(void)_list_clear(self);
}
if (iterable != NULL) {
if (_PyObject_HasLen(iterable)) {
Py_ssize_t iter_len = PyObject_Size(iterable);
if (iter_len == -1) {
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
return -1;
}
PyErr_Clear();
}
if (iter_len > 0 && self->ob_item == NULL
&& list_preallocate_exact(self, iter_len)) {
return -1;
}
}
PyObject *rv = list_extend(self, iterable);
if (rv == NULL)
return -1;
Expand Down