Skip to content

Reduce uncontended Mutex lock and unlock overhead #403

Open

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. sundeep8967 commented on Nov 3, 2026

    @sundeep8967

    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?

  2. tisonkun commented on Nov 3, 2026

    @tisonkun
    MemberAuthor

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

  3. added 2 commits that reference this issue on Nov 9, 2026
    ba0ef2a
    4da8edd
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions