-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-139103: fix free-threading dataclass.__init__ perf issue
#141596
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 6 commits
fbda041
273d2af
724897b
e915039
65c695f
aaaec25
c02223e
34d4ffa
3846637
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 @@ | ||
| Make ``type_setattro`` use defer refcount in free-threading for functions without defer refcount. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6546,6 +6546,18 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value) | |||||||||||||
| assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_INLINE_VALUES)); | ||||||||||||||
| assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_MANAGED_DICT)); | ||||||||||||||
|
|
||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||
| if (value != NULL && PyFunction_Check(value)) { | ||||||||||||||
| if (!_PyObject_HasDeferredRefcount(value)) { | ||||||||||||||
| BEGIN_TYPE_LOCK(); | ||||||||||||||
|
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. We don't need to hold the type lock here, since
Contributor
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. Hi @ZeroIntensity , Thanks very much for pointing this out. ❤ I go through the code of Lines 2734 to 2739 in 3d14805
It uses compare-and-set to guard the I removed the Best Regards, |
||||||||||||||
| if (!_PyObject_HasDeferredRefcount(value)) { | ||||||||||||||
|
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. Why is this checked twice?
Contributor
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. Hi @ZeroIntensity , I tried to double-check the flag to reduce the conflict of entering the lock. An atomic action on the type flag is very efficient and is safe for our scenario. Thanks very much for your suggestion! Best Regards, |
||||||||||||||
| PyUnstable_Object_EnableDeferredRefcount(value); | ||||||||||||||
| } | ||||||||||||||
| END_TYPE_LOCK(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| PyObject *old_value = NULL; | ||||||||||||||
| PyObject *descr = _PyType_LookupRef(metatype, name); | ||||||||||||||
| if (descr != NULL) { | ||||||||||||||
|
|
@@ -6573,6 +6585,7 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value) | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
LindaSummer marked this conversation as resolved.
Outdated
|
||||||||||||||
| BEGIN_TYPE_DICT_LOCK(dict); | ||||||||||||||
| res = type_update_dict(type, (PyDictObject *)dict, name, value, &old_value); | ||||||||||||||
| assert(_PyType_CheckConsistency(type)); | ||||||||||||||
|
|
||||||||||||||
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 is too technical for a news entry. Instead, can we say something like "Improve multithreaded scaling of dataclasses on the free-threaded build"?
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.
Hi @ZeroIntensity ,
Thanks very much for your suggestion!
I will update the news entry as suggested.
Best Regards,
Edward