-
-
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
Open
Siyet
wants to merge
2
commits into
python:main
Choose a base branch
from
Siyet:gh-142659-optimize-set-replace-body
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−54
Open
Changes from all commits
Commits
Show all changes
2 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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core_and_Builtins/2026-04-06-11-32-52.gh-issue-142659.44riUp.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,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. |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||
|
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.
Suggested change
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. This assertion is going to be prone to I'm not sure if we should do anything about that. |
||||||
|
|
||||||
| setentry *dst_table = dst->table; | ||||||
| setentry *src_table = src->table; | ||||||
| FT_ATOMIC_STORE_PTR_RELEASE(dst->table, NULL); | ||||||
| src->table = NULL; | ||||||
|
|
||||||
| t = dst->fill; dst->fill = src->fill; src->fill = t; | ||||||
| t = dst->used; | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(dst->used, src->used); | ||||||
| src->used = t; | ||||||
| t = dst->mask; | ||||||
| FT_ATOMIC_STORE_SSIZE_RELEASE(dst->mask, src->mask); | ||||||
| src->mask = t; | ||||||
|
|
||||||
| u = dst_table; | ||||||
| if (dst_table == dst->smalltable) | ||||||
| u = src->smalltable; | ||||||
| dst_table = src_table; | ||||||
| if (src_table == src->smalltable) | ||||||
| dst_table = dst->smalltable; | ||||||
| src_table = u; | ||||||
|
|
||||||
| if (dst_table == dst->smalltable || src_table == src->smalltable) { | ||||||
| memcpy(tab, dst->smalltable, sizeof(tab)); | ||||||
| #ifndef Py_GIL_DISABLED | ||||||
| memcpy(a->smalltable, b->smalltable, sizeof(tab)); | ||||||
| memcpy(b->smalltable, tab, sizeof(tab)); | ||||||
| memcpy(dst->smalltable, src->smalltable, sizeof(tab)); | ||||||
| memcpy(src->smalltable, tab, sizeof(tab)); | ||||||
| #else | ||||||
| copy_small_table(a->smalltable, b->smalltable); | ||||||
| copy_small_table(b->smalltable, tab); | ||||||
| copy_small_table(dst->smalltable, src->smalltable); | ||||||
| memcpy(src->smalltable, tab, sizeof(tab)); | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) && | ||||||
| PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) { | ||||||
| h = FT_ATOMIC_LOAD_SSIZE_RELAXED(a->hash); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, FT_ATOMIC_LOAD_SSIZE_RELAXED(b->hash)); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, h); | ||||||
| } else { | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, -1); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, -1); | ||||||
| } | ||||||
| if (!SET_IS_SHARED(b) && SET_IS_SHARED(a)) { | ||||||
| SET_MARK_SHARED(b); | ||||||
| } | ||||||
| if (!SET_IS_SHARED(a) && SET_IS_SHARED(b)) { | ||||||
| SET_MARK_SHARED(a); | ||||||
| FT_ATOMIC_STORE_SSIZE_RELAXED(dst->hash, -1); | ||||||
|
|
||||||
| if (SET_IS_SHARED(dst)) { | ||||||
| SET_MARK_SHARED(src); | ||||||
| } | ||||||
| FT_ATOMIC_STORE_PTR_RELEASE(a->table, a_table); | ||||||
| FT_ATOMIC_STORE_PTR_RELEASE(b->table, b_table); | ||||||
|
|
||||||
| FT_ATOMIC_STORE_PTR_RELEASE(dst->table, dst_table); | ||||||
| src->table = src_table; | ||||||
| } | ||||||
|
|
||||||
| /*[clinic input] | ||||||
|
|
@@ -1797,7 +1791,7 @@ set_intersection_update(PySetObject *so, PyObject *other) | |||||
| tmp = set_intersection(so, other); | ||||||
| if (tmp == NULL) | ||||||
| return NULL; | ||||||
| set_swap_bodies(so, (PySetObject *)tmp); | ||||||
| set_replace_body(so, (PySetObject *)tmp); | ||||||
| Py_DECREF(tmp); | ||||||
| Py_RETURN_NONE; | ||||||
| } | ||||||
|
|
@@ -1821,7 +1815,7 @@ set_intersection_update_multi_impl(PySetObject *so, PyObject * const *others, | |||||
| if (tmp == NULL) | ||||||
| return NULL; | ||||||
| Py_BEGIN_CRITICAL_SECTION(so); | ||||||
| set_swap_bodies(so, (PySetObject *)tmp); | ||||||
| set_replace_body(so, (PySetObject *)tmp); | ||||||
| Py_END_CRITICAL_SECTION(); | ||||||
| Py_DECREF(tmp); | ||||||
| Py_RETURN_NONE; | ||||||
|
|
||||||
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 the called is required to cleanup
srcimmediately afterwards, maybe it is cleaner to let theset_replace_bodysteal the reference and do the decref.