Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-139951: Do not track immutable tuples in GC#140204
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
efimov-mikhail wants to merge20 commits intopython:mainfromefimov-mikhail:sergey-miryanov-139389-donot-track-immutable-tuples
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
20 commits Select commitHold shift + click to select a range
fbb7342 Do not track immutable tuples in PyTuple_Pack
sergey-miryanovbe72b3f Extra stats
sergey-miryanov04f0f66 Implement for all
sergey-miryanov3b80c93 Merge branch 'main' into 139389-donot-track-immutable-tuples
sergey-miryanovff81eb2 Count total tuples
sergey-miryanovf1220e4 expirements
efimov-mikhail8b7fcd5 some clean
efimov-mikhailb281fe8 Remove statistics
efimov-mikhail5c117a7 Update PyTuple_FromArray
efimov-mikhail0d7eff1 Helper function is added
efimov-mikhaila679b50 Update Objects/tupleobject.c
efimov-mikhail35dfe24 Update Objects/tupleobject.c
efimov-mikhail4297f8b Review addressed
efimov-mikhailddfe151 News updated
efimov-mikhail30dd590 expirements
efimov-mikhail63c0e3e Revert GC changes
efimov-mikhail9ce41be Check tuple tracking after tp_subscipt
efimov-mikhail77a0373 Review addressed
efimov-mikhail567105f Address feedback
efimov-mikhaild00b16a Merge branch 'main' into sergey-miryanov-139389-donot-track-immutable…
efimov-mikhailFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletionsLib/test/test_capi/test_tuple.py
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
26 changes: 23 additions & 3 deletionsLib/test/test_tuple.py
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 deletionsMisc/NEWS.d/next/Core_and_Builtins/2025-10-16-15-02-11.gh-issue-139951.tqbIsW.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 @@ | ||
| Do not track immutable tuples in GC. | ||
| Patch by Sergey Miryanov and Mikhail Efimov |
60 changes: 45 additions & 15 deletionsObjects/tupleobject.c
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 |
|---|---|---|
| @@ -145,17 +145,33 @@ _PyTuple_MaybeUntrack(PyObject *op) | ||
| t = (PyTupleObject *) op; | ||
| n = Py_SIZE(t); | ||
| for (i = 0; i < n; i++) { | ||
| PyObject *item =t->ob_item[i]; | ||
| /* Tuple with NULL elements aren't | ||
| fully constructed, don't untrack | ||
| them yet. */ | ||
| if (!item || | ||
| _PyObject_GC_MAY_BE_TRACKED(item)) | ||
| return; | ||
| } | ||
| _PyObject_GC_UNTRACK(op); | ||
| } | ||
| /* gh-139951: This function helps to avoid performance issue in the GC, | ||
| * we don't track immutable tuples. */ | ||
| static bool | ||
efimov-mikhail marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| tuple_need_tracking(PyTupleObject *self) | ||
| { | ||
| Py_ssize_t n = PyTuple_GET_SIZE(self); | ||
| for (Py_ssize_t i = 0; i < n; i++) { | ||
| PyObject *item = self->ob_item[i]; | ||
| assert(item); | ||
| if (_PyObject_GC_MAY_BE_TRACKED(item)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| PyObject * | ||
| PyTuple_Pack(Py_ssize_t n, ...) | ||
| { | ||
| @@ -169,19 +185,21 @@ PyTuple_Pack(Py_ssize_t n, ...) | ||
| } | ||
| va_start(vargs, n); | ||
| PyTupleObject *tuple = tuple_alloc(n); | ||
| if (tuple == NULL) { | ||
| va_end(vargs); | ||
| return NULL; | ||
| } | ||
| items =tuple->ob_item; | ||
| for (i = 0; i < n; i++) { | ||
| o = va_arg(vargs, PyObject *); | ||
| items[i] = Py_NewRef(o); | ||
| } | ||
| va_end(vargs); | ||
| if (tuple_need_tracking(tuple)) { | ||
| _PyObject_GC_TRACK(tuple); | ||
| } | ||
| return (PyObject *)tuple; | ||
| } | ||
| @@ -381,7 +399,9 @@ PyTuple_FromArray(PyObject *const *src, Py_ssize_t n) | ||
| PyObject *item = src[i]; | ||
| dst[i] = Py_NewRef(item); | ||
| } | ||
| if (tuple_need_tracking(tuple)) { | ||
| _PyObject_GC_TRACK(tuple); | ||
| } | ||
| return (PyObject *)tuple; | ||
| } | ||
| @@ -399,7 +419,9 @@ _PyTuple_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n) | ||
| for (Py_ssize_t i = 0; i < n; i++) { | ||
| dst[i] = PyStackRef_AsPyObjectSteal(src[i]); | ||
| } | ||
| if (tuple_need_tracking(tuple)) { | ||
| _PyObject_GC_TRACK(tuple); | ||
| } | ||
| return (PyObject *)tuple; | ||
| } | ||
| @@ -421,7 +443,9 @@ _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n) | ||
| PyObject *item = src[i]; | ||
| dst[i] = item; | ||
| } | ||
| if (tuple_need_tracking(tuple)) { | ||
| _PyObject_GC_TRACK(tuple); | ||
| } | ||
| return (PyObject *)tuple; | ||
| } | ||
| @@ -494,7 +518,9 @@ tuple_concat(PyObject *aa, PyObject *bb) | ||
| dest[i] = Py_NewRef(v); | ||
| } | ||
| if (_PyObject_GC_IS_TRACKED(a) || _PyObject_GC_IS_TRACKED(b)) { | ||
| _PyObject_GC_TRACK(np); | ||
| } | ||
| return (PyObject *)np; | ||
| } | ||
| @@ -543,8 +569,10 @@ tuple_repeat(PyObject *self, Py_ssize_t n) | ||
| _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, | ||
| sizeof(PyObject *)*input_size); | ||
| } | ||
| if (_PyObject_GC_IS_TRACKED(a)) { | ||
| _PyObject_GC_TRACK(np); | ||
| } | ||
| return (PyObject *)np; | ||
| } | ||
| /*[clinic input] | ||
| @@ -821,7 +849,9 @@ tuple_subscript(PyObject *op, PyObject* item) | ||
| dest[i] = it; | ||
| } | ||
| if (tuple_need_tracking(result)) { | ||
| _PyObject_GC_TRACK(result); | ||
| } | ||
| return (PyObject *)result; | ||
| } | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.