epoch: Skip try_advance in collect() when the queue is empty#1299
Open
raphaelroshan wants to merge 1 commit into
Open
epoch: Skip try_advance in collect() when the queue is empty#1299raphaelroshan wants to merge 1 commit into
raphaelroshan wants to merge 1 commit into
Conversation
collect() runs on the pin() hot path every PINNINGS_BETWEEN_COLLECT pins and unconditionally calls try_advance(), which issues a SeqCst fence and traverses the intrusive list of every registered participant. Advancing the epoch is only useful to make queued bags expirable, so when the global garbage queue is empty the whole traversal is pure overhead -- the common case in read-mostly workloads where defer_destroy is rare (see crossbeam-rs#1001, crossbeam-rs#852). Short-circuit collect() when the queue is observed empty. This only delays epoch advancement, never advances it early, so reclamation stays sound: any thread that pushed a bag advances the epoch on a later collect(). Adds a Queue::is_empty(guard) helper mirroring the existing test-only one.
raphaelroshan
marked this pull request as ready for review
July 15, 2026 06:37
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.
Addresses #1001 (and the overhead reported in #852).
Problem
Global::collect()runs on thepin()hot path (everyPINNINGS_BETWEEN_COLLECTpins) and unconditionally callstry_advance()first.try_advance()is genuinely expensive: aSeqCstfence plus a full traversal of the intrusive linked list of every registered participant (cache-miss-heavy pointer chasing, as theTODOthere notes).Advancing the epoch is only useful to make queued
SealedBags expirable. When the global garbage queue is empty there is nothing to expire and nothing for the subsequenttry_pop_ifloop to pop, so the entiretry_advancecost is wasted. In read-mostly workloads wheredefer_destroyis rare (the scenario in #1001), this is paid on every periodiccollect()and scales O(participants).Change
Short-circuit
collect()when the queue is observed empty, via a newQueue::is_empty(guard)(a singleAcquireload ofhead.next, mirroring the existing test-only helper) — instead of theSeqCstfence + O(participants) traversal.Why it's sound
Skipping the advance is monotone-conservative: it can only delay epoch advancement, never advance it early, so it can never cause premature reclamation. Liveness is preserved because any thread that pushed a bag is itself on the
collect()cadence and will observe a non-empty queue and advance. Worst case is a bounded reclamation delay, in the same spirit as the existingIterError::Stalledearly return that already declines to advance.Verification
cargo test -p crossbeam-epoch— lib (44) + doctests (51) green, includingcollector::tests::pin_holds_advance,incremental, andbuffering.ci/crossbeam-epoch-loom.sh(loom,LOOM_MAX_PREEMPTIONS=2) — green.cargo fmtapplied.The win is deterministic by construction: in the empty-queue case
collect()now performs oneAcquireload instead of aSeqCstfence plus a traversal of all participants. Happy to add a benchmark or a counted-metric harness if useful.