-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-142659: Optimize set_swap_bodies for intersection_update #148155
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
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,3 @@ | ||
| Optimize :meth:`set.intersection_update` by replacing the general-purpose | ||
| ``set_swap_bodies()`` with a specialized ``set_replace_body()`` that skips | ||
| unnecessary atomic operations and checks for the temporary set argument. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1475,74 +1475,68 @@ copy_small_table(setentry *dest, setentry *src) | |||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| /* set_swap_bodies() switches the contents of any two sets by moving their | ||||||
| internal data pointers and, if needed, copying the internal smalltables. | ||||||
| Semantically equivalent to: | ||||||
| /* set_replace_body() replaces the contents of dst with those of src, | ||||||
| moving dst's old contents into src for proper cleanup on Py_DECREF. | ||||||
|
|
||||||
| t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t | ||||||
| The caller guarantees that src is a uniquely-referenced temporary set | ||||||
| that will be discarded immediately afterward. This allows us to skip | ||||||
| atomic operations and shared-marking on src's fields, and to skip the | ||||||
| frozenset hash swap (neither argument is ever a frozenset here). | ||||||
|
|
||||||
| The function always succeeds and it leaves both objects in a stable state. | ||||||
| Useful for operations that update in-place (by allowing an intermediate | ||||||
| result to be swapped into one of the original inputs). | ||||||
| */ | ||||||
|
|
||||||
| static void | ||||||
| set_swap_bodies(PySetObject *a, PySetObject *b) | ||||||
| set_replace_body(PySetObject *dst, PySetObject *src) | ||||||
| { | ||||||
| Py_ssize_t t; | ||||||
| setentry *u; | ||||||
| setentry tab[PySet_MINSIZE]; | ||||||
| Py_hash_t h; | ||||||
|
|
||||||
| setentry *a_table = a->table; | ||||||
| setentry *b_table = b->table; | ||||||
| FT_ATOMIC_STORE_PTR_RELEASE(a->table, NULL); | ||||||
| FT_ATOMIC_STORE_PTR_RELEASE(b->table, NULL); | ||||||
|
|
||||||
| t = a->fill; a->fill = b->fill; b->fill = t; | ||||||
| t = a->used; | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(a->used, b->used); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(b->used, t); | ||||||
| t = a->mask; | ||||||
| FT_ATOMIC_STORE_SSIZE_RELEASE(a->mask, b->mask); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELEASE(b->mask, t); | ||||||
|
|
||||||
| u = a_table; | ||||||
| if (a_table == a->smalltable) | ||||||
| u = b->smalltable; | ||||||
| a_table = b_table; | ||||||
| if (b_table == b->smalltable) | ||||||
| a_table = a->smalltable; | ||||||
| b_table = u; | ||||||
|
|
||||||
| if (a_table == a->smalltable || b_table == b->smalltable) { | ||||||
| memcpy(tab, a->smalltable, sizeof(tab)); | ||||||
|
|
||||||
| assert(!PyType_IsSubtype(Py_TYPE(dst), &PyFrozenSet_Type)); | ||||||
| assert(!PyType_IsSubtype(Py_TYPE(src), &PyFrozenSet_Type)); | ||||||
| assert(Py_REFCNT(src) == 1); | ||||||
|
||||||
| assert(Py_REFCNT(src) == 1); | |
| assert(_PyObject_IsUniquelyReferenced(src)); |
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 assertion is going to be prone to gc.get_objects()/gc.get_referrers() issues like in #148180.
I'm not sure if we should do anything about that.
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.
Will gc.get_objects and similar ever see this set? They use a stop-the-world heap traversal, which can't happen until set_intersection_update / set_intersection_update_multi_impl returns, and by the time that happens, this src set is already gone.
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.
I agree with @zhuyifei1999 - src is a local variable that gets freed before the function returns, so gc can't observe it.
Switched to _PyObject_IsUniquelyReferenced per @eendebakpt's suggestion regardless, since I believe it's the correct API for free-threaded builds.
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 the called is required to cleanup
srcimmediately afterwards, maybe it is cleaner to let theset_replace_bodysteal the reference and do the decref.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.
In
set_intersection_update_multi_impl, I placedPy_DECREF(tmp)afterPy_END_CRITICAL_SECTION().My reasoning was that moving the decref inside set_replace_body would run the deallocation (and decref of all keys) while still holding the critical section lock, which I thought might be undesirable.
By keeping it outside, the caller has control over when deallocation happens.
This is my first PR though and I'm still getting familiar with the project, so if I'm wrong here - happy to rework it.