-
-
Notifications
You must be signed in to change notification settings - Fork 35k
[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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| 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,26 @@ _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; | ||
| } | ||
| // The constructor returns a tracked object; keep it untracked while it is | ||
| // filled and GC-track it once complete (done:), so a half-built frozendict | ||
| // is never observable. | ||
| _PyObject_GC_UNTRACK(d); | ||
|
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. Thanks for driving this. I believe the
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. tl; dr I wrote PR gh-152067 which is a similar fix, but with a safer design. I don't think that calling Moreover, we cannot use IMO we should only be worried about the dictionary being not tracked by the GC if the dictionary is a frozendict. And it's a frozendict, we can copy it using |
||
|
|
||
| // 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); | ||
|
|
@@ -3456,23 +3461,23 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value) | |
| 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 +3487,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 +3546,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 */ | ||
|
|
||
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