gh-142659: Optimize set_swap_bodies for intersection_update#148155
Open
Siyet wants to merge 2 commits intopython:mainfrom
Open
gh-142659: Optimize set_swap_bodies for intersection_update#148155Siyet wants to merge 2 commits intopython:mainfrom
Siyet wants to merge 2 commits intopython:mainfrom
Conversation
Replace the general-purpose set_swap_bodies() with a specialized set_replace_body() that exploits the invariant that src is always a uniquely-referenced temporary about to be discarded.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Contributor
|
@Siyet Could you add a few benchmarks for the affected cases? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replace the general-purpose
set_swap_bodies()with a specializedset_replace_body()that exploits the invariant that the source argument is always a uniquely-referenced temporary set about to be discarded.Follow-up to the observation in #132290 (comment) and #142659.
Changes
set_swap_bodies()was designed for arbitrary two-way swaps between any two sets, but it is only called fromset_intersection_update()andset_intersection_update_multi_impl(), where the second argument (tmp) is always a freshly created temporary withPy_REFCNT == 1.The new
set_replace_body()exploits this invariant:src: the temporary is not visible to other threads, so plain assignments suffice (saves atomic fence overhead in the free-threaded build).copy_small_tableforsrc: use plainmemcpyinstead of per-entry atomic stores when writing back to src's smalltable (Py_GIL_DISABLEDpath).assert).srcis never shared (enforced viaassert), so only one direction of the shared-marking check is needed — propagate shared status fromdsttosrcfor proper deallocation of old entries.Py_hash_t hvariable.All assumptions are guarded by
assert()to document and enforce the contract.