Repository navigation
Description
Repeatedly acquiring and dropping a borrowed Mutex guard on the same mutex took 322 ms versus 199 ms for async-lock. The ordering changes under contention. With four queued waiters, Asyncband was faster than async-lock and behind Tokio.
Apple M4 Max, macOS arm64, rustc 1.101.0-nightly. Each cell gives rank, elapsed time relative to Asyncband and the whole-batch median over 10 interleaved runs. Lower is faster. Supplemental measurements
| Scenario | Fixed rounds | Asyncband | Tokio | async-lock |
|---|---|---|---|---|
| Uncontended lock and unlock | 24,000,000 | #3, 1.00× (322 ms) | #2, 0.74× (237 ms) | #1, 0.62× (199 ms) |
| Four queued waiters | 1,250,000 | #2, 1.00× (289 ms) | #1, 0.84× (242 ms) | #3, 1.88× (543 ms) |
MutexGuard::drop returns the sole permit through release_with_lock, which takes the semaphore's queue lock even when nobody is waiting. This benchmark reuses the same mutex across rounds. In a separate run, substituting parking_lot for the internal lock moved the Asyncband median from 334 to 312 ms, with async-lock at 198 ms. The cost of taking the queue lock on each uncontended release is worth isolating.
First contribution
Profile acquisition and guard drop separately with an empty queue and with a registered waiter. Compare a candidate against both rows. Keep FIFO order for queued callers and verify that cancelling a waiter or a granted handoff still lets the next caller progress. Cross-check future-size work already under review in #394. Related PR #394
To run the supplemental peer_checks command below, save and run peer-checks.rb from the linked comment in a disposable checkout of 6102089. The benchmark and library sources match 36ba6f2. Generator and instructions
cargo x bench --bench primitives -- mutex::lock::uncontended_reuse --repeat 10 --raw
cargo x bench --bench peer_checks -- mutex/ --repeat 10 --raw
Activity
sundeep8967 commentedon Nov 3, 2026
Hi team, I would like to work on this performance enhancement. I can benchmark and profile uncontended
MutexGuard::dropto isolate and avoid acquiring the semaphore's queue lock when there are no waiters registered, while verifying FIFO order and cancellation edge cases. Could you please assign this to me?tisonkun commentedon Nov 3, 2026
@sundeep8967 Thanks for your interest and feel free to submit a PR for review.
perf(mutex): optimize uncontended lock release with spare permit
perf(mutex): optimize uncontended lock release with spare permit
nanami-0713 commentedon Nov 10, 2026
Reproduced both rows on Apple M5 (10 cores), macOS 27.0.1, rustc 1.99.1 stable, in a disposable checkout of
b1354ae(current main, so #394's acquisition-future shrink is already in the tree). Cells give the median of ten repetitions with min..max ranges; all three implementations ran interleaved in one session via the PR #383peer-checks.rbgenerator.Both orderings match the issue: async-lock wins the uncontended row (297/200 = 1.49x here; 322/199 = 1.62x on the issue's M4 Max table) and falls to last under four queued waiters, with Asyncband between. A single-library
primitivesrun of the uncontended row in its own session measured 309 ms (295..334), consistent with the interleaved row.Allocation probe (a
#[global_allocator]counter over three windows):lockpolled to pending; holder drops; the waiter completes and drops): 100,000 cycles allocate once in total — 192 B on the first registration. The captured backtrace names the site:Arena<WaitNode>::insert→RawVec::grow_one→grow_amortizedundersemaphore::acquired_or_enqueue— the waiter arena's slot-vector growth, the same first-registration cost documented in Reduce first-waiter overhead in Semaphore permit handoff #400. The remaining 99,999 cycles allocate nothing.block_onmachinery); nothing traced or counted there recurred in the steady-state window.So the uncontended cost is not in allocation; it sits in the non-allocating release path. That is consistent with the issue's own analysis —
MutexGuard::dropreturns the permit throughrelease_with_lock, which takes the queue lock even with no waiter — and with the issue's parking_lot control, which moved the median only from 334 to 312 ms on its host while async-lock's 199 ms row was untouched.Commands used (generator and instructions from the PR #383 comment linked in the issue):
Performance tracking issue #396
perf(mutex): optimize uncontended lock release (apache#406)