From 79ac0ff8ec6cbf29e4070dba797165b44b806654 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 6 Jul 2022 14:33:27 +0200 Subject: [PATCH 1/9] optimize list.pop --- Objects/listobject.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 83dfb7da01dfc3..93f35d57c6dfcd 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1040,21 +1040,26 @@ 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) { + Py_INCREF(v); + status = _list_clear(self); + } + else { + if (Py_SIZE(self)-1-index) + memmove(&items[index], &items[index+1], (Py_SIZE(self)-1-index)* sizeof(PyObject *)); 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 *)); + items[index] = v; return NULL; } - return v; } /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ From 4cf3c199dbeb556843a6e11ccb392802babaa756 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 6 Jul 2022 18:44:01 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst 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 00000000000000..a2a8e0c144ab2d --- /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. From 4db7ec56a2ac9c5c0479a56449df3eb36882259c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 6 Jul 2022 23:25:18 +0200 Subject: [PATCH 3/9] fix news item --- .../2022-07-06-18-44-00.gh-issue-94603.Q_03xV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index a2a8e0c144ab2d..de4fe4d6df8c3a 100644 --- 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 @@ -1 +1 @@ -Improve performance of `list.pop` for small lists. +Improve performance of ``list.pop`` for small lists. From 1512885e242707ba2aa84346e410316d61bc5730 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 Jul 2022 10:32:09 +0200 Subject: [PATCH 4/9] Update Objects/listobject.c Co-authored-by: Dong-hee Na --- Objects/listobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 93f35d57c6dfcd..43e533d146f3c2 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1052,8 +1052,9 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) memmove(&items[index], &items[index+1], (Py_SIZE(self)-1-index)* sizeof(PyObject *)); status = list_resize(self, Py_SIZE(self) - 1); } - if (status >= 0) - return v; /* and v now owns the reference the list had */ + 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 *)); From 58e38d6ec7e07406cb65545e72a2d2ad42442950 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 10 Jul 2022 17:09:42 +0200 Subject: [PATCH 5/9] Update Objects/listobject.c Co-authored-by: Dong-hee Na --- Objects/listobject.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 43e533d146f3c2..242f5eceb13f7e 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1048,8 +1048,9 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) status = _list_clear(self); } else { - if (Py_SIZE(self)-1-index) + if (Py_SIZE(self)-1-index) { memmove(&items[index], &items[index+1], (Py_SIZE(self)-1-index)* sizeof(PyObject *)); + } status = list_resize(self, Py_SIZE(self) - 1); } if (status >= 0) { From 44a4efe94f07040f73b0d3d0a84942001c8d5733 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 26 Dec 2022 21:01:56 +0100 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Dong-hee Na --- Objects/listobject.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 58b98bfc2bc8f2..b8c7c5df3225cc 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1025,13 +1025,14 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) PyObject **items = self->ob_item; v = items[index]; - if(Py_SIZE(self)-1==0) { + Py_ssize_t pop_after_size = Py_SIZE(self) - 1; + if(pop_after_size == 0) { Py_INCREF(v); status = _list_clear(self); } else { - if (Py_SIZE(self)-1-index) { - memmove(&items[index], &items[index+1], (Py_SIZE(self)-1-index)* sizeof(PyObject *)); + if ((pop_after_size - index) > 0) { + memmove(&items[index], &items[index+1], (pop_after_size - index) * sizeof(PyObject *)); } status = list_resize(self, Py_SIZE(self) - 1); } @@ -1040,7 +1041,7 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) } else { // list resize failed, need to restore - memmove(&items[index+1], &items[index], (Py_SIZE(self)-1-index)* sizeof(PyObject *)); + memmove(&items[index+1], &items[index], (pop_after_size - index)* sizeof(PyObject *)); items[index] = v; return NULL; } From 7c6c9fa0f441e65aad6037fc7705514ffaec0940 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 26 Dec 2022 21:04:44 +0100 Subject: [PATCH 7/9] rename to size_after_pop --- Objects/listobject.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index b8c7c5df3225cc..7d65bb3714cc9b 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1025,23 +1025,23 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) PyObject **items = self->ob_item; v = items[index]; - Py_ssize_t pop_after_size = Py_SIZE(self) - 1; - if(pop_after_size == 0) { + Py_ssize_t size_after_pop = Py_SIZE(self) - 1; + if(size_after_pop == 0) { Py_INCREF(v); status = _list_clear(self); } else { - if ((pop_after_size - index) > 0) { - memmove(&items[index], &items[index+1], (pop_after_size - index) * sizeof(PyObject *)); + if ((size_after_pop - index) > 0) { + memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *)); } - status = list_resize(self, Py_SIZE(self) - 1); + status = list_resize(self, size_after_pop); } if (status >= 0) { - return v; // and v now owns the reference the list had + return v; // and v now owns the reference the list had } else { // list resize failed, need to restore - memmove(&items[index+1], &items[index], (pop_after_size - index)* sizeof(PyObject *)); + memmove(&items[index+1], &items[index], (size_after_pop - index)* sizeof(PyObject *)); items[index] = v; return NULL; } From 7c1a8355d7ca723039d05546d171a2359644ce1c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 26 Dec 2022 21:05:21 +0100 Subject: [PATCH 8/9] make variable const --- Objects/listobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 7d65bb3714cc9b..470e20f01d9d5c 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1025,7 +1025,7 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) PyObject **items = self->ob_item; v = items[index]; - Py_ssize_t size_after_pop = Py_SIZE(self) - 1; + const Py_ssize_t size_after_pop = Py_SIZE(self) - 1; if(size_after_pop == 0) { Py_INCREF(v); status = _list_clear(self); From 23eb3aa79c6e0fe9d0bcf4a969adb975f6d40bae Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 27 Dec 2022 12:29:50 +0900 Subject: [PATCH 9/9] Update Objects/listobject.c --- Objects/listobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 470e20f01d9d5c..b093f88a35fc47 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1026,7 +1026,7 @@ list_pop_impl(PyListObject *self, Py_ssize_t index) PyObject **items = self->ob_item; v = items[index]; const Py_ssize_t size_after_pop = Py_SIZE(self) - 1; - if(size_after_pop == 0) { + if (size_after_pop == 0) { Py_INCREF(v); status = _list_clear(self); }