Repository navigation
perf(rwlock): avoid wait-queue locking on uncontended write and reservation releases - #448
Merged
Merged
Conversation
Mark uncontended `Semaphore` acquire, try-acquire, poll, and release entry points `#[inline]`, move queue-locked slow paths into `#[cold]` helpers, and return cancelled permits directly when the wait queue is empty without constructing a `WakerBatch`.
…nd downgrade Scale `RwLock` reader slots to two permits and keep one spare permit whenever `max_readers <= usize::MAX >> 1`, so uncontended write guard drops and downgrades release permits through the semaphore's lock-free fast path instead of acquiring its wait-queue mutex, while preserving one-permit accounting up to `NonZeroUsize::MAX` without overflow. Also represent `WriteAccess` with `Option<O>` over `LockState` inside `rwlock::access` instead of storing a redundant `permits_acquired` count, shrinking `RwLockWriteGuard`, `OwnedRwLockWriteGuard`, `MappedRwLockWriteGuard`, and `OwnedMappedRwLockWriteGuard` by one word and avoiding an `Arc` clone on `OwnedRwLockWriteGuard::downgrade`.
…n release Back `UpgradableRwLock`'s internal upgrade reservation with a three-permit semaphore where each reservation claims and returns two permits, leaving one spare permit when uncontended so releasing a reservation during `upgrade`, `downgrade`, `write`, or guard drop skips the semaphore's wait-queue mutex. Fixes #404
8 tasks done
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
Reduce uncontended
RwLockandUpgradableRwLockacquisition, promotion, downgrade, and release overhead across three focused commits:Semaphorepaths: Mark uncontendedinternal::semaphore::Semaphoreacquire, try-acquire, poll, and release entry points#[inline], move queue-locked slow paths into#[cold]helpers, and return cancelled permits directly when the wait queue is empty without constructing aWakerBatch.RwLockwrite release and downgrade: Scale reader slots to two permits with one spare permit whenevermax_readers <= usize::MAX >> 1(preserving one-permit accounting up toNonZeroUsize::MAXwithout overflow), and replaceWriteAccess::permits_acquiredwithOption<O>overLockStateto shrinkRwLockWriteGuard,OwnedRwLockWriteGuard,MappedRwLockWriteGuard, andOwnedMappedRwLockWriteGuardby one word while avoiding anArcclone onOwnedRwLockWriteGuard::downgrade.UpgradableRwLockreservation release (Reduce the cost of upgrading a held UpgradableRwLock read guard #404): BackUpgradableRwLock's internal upgrade reservation with a three-permit semaphore (two permits per reservation, one spare permit) so uncontendedupgradable_read,upgrade,try_upgrade,downgrade, andwriterelease the reservation without acquiring its wait-queue mutex.Benchmarks
Apple M4 Max (aarch64-apple-darwin), 7-run medians via
cargo x bench --bench primitives -- rwlock --repeat 7andcargo x bench --bench upgradable_stages -- --rounds 2000000 --repeat 7:main(7c39e3c)mainrwlock::upgradable::upgradable_read_then_upgraderwlock::upgradable::upgradable_read_then_try_upgraderwlock::upgradable::upgradable_read_then_downgraderwlock::upgradable::upgradable_read_reuserwlock::upgradable::write_reuserwlock::upgradable::read_reuserwlock::upgradable::readers_then_queued_upgrade/1rwlock::upgradable::readers_then_queued_upgrade/4rwlock::upgradable::readers_then_queued_upgrade/16rwlock::read::read_reuserwlock::read::eight_reads_then_one_writeDiagnostic
upgradable_stages(2,000,000rounds, empty clock interval15.37 ns/round): uncontended acquisition drops from25.86to21.99 ns/round, promotion from28.55to23.92 ns/round, and queued acquisition behind a writer from62.24to52.05 ns/round. Cancelling the first request queued behind a held lock returns the spare permit (~18 ms across 5,000,000 queued-cancel cycles).Fixes #404
Part of #445