-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-91247: improve performance of list and tuple repeat (with specialization for n=1) #91482
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
Merged
sweeneyde
merged 30 commits into
python:main
from
eendebakpt:performance/list_repeat_v2_specialized
Jul 26, 2022
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
48d758c
use memcpy in list repat and list inplace repeat
eendebakpt e32f554
use memcpy in tuple repeat
eendebakpt 3408397
fix debug build
eendebakpt 2ddedf9
remove double ref counting
eendebakpt 74e7cc0
📜🤖 Added by blurb_it.
blurb-it[bot] 493bb9d
make implementations of list repeat and tuple repeat similar
eendebakpt 4f292a4
eliminate duplicated code
eendebakpt 5445cfb
fix ci
eendebakpt 3d287c7
remove Py_INCREF_n from public api
eendebakpt 640643e
take multiplication out of the loop
eendebakpt 7b5e5b2
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt 66cc8ae
fix formatting of news item
eendebakpt 30baa6f
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt 820add4
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt 5750f01
address review comments
eendebakpt 798e732
revert check on input_size==0
eendebakpt ae0c64e
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt 70c7fce
address review comment to rename Py_RefcntAdd to _PyRefcntAdd
eendebakpt 18ebad8
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt be45ed9
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt e9065f6
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt d47f6d7
Apply suggestions from code review
eendebakpt 4584415
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt f6e6bdf
Update Misc/NEWS.d/next/Core and Builtins/2022-03-22-13-12-27.bpo-470…
eendebakpt a8c32ac
Apply suggestions from code review
eendebakpt 8f152f2
Merge branch 'main' into performance/list_repeat_v2_specialized
eendebakpt 4211b00
Apply suggestions from code review
eendebakpt 7056599
pep7
eendebakpt 53fa880
move assert to _Py_memory_repeat
eendebakpt 516f091
Merge branch 'main' into performance/list_repeat_v2_specialized
sweeneyde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2022-03-22-13-12-27.bpo-47091.tJcy-P.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Improve performance of repetition of :class:`list` and :class:`tuple` by using ``memcpy`` to copy data and performing the reference increments in one step. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -551,47 +551,41 @@ list_concat(PyListObject *a, PyObject *bb) | |
| static PyObject * | ||
| list_repeat(PyListObject *a, Py_ssize_t n) | ||
| { | ||
| Py_ssize_t size; | ||
| PyListObject *np; | ||
| if (n < 0) | ||
| n = 0; | ||
| if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) | ||
| return PyErr_NoMemory(); | ||
| size = Py_SIZE(a) * n; | ||
| if (size == 0) | ||
| const Py_ssize_t input_size = Py_SIZE(a); | ||
| if (input_size == 0 || n <= 0) | ||
| return PyList_New(0); | ||
| np = (PyListObject *) list_new_prealloc(size); | ||
| assert(n > 0); | ||
|
|
||
| if (input_size > PY_SSIZE_T_MAX / n) | ||
| return PyErr_NoMemory(); | ||
| Py_ssize_t output_size = input_size * n; | ||
|
|
||
| PyListObject *np = (PyListObject *) list_new_prealloc(output_size); | ||
| if (np == NULL) | ||
| return NULL; | ||
|
|
||
| PyObject **dest = np->ob_item; | ||
| PyObject **dest_end = dest + size; | ||
| if (Py_SIZE(a) == 1) { | ||
| if (input_size == 1) { | ||
| PyObject *elem = a->ob_item[0]; | ||
| Py_SET_REFCNT(elem, Py_REFCNT(elem) + n); | ||
| #ifdef Py_REF_DEBUG | ||
| _Py_RefTotal += n; | ||
| #endif | ||
| _Py_RefcntAdd(elem, n); | ||
| PyObject **dest_end = dest + output_size; | ||
| while (dest < dest_end) { | ||
| *dest++ = elem; | ||
| } | ||
| } | ||
| else { | ||
| PyObject **src = a->ob_item; | ||
| PyObject **src_end = src + Py_SIZE(a); | ||
| PyObject **src_end = src + input_size; | ||
| while (src < src_end) { | ||
| Py_SET_REFCNT(*src, Py_REFCNT(*src) + n); | ||
| #ifdef Py_REF_DEBUG | ||
| _Py_RefTotal += n; | ||
| #endif | ||
| *dest++ = *src++; | ||
| } | ||
| // Now src chases after dest in the same buffer | ||
| src = np->ob_item; | ||
| while (dest < dest_end) { | ||
| _Py_RefcntAdd(*src, n); | ||
| *dest++ = *src++; | ||
| } | ||
|
|
||
| _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: this cannot overflow because |
||
| sizeof(PyObject *)*input_size); | ||
| } | ||
| Py_SET_SIZE(np, size); | ||
|
|
||
| Py_SET_SIZE(np, output_size); | ||
| return (PyObject *) np; | ||
| } | ||
|
|
||
|
|
@@ -743,12 +737,8 @@ PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | |
| static PyObject * | ||
| list_inplace_repeat(PyListObject *self, Py_ssize_t n) | ||
| { | ||
| PyObject **items; | ||
| Py_ssize_t size, i, j, p; | ||
|
|
||
|
|
||
| size = PyList_GET_SIZE(self); | ||
| if (size == 0 || n == 1) { | ||
| Py_ssize_t input_size = PyList_GET_SIZE(self); | ||
| if (input_size == 0 || n == 1) { | ||
| Py_INCREF(self); | ||
| return (PyObject *)self; | ||
| } | ||
|
|
@@ -759,22 +749,21 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n) | |
| return (PyObject *)self; | ||
| } | ||
|
|
||
| if (size > PY_SSIZE_T_MAX / n) { | ||
| if (input_size > PY_SSIZE_T_MAX / n) { | ||
| return PyErr_NoMemory(); | ||
| } | ||
| Py_ssize_t output_size = input_size * n; | ||
|
|
||
| if (list_resize(self, size*n) < 0) | ||
| if (list_resize(self, output_size) < 0) | ||
| return NULL; | ||
|
|
||
| p = size; | ||
| items = self->ob_item; | ||
| for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */ | ||
| for (j = 0; j < size; j++) { | ||
| PyObject *o = items[j]; | ||
| Py_INCREF(o); | ||
| items[p++] = o; | ||
| } | ||
| PyObject **items = self->ob_item; | ||
| for (Py_ssize_t j = 0; j < input_size; j++) { | ||
| _Py_RefcntAdd(items[j], n-1); | ||
| } | ||
| _Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size, | ||
| sizeof(PyObject *)*input_size); | ||
|
|
||
| Py_INCREF(self); | ||
| return (PyObject *)self; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.