-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-153083: Defer GC tracking of an array to the end of construction. #153284
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
Open
corona10
wants to merge
2
commits into
python:main
Choose a base branch
from
corona10:gh-153083
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+34
−11
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2026-07-08-01-47-15.gh-issue-153083.XbKZCp.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,2 @@ | ||
| Defer GC tracking of an :class:`array.array` to the end of its construction. | ||
| Patch by Donghee Na. |
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| #include "pycore_floatobject.h" // _PY_FLOAT_BIG_ENDIAN | ||
| #include "pycore_modsupport.h" // _PyArg_NoKeywords() | ||
| #include "pycore_moduleobject.h" // _PyModule_GetState() | ||
| #include "pycore_object.h" // _PyObject_GC_TRACK() | ||
| #include "pycore_tuple.h" // _PyTuple_FromPairSteal | ||
| #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() | ||
|
|
||
|
|
@@ -752,7 +753,8 @@ class array.array "arrayobject *" "ArrayType" | |
| /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a5c29edf59f176a3]*/ | ||
|
|
||
| static PyObject * | ||
| newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr) | ||
| newarrayobject_untracked(PyTypeObject *type, Py_ssize_t size, | ||
| const struct arraydescr *descr) | ||
| { | ||
| arrayobject *op; | ||
| size_t nbytes; | ||
|
|
@@ -767,7 +769,7 @@ newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *des | |
| return PyErr_NoMemory(); | ||
| } | ||
| nbytes = size * descr->itemsize; | ||
| op = (arrayobject *) type->tp_alloc(type, 0); | ||
| op = (arrayobject *) _PyType_AllocNoTrack(type, 0); | ||
|
Contributor
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. Should we update |
||
| if (op == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
@@ -789,6 +791,16 @@ newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *des | |
| return (PyObject *) op; | ||
| } | ||
|
|
||
| static PyObject * | ||
| newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr) | ||
| { | ||
| PyObject *op = newarrayobject_untracked(type, size, descr); | ||
| if (op != NULL) { | ||
| _PyObject_GC_TRACK(op); | ||
| } | ||
| return op; | ||
| } | ||
|
|
||
| static PyObject * | ||
| getarrayitem(PyObject *op, Py_ssize_t i) | ||
| { | ||
|
|
@@ -990,13 +1002,14 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | |
| ihigh = ilow; | ||
| else if (ihigh > Py_SIZE(a)) | ||
| ihigh = Py_SIZE(a); | ||
| np = (arrayobject *) newarrayobject(state->ArrayType, ihigh - ilow, a->ob_descr); | ||
| np = (arrayobject *) newarrayobject_untracked(state->ArrayType, ihigh - ilow, a->ob_descr); | ||
| if (np == NULL) | ||
| return NULL; | ||
| if (ihigh > ilow) { | ||
| memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, | ||
| (ihigh-ilow) * a->ob_descr->itemsize); | ||
| } | ||
| _PyObject_GC_TRACK(np); | ||
| return (PyObject *)np; | ||
| } | ||
|
|
||
|
|
@@ -1067,7 +1080,7 @@ array_concat(PyObject *op, PyObject *bb) | |
| return PyErr_NoMemory(); | ||
| } | ||
| size = Py_SIZE(a) + Py_SIZE(b); | ||
| np = (arrayobject *) newarrayobject(state->ArrayType, size, a->ob_descr); | ||
| np = (arrayobject *) newarrayobject_untracked(state->ArrayType, size, a->ob_descr); | ||
| if (np == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
@@ -1078,6 +1091,7 @@ array_concat(PyObject *op, PyObject *bb) | |
| memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize, | ||
| b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); | ||
| } | ||
| _PyObject_GC_TRACK(np); | ||
| return (PyObject *)np; | ||
| #undef b | ||
| } | ||
|
|
@@ -1095,16 +1109,19 @@ array_repeat(PyObject *op, Py_ssize_t n) | |
| return PyErr_NoMemory(); | ||
| } | ||
| Py_ssize_t size = array_length * n; | ||
| arrayobject* np = (arrayobject *) newarrayobject(state->ArrayType, size, a->ob_descr); | ||
| arrayobject* np = (arrayobject *) newarrayobject_untracked(state->ArrayType, size, a->ob_descr); | ||
| if (np == NULL) | ||
| return NULL; | ||
| if (size == 0) | ||
| if (size == 0) { | ||
| _PyObject_GC_TRACK(np); | ||
| return (PyObject *)np; | ||
| } | ||
|
|
||
| const Py_ssize_t oldbytes = array_length * a->ob_descr->itemsize; | ||
| const Py_ssize_t newbytes = oldbytes * n; | ||
| _PyBytes_RepeatBuffer(np->ob_item, newbytes, a->ob_item, oldbytes); | ||
|
|
||
| _PyObject_GC_TRACK(np); | ||
| return (PyObject *)np; | ||
| } | ||
|
|
||
|
|
@@ -2666,17 +2683,17 @@ array_subscr(PyObject *op, PyObject *item) | |
| return newarrayobject(state->ArrayType, 0, self->ob_descr); | ||
| } | ||
| else if (step == 1) { | ||
| PyObject *result = newarrayobject(state->ArrayType, | ||
| slicelength, self->ob_descr); | ||
| PyObject *result = newarrayobject_untracked(state->ArrayType, slicelength, self->ob_descr); | ||
| if (result == NULL) | ||
| return NULL; | ||
| memcpy(((arrayobject *)result)->ob_item, | ||
| self->ob_item + start * itemsize, | ||
| slicelength * itemsize); | ||
| _PyObject_GC_TRACK(result); | ||
| return result; | ||
| } | ||
| else { | ||
| result = newarrayobject(state->ArrayType, slicelength, self->ob_descr); | ||
| result = newarrayobject_untracked(state->ArrayType, slicelength, self->ob_descr); | ||
| if (!result) return NULL; | ||
|
|
||
| ar = (arrayobject*)result; | ||
|
|
@@ -2688,6 +2705,7 @@ array_subscr(PyObject *op, PyObject *item) | |
| itemsize); | ||
| } | ||
|
|
||
| _PyObject_GC_TRACK(result); | ||
| return result; | ||
| } | ||
| } | ||
|
|
@@ -2973,7 +2991,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| else | ||
| len = 0; | ||
|
|
||
| a = newarrayobject(type, len, descr); | ||
| a = newarrayobject_untracked(type, len, descr); | ||
| if (a == NULL) { | ||
| Py_XDECREF(it); | ||
| return NULL; | ||
|
|
@@ -3039,6 +3057,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| } | ||
| Py_DECREF(it); | ||
| } | ||
| // Track only once fully built. | ||
| _PyObject_GC_TRACK(a); | ||
| return a; | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.