Skip to content

Reduce uncontended Mutex lock and unlock overhead #403

Closed

Description

@tisonkun

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

Performance tracking issue #396

Activity

  1. added
    help wanted
    good first issue
    on Nov 3, 2026
  2. sundeep8967 commented on Nov 3, 2026

    @sundeep8967
    Contributor

    Hi team, I would like to work on this performance enhancement. I can benchmark and profile uncontended MutexGuard::drop to 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?

  3. tisonkun commented on Nov 3, 2026

    @tisonkun
    MemberAuthor

    @sundeep8967 Thanks for your interest and feel free to submit a PR for review.

  4. added 2 commits that reference this issue on Nov 9, 2026
    ba0ef2a
    4da8edd
  5. nanami-0713 commented on Nov 10, 2026

    @nanami-0713
    Contributor

    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 #383 peer-checks.rb generator.

    Scenario Rounds Asyncband Tokio async-lock
    Uncontended lock and unlock 24,000,000 297 ms (293..328) 232 ms (232..236) 200 ms (200..205)
    Four queued waiters 1,250,000 203 ms (200..217) 176 ms (173..182) 421 ms (417..437)

    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 primitives run 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):

    • Uncontended steady state: zero allocations across 1,000,000 lock/unlock cycles after warm-up. The uncontended row is not allocation-shaped on this host.
    • Registered-waiter handoff (one holder; a second lock polled 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_amortized under semaphore::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.
    • The one traced warm-up allocation is the blocking executor's thread-local parker cache (this probe's block_on machinery); 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::drop returns the permit through release_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):

    cargo x bench --bench primitives -- mutex::lock::uncontended_reuse --repeat 10 --raw
    cargo x bench --bench peer_checks -- mutex/ --repeat 10 --raw

    Performance tracking issue #396

  6. added a commit that references this issue on Nov 14, 2026
    e01aee3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions