Skip to content

gh-142659: Optimize set_swap_bodies for intersection_update#148155

Open
Siyet wants to merge 2 commits intopython:mainfrom
Siyet:gh-142659-optimize-set-replace-body
Open

gh-142659: Optimize set_swap_bodies for intersection_update#148155
Siyet wants to merge 2 commits intopython:mainfrom
Siyet:gh-142659-optimize-set-replace-body

Conversation

@Siyet
Copy link
Copy Markdown

@Siyet Siyet commented Apr 6, 2026

Summary

Replace the general-purpose set_swap_bodies() with a specialized set_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 from set_intersection_update() and set_intersection_update_multi_impl(), where the second argument (tmp) is always a freshly created temporary with Py_REFCNT == 1.

The new set_replace_body() exploits this invariant:

  • Skip atomic stores on src: the temporary is not visible to other threads, so plain assignments suffice (saves atomic fence overhead in the free-threaded build).
  • Skip copy_small_table for src: use plain memcpy instead of per-entry atomic stores when writing back to src's smalltable (Py_GIL_DISABLED path).
  • Remove frozenset hash swap: neither argument is ever a frozenset in these call sites (enforced via assert).
  • Simplify shared-marking logic: src is never shared (enforced via assert), so only one direction of the shared-marking check is needed — propagate shared status from dst to src for proper deallocation of old entries.
  • Remove unused Py_hash_t h variable.

All assumptions are guarded by assert() to document and enforce the contract.

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.
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 6, 2026

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 skip news label instead.

@eendebakpt
Copy link
Copy Markdown
Contributor

@Siyet Could you add a few benchmarks for the affected cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants