-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
gh-151722: Defer GC tracking of frozendict to end of construction #151740
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd1b1a4
gh-151722: Defer GC tracking of frozendict to end of construction
corona10 399d810
Address code review
corona10 286eafd
fix
corona10 d74be81
fix
corona10 897bdec
Update
corona10 69a6316
Address code review
corona10 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-00-30-47.gh-issue-151722.RPMPIY.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 :class:`frozendict` to end of 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -138,6 +138,7 @@ As a consequence of this, split keys have a maximum size of 16. | |||||
| // Forward declarations | ||||||
| static PyObject* frozendict_new(PyTypeObject *type, PyObject *args, | ||||||
| PyObject *kwds); | ||||||
| static PyObject* frozendict_new_untracked(PyTypeObject *type); | ||||||
| static PyObject* dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | ||||||
| static int dict_merge(PyObject *a, PyObject *b, int override, PyObject **dupkey); | ||||||
| static int dict_contains(PyObject *op, PyObject *key); | ||||||
|
|
@@ -4147,11 +4148,6 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject ** | |||||
| STORE_USED(mp, other->ma_used); | ||||||
| ASSERT_CONSISTENT(mp); | ||||||
|
|
||||||
| if (_PyObject_GC_IS_TRACKED(other) && !_PyObject_GC_IS_TRACKED(mp)) { | ||||||
| /* Maintain tracking. */ | ||||||
| _PyObject_GC_TRACK(mp); | ||||||
| } | ||||||
|
|
||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -5242,14 +5238,13 @@ static PyNumberMethods dict_as_number = { | |||||
| }; | ||||||
|
|
||||||
| static PyObject * | ||||||
| dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||||
| dict_new_untracked(PyTypeObject *type) | ||||||
| { | ||||||
| assert(type != NULL); | ||||||
| assert(type->tp_alloc != NULL); | ||||||
| // dict subclasses must implement the GC protocol | ||||||
| assert(_PyType_IS_GC(type)); | ||||||
|
|
||||||
| PyObject *self = type->tp_alloc(type, 0); | ||||||
| PyObject *self = _PyType_AllocNoTrack(type, 0); | ||||||
| if (self == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
@@ -5262,9 +5257,19 @@ dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||
| d->ma_keys = Py_EMPTY_KEYS; | ||||||
| d->ma_values = NULL; | ||||||
| ASSERT_CONSISTENT(d); | ||||||
| if (!_PyObject_GC_IS_TRACKED(d)) { | ||||||
| _PyObject_GC_TRACK(d); | ||||||
| return self; | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| dict_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds)) | ||||||
| { | ||||||
| /* tp_new only allocates; args/kwds are consumed by dict_init (tp_init). */ | ||||||
|
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.
Suggested change
|
||||||
| PyObject *self = dict_new_untracked(type); | ||||||
| if (self == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
| assert(!_PyObject_GC_IS_TRACKED(self)); | ||||||
| _PyObject_GC_TRACK(self); | ||||||
| return self; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -5323,7 +5328,9 @@ frozendict_vectorcall(PyObject *type, PyObject * const*args, | |||||
| return Py_NewRef(args[0]); | ||||||
| } | ||||||
|
|
||||||
| PyObject *self = frozendict_new(_PyType_CAST(type), NULL, NULL); | ||||||
| /* Keep the frozendict untracked until it is fully built, so a half-built | ||||||
| object is never reachable from another thread. */ | ||||||
|
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.
Suggested change
You may add a reference to the issue gh-151722. |
||||||
| PyObject *self = frozendict_new_untracked(_PyType_CAST(type)); | ||||||
| if (self == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
@@ -5343,6 +5350,8 @@ frozendict_vectorcall(PyObject *type, PyObject * const*args, | |||||
| } | ||||||
| } | ||||||
| } | ||||||
| assert(!_PyObject_GC_IS_TRACKED(self)); | ||||||
| _PyObject_GC_TRACK(self); | ||||||
| return self; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -8361,17 +8370,27 @@ frozendict_hash(PyObject *op) | |||||
| } | ||||||
|
|
||||||
|
|
||||||
| /* Allocate an empty, GC-untracked frozendict; the constructor tracks it once | ||||||
| fully built. */ | ||||||
| static PyObject * | ||||||
| frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||||
| frozendict_new_untracked(PyTypeObject *type) | ||||||
| { | ||||||
| PyObject *d = dict_new(type, args, kwds); | ||||||
| PyObject *d = dict_new_untracked(type); | ||||||
| if (d == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
| assert(can_modify_dict(_PyAnyDict_CAST(d))); | ||||||
| _PyFrozenDictObject_CAST(d)->ma_hash = -1; | ||||||
| return d; | ||||||
| } | ||||||
|
|
||||||
| PyFrozenDictObject *self = _PyFrozenDictObject_CAST(d); | ||||||
| self->ma_hash = -1; | ||||||
| static PyObject * | ||||||
| frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||||
| { | ||||||
| PyObject *d = frozendict_new_untracked(type); | ||||||
| if (d == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
||||||
| if (args != NULL) { | ||||||
| if (dict_update_common(d, args, kwds, "frozendict") < 0) { | ||||||
|
|
@@ -8383,6 +8402,8 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||
| assert(kwds == NULL); | ||||||
| } | ||||||
|
|
||||||
| assert(!_PyObject_GC_IS_TRACKED(d)); | ||||||
| _PyObject_GC_TRACK(d); | ||||||
| return d; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
#127027
No more lazy track from here, we can remove this logic safely.
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.
This removal makes me a little bit nervous. You may add the following assertion at the top of dict_dict_merge() (after other assertions).