Skip to content

Add benchmark - #4

Merged
darsto merged 8 commits into
masterfrom
fix/benchmark-and-fixes
Sep 27, 2026
Merged

darsto merged 8 commits into
masterfrom
fix/benchmark-and-fixes

Conversation

@darsto

@darsto darsto commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Add benchmark and fixup the bugs it revealed.

While here, fixup no-std builds and add CI for it.

borrow_dyn_multi_thread* tests occasionally hanged.
A wait_to_lend() could register a waker that replaced
an active LendGuard's waker, potentially preventing
that LendGuard's thread from ever waking up.

Fix that up by checking mutex.state in wait_to_lend()
and return early if another lend()/wait_to_lend() is
in progress.

The borrow_dyn_multi_thread* tests need to be adjusted
too as instead of deadlocking they would panic now.
To properly have multiple concurrent lend-ers it's
necessary to synchronize the lending externally, i.e.
with a normal async mutex.
```
warning: use of deprecated module `core::intrinsics`: import this function via `std::mem` instead
   --> src/mpmc.rs:174:39
    |
174 |         Ok(unsafe { core::intrinsics::transmute(slot) })
    |                                       ^^^^^^^^^
    |
    = note: `#[warn(deprecated)]` on by default

warning: use of deprecated module `core::intrinsics`: import this function via `std::mem` instead
   --> src/mpmc.rs:235:41
    |
235 |         Some(unsafe { core::intrinsics::transmute(slot) })
    |                                         ^^^^^^^^^

warning: `borrow_mutex` (lib) generated 2 warnings (1 duplicate)
```
The uncontended benchmark was actually affected by the extra
CAS+store, making uncontended wait_for_lend() marginally slower
than smol::lock::Mutex.

Use a separate AtomicWaker for lend() and wait_to_lend(), then
don't check for multiple parallel wait_to_lend().
This means it's possible for some wait_to_lend() to overwrite
another wait-er's waker, leading to that wait-er not being
awoken. It's a misbehavior due to misuse, but too bad.

In uncontended case, BorrowMutex is now again marginally faster
than smol::lock::Mutex.
@darsto
darsto force-pushed the fix/benchmark-and-fixes branch from 2f9a30d to 8deba88 Compare September 27, 2026 14:38
@darsto
darsto force-pushed the fix/benchmark-and-fixes branch from 8deba88 to 23c4e56 Compare September 27, 2026 14:44
@darsto
darsto merged commit 56633b6 into master Sep 27, 2026
5 checks passed
@darsto
darsto deleted the fix/benchmark-and-fixes branch September 27, 2026 15:00
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.

1 participant