Skip to content

perf(rwlock): avoid wait-queue locking on uncontended write and reservation releases - #448

Merged
tisonkun merged 3 commits into
mainfrom
perf/rwlock-uncontended-release
Nov 22, 2026
Merged

tisonkun merged 3 commits into
mainfrom
perf/rwlock-uncontended-release

Conversation

@tisonkun

Copy link
Copy Markdown
Member

Summary

Reduce uncontended RwLock and UpgradableRwLock acquisition, promotion, downgrade, and release overhead across three focused commits:

  1. Outline contended Semaphore paths: Mark uncontended internal::semaphore::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.
  2. Avoid wait-queue locking on RwLock write release and downgrade: Scale reader slots to two permits with one spare permit whenever max_readers <= usize::MAX >> 1 (preserving one-permit accounting up to NonZeroUsize::MAX without overflow), and replace WriteAccess::permits_acquired with Option<O> over LockState to shrink RwLockWriteGuard, OwnedRwLockWriteGuard, MappedRwLockWriteGuard, and OwnedMappedRwLockWriteGuard by one word while avoiding an Arc clone on OwnedRwLockWriteGuard::downgrade.
  3. Avoid wait-queue locking on UpgradableRwLock reservation release (Reduce the cost of upgrading a held UpgradableRwLock read guard #404): Back UpgradableRwLock's internal upgrade reservation with a three-permit semaphore (two permits per reservation, one spare permit) so uncontended upgradable_read, upgrade, try_upgrade, downgrade, and write release 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 7 and cargo x bench --bench upgradable_stages -- --rounds 2000000 --repeat 7:

Benchmark Rounds main (7c39e3c) This PR Change vs main
rwlock::upgradable::upgradable_read_then_upgrade 8,300,000 243 ms 110 ms -54.7%
rwlock::upgradable::upgradable_read_then_try_upgrade 10,000,000 246 ms 122 ms -50.4%
rwlock::upgradable::upgradable_read_then_downgrade 15,000,000 245 ms 109 ms -55.5%
rwlock::upgradable::upgradable_read_reuse 15,000,000 244 ms 110 ms -54.9%
rwlock::upgradable::write_reuse 10,000,000 249 ms 93 ms -62.7%
rwlock::upgradable::read_reuse 29,000,000 194 ms 148 ms -23.7%
rwlock::upgradable::readers_then_queued_upgrade/1 1,000,000 75 ms 48 ms -36.0%
rwlock::upgradable::readers_then_queued_upgrade/4 1,000,000 122 ms 90 ms -26.2%
rwlock::upgradable::readers_then_queued_upgrade/16 1,000,000 300 ms 258 ms -14.0%
rwlock::read::read_reuse 30,000,000 193 ms 154 ms -20.2%
rwlock::read::eight_reads_then_one_write 3,000,000 192 ms 165 ms -14.1%

Diagnostic upgradable_stages (2,000,000 rounds, empty clock interval 15.37 ns/round): uncontended acquisition drops from 25.86 to 21.99 ns/round, promotion from 28.55 to 23.92 ns/round, and queued acquisition behind a writer from 62.24 to 52.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

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
@tisonkun
tisonkun merged commit 17c44a8 into main Nov 22, 2026
9 checks passed
@tisonkun
tisonkun deleted the perf/rwlock-uncontended-release branch November 22, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce the cost of upgrading a held UpgradableRwLock read guard

1 participant