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-94603: micro optimize list.pop #94604

Merged
merged 17 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of ``list.pop`` for small lists.
29 changes: 18 additions & 11 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,21 +1022,28 @@ list_pop_impl(PyListObject *self, Py_ssize_t index)
PyErr_SetString(PyExc_IndexError, "pop index out of range");
return NULL;
}
v = self->ob_item[index];
if (index == Py_SIZE(self) - 1) {

PyObject **items = self->ob_item;
v = items[index];
if(Py_SIZE(self)-1==0) {
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
Py_INCREF(v);
status = _list_clear(self);
}
else {
if (Py_SIZE(self)-1-index) {
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
memmove(&items[index], &items[index+1], (Py_SIZE(self)-1-index)* sizeof(PyObject *));
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
}
status = list_resize(self, Py_SIZE(self) - 1);
if (status >= 0)
return v; /* and v now owns the reference the list had */
else
return NULL;
}
Py_INCREF(v);
status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
if (status < 0) {
Py_DECREF(v);
if (status >= 0) {
return v; // and v now owns the reference the list had
}
else {
// list resize failed, need to restore
memmove(&items[index+1], &items[index], (Py_SIZE(self)-1-index)* sizeof(PyObject *));
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
items[index] = v;
return NULL;
}
return v;
}

/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
Expand Down