[Python-checkins] gh-94603: micro optimize list.pop (gh-94604)

corona10 webhook-mailer at python.org
Tue Dec 27 05:56:00 EST 2022


https://github.com/python/cpython/commit/b3da6989524001d707ebb386bfab998452aa57bb
commit: b3da6989524001d707ebb386bfab998452aa57bb
branch: main
author: Pieter Eendebak <pieter.eendebak at gmail.com>
committer: corona10 <donghee.na92 at gmail.com>
date: 2022-12-27T19:55:54+09:00
summary:

gh-94603: micro optimize list.pop (gh-94604)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst
M Objects/listobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst
new file mode 100644
index 000000000000..de4fe4d6df8c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst	
@@ -0,0 +1 @@
+Improve performance of ``list.pop`` for small lists.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 1d32915b17a1..b093f88a35fc 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1022,21 +1022,29 @@ 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) {
-        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;
+
+    PyObject **items = self->ob_item;
+    v = items[index];
+    const Py_ssize_t size_after_pop = Py_SIZE(self) - 1;
+    if (size_after_pop == 0) {
+        Py_INCREF(v);
+        status = _list_clear(self);
     }
-    Py_INCREF(v);
-    status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
-    if (status < 0) {
-        Py_DECREF(v);
+    else {
+        if ((size_after_pop - index) > 0) {
+            memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *));
+        }
+        status = list_resize(self, size_after_pop);
+    }
+    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], (size_after_pop - index)* sizeof(PyObject *));
+        items[index] = v;
         return NULL;
     }
-    return v;
 }
 
 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */



More information about the Python-checkins mailing list