-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
[WIP] gh-151722: Defer GC tracking of frozendict.fromkeys() as possible #152021
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
Closed
+26
−10
Closed
Changes from all commits
Commits
Show all changes
4 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-06-24-02-15-21.gh-issue-151722.LY1eI2.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 a :class:`frozendict` created by | ||
| :meth:`!frozendict.fromkeys` until the end of construction as possible. |
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 |
|---|---|---|
|
|
@@ -140,6 +140,7 @@ 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 PyObject* dict_new_untracked(PyTypeObject *type); | ||
| static int dict_merge(PyObject *a, PyObject *b, int override, PyObject **dupkey); | ||
| static int dict_contains(PyObject *op, PyObject *key); | ||
| static int dict_merge_from_seq2(PyObject *d, PyObject *seq2, int override); | ||
|
|
@@ -3419,22 +3420,22 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value) | |
| PyObject *d; | ||
| int status; | ||
|
|
||
| PyTypeObject *cls_type = _PyType_CAST(cls); | ||
| d = _PyObject_CallNoArgs(cls); | ||
| if (d == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| // If cls is a dict or frozendict subclass with overridden constructor, | ||
| // copy the frozendict. | ||
| PyTypeObject *cls_type = _PyType_CAST(cls); | ||
| if (PyFrozenDict_Check(d) && cls_type->tp_new != frozendict_new) { | ||
| // Subclass-friendly copy | ||
| PyObject *copy; | ||
| if (PyObject_IsSubclass(cls, (PyObject*)&PyFrozenDict_Type)) { | ||
| copy = frozendict_new(cls_type, NULL, NULL); | ||
| copy = frozendict_new_untracked(cls_type); | ||
| } | ||
| else { | ||
| copy = dict_new(cls_type, NULL, NULL); | ||
| copy = dict_new_untracked(cls_type); | ||
| } | ||
| if (copy == NULL) { | ||
| Py_DECREF(d); | ||
|
|
@@ -3449,30 +3450,35 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value) | |
| } | ||
| assert(!PyFrozenDict_Check(d) || can_modify_dict((PyDictObject*)d)); | ||
|
|
||
| // Keep d untracked while it is filled; GC-track it once complete (done:). | ||
| if (_PyObject_GC_IS_TRACKED(d)) { | ||
| _PyObject_GC_UNTRACK(d); | ||
| } | ||
|
|
||
| if (PyDict_CheckExact(d)) { | ||
| if (PyDict_CheckExact(iterable)) { | ||
| PyDictObject *mp = (PyDictObject *)d; | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION2(d, iterable); | ||
| d = (PyObject *)dict_dict_fromkeys(mp, iterable, value); | ||
| Py_END_CRITICAL_SECTION2(); | ||
| return d; | ||
| goto done; | ||
| } | ||
| else if (PyFrozenDict_CheckExact(iterable)) { | ||
| PyDictObject *mp = (PyDictObject *)d; | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION(d); | ||
| d = (PyObject *)dict_dict_fromkeys(mp, iterable, value); | ||
| Py_END_CRITICAL_SECTION(); | ||
| return d; | ||
| goto done; | ||
| } | ||
| else if (PyAnySet_CheckExact(iterable)) { | ||
| PyDictObject *mp = (PyDictObject *)d; | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION2(d, iterable); | ||
| d = (PyObject *)dict_set_fromkeys(mp, iterable, value); | ||
| Py_END_CRITICAL_SECTION2(); | ||
| return d; | ||
| goto done; | ||
| } | ||
| } | ||
| else if (PyFrozenDict_CheckExact(d)) { | ||
|
|
@@ -3482,20 +3488,20 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value) | |
| Py_BEGIN_CRITICAL_SECTION(iterable); | ||
| d = (PyObject *)dict_dict_fromkeys(mp, iterable, value); | ||
| Py_END_CRITICAL_SECTION(); | ||
| return d; | ||
| goto done; | ||
| } | ||
| else if (PyFrozenDict_CheckExact(iterable)) { | ||
| PyDictObject *mp = (PyDictObject *)d; | ||
| d = (PyObject *)dict_dict_fromkeys(mp, iterable, value); | ||
| return d; | ||
| goto done; | ||
| } | ||
| else if (PyAnySet_CheckExact(iterable)) { | ||
| PyDictObject *mp = (PyDictObject *)d; | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION(iterable); | ||
| d = (PyObject *)dict_set_fromkeys(mp, iterable, value); | ||
| Py_END_CRITICAL_SECTION(); | ||
| return d; | ||
| goto done; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -3541,12 +3547,20 @@ dict_iter_exit:; | |
| if (PyErr_Occurred()) | ||
| goto Fail; | ||
| Py_DECREF(it); | ||
| return d; | ||
| goto done; | ||
|
|
||
| Fail: | ||
| Py_DECREF(it); | ||
| Py_DECREF(d); | ||
| return NULL; | ||
|
|
||
| done: | ||
| // Built untracked above; GC-track now that it is complete. | ||
| if (d != NULL) { | ||
| assert(!_PyObject_GC_IS_TRACKED(d)); | ||
|
Member
Author
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. @tonghuaroot
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. Agreed, the assert covers it. Thanks for taking this over. |
||
| _PyObject_GC_TRACK(d); | ||
| } | ||
| return d; | ||
| } | ||
|
|
||
| /* Methods */ | ||
|
|
||
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.
If there is a good way to obtain an untracked object here, that would be great, but I think that is a separate topic.
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.
cc @vstinner
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.
Is these cases considered?
__init__instead of__new__, passes itsselfto another thread__init__is being executed