Skip to content

perf(semaphore): shrink the permit acquisition future - #394

Merged
orthur2 merged 2 commits into
apache:mainfrom
orthur2:perf/semaphore-acquire-future
Nov 4, 2026
Merged

orthur2 merged 2 commits into
apache:mainfrom
orthur2:perf/semaphore-acquire-future

Conversation

@orthur2

@orthur2 orthur2 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The first commit makes internal::Semaphore::acquire return its Acquire future instead of awaiting it inside an async fn. On main, that async fn keeps its own copy of &Semaphore and n next to the Acquire, plus a state tag, so the waiting state of every public future built on it carries 24 bytes it never uses. poll_acquire, which returned the same Acquire for the bounded broadcast sender, goes away. Acquire gets the crate's usual must_use message, since it no longer comes from an async fn.

The second commit removes Acquire::done, whose padding makes Acquire 32 bytes instead of 24. The flag only made a completed Acquire return Ready again without taking more permits, and no caller polls one again: every caller but the broadcast sender awaits it inside an async fn, and the sender replaces it once it completes. Polling a completed Acquire now acquires again, as Tokio's Acquire does.

Public APIs and their semantics are unchanged. Sizes from size_of_val on aarch64-apple-darwin with Rust 1.96.0, using u64 values and initializers that return ready(..):

Future main First commit Both commits
Mutex::lock, Mutex::lock_owned 80 56 48
RwLock::read, write, read_owned, write_owned 80 56 48
Condvar::wait 136 112 104
Semaphore::acquire, Semaphore::acquire_owned 96 72 64
Once::call_once 136 112 104
OnceCell::get_or_init 152 128 120
OnceCell::try_insert 216 192 184
UpgradableRwLock::write 112 88 80
broadcast BoundedSender::send 80 80 72

Once and OnceCell futures shrink only when the permit wait is their largest state, so an initializer that keeps 1 KiB across an .await leaves call_once at 1072 bytes and get_or_init at 1088 on all three versions.

Design Notes

Resetting requested_permits to zero on completion would keep the old re-poll behavior at the same size, since a zero-permit acquisition always succeeds without queueing. I haven't measured it. The first commit alone also keeps the old behavior.

A hand-written Mutex::lock future (poll_fn over Acquire and &Mutex) would be 32 bytes against 48 with both commits, because the async fn keeps self twice and a state tag. I left it alone since it would change the public signatures from async fn.

Benchmarks

main is 36ba6f2. Apple M5, macOS 26.4, Rust 1.96.0. cargo x bench --bench primitives -- <filter> --repeat 3 for the mutex::lock, rwlock::, semaphore::, once::get_or_init, pool::bounded, broadcast::mpmc::bounded, and condvar::wait scenarios, with the three versions rotating for 9 rounds. main is the median elapsed time of the 24 runs after the first round, and the other columns are the change of their medians.

Benchmark main First commit Both commits
mutex::lock::uncontended_reuse 335 ms -7.5% -12.1%
mutex::lock::queued_handoff_cycle/16 211 ms +0.0% +1.4%
rwlock::read::read_reuse 224 ms -2.7% -3.6%
rwlock::read::eight_reads_then_one_write 209 ms +4.5% +1.2%
rwlock::upgradable::read_reuse 261 ms -12.5% -18.2%
rwlock::upgradable::eight_reads_then_one_write 260 ms -12.9% -17.7%
semaphore::acquire::queued_owned_burst/4 250 ms -7.2% -7.8%
condvar::wait::notify_waiter_batch/1 466 ms -6.5% -9.3%
condvar::wait::notify_waiter_batch/16 208 ms +1.9% +1.9%
once::get_or_init::initialize_cell_waiter_batch/4 278 ms -3.2% -3.8%
pool::bounded::bounded_warm_get_and_return 312 ms -1.4% -2.1%
semaphore::acquire::try_acquire_release 233 ms -0.4% +0.2%

try_acquire_release never builds an Acquire, so its row is the drift between binaries: +0.2% by median, yet slower than main in 6 of the 8 rounds.

The first commit alone is slower than main on rwlock::read::eight_reads_then_one_write in all 8 rounds, and I haven't found why. On an earlier base, forcing Acquire::poll_once out of line or aligning functions and blocks to 64 bytes didn't remove it. With the second commit it is +1.2%, faster than main in 3 of the 8 rounds.

The broadcast send future only shrinks with the second commit. broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders, which parks senders on a full channel, is 1.8% to 11% faster with both commits across its four sizes, but main's own runs at the smallest size spread from 584 to 788 ms.

With both commits, no scenario is more than 2% slower than main, and they are at or below the first commit alone on 54 of the 64 scenarios, so I'd keep both.

All 64 scenarios
Benchmark main First commit Both commits
mutex::lock::uncontended_reuse 335 ms -7.5% -12.1%
mutex::lock::queued_handoff_cycle/1 394 ms +0.5% -1.0%
mutex::lock::queued_handoff_cycle/4 254 ms +0.6% -1.8%
mutex::lock::queued_handoff_cycle/16 211 ms +0.0% +1.4%
rwlock::upgradable::read_reuse 261 ms -12.5% -18.2%
rwlock::upgradable::write_reuse 278 ms -4.5% -7.7%
rwlock::upgradable::eight_reads_then_one_write 260 ms -12.9% -17.7%
rwlock::upgradable::upgradable_read_reuse 300 ms -3.0% -9.0%
rwlock::upgradable::upgradable_read_then_upgrade 276 ms -2.9% -6.3%
rwlock::upgradable::upgradable_read_then_try_upgrade 262 ms -3.4% -6.7%
rwlock::upgradable::upgradable_read_then_downgrade 303 ms -3.8% -9.6%
rwlock::upgradable::readers_then_queued_upgrade/1 77 ms -10.4% -9.1%
rwlock::upgradable::readers_then_queued_upgrade/4 122 ms -7.0% -7.4%
rwlock::upgradable::readers_then_queued_upgrade/16 322 ms -7.9% -10.0%
rwlock::read::eight_reads_then_one_write 209 ms +4.5% +1.2%
rwlock::read::read_reuse 224 ms -2.7% -3.6%
rwlock::write::readers_then_queued_writer/1 589 ms -3.8% -0.5%
rwlock::write::readers_then_queued_writer/4 198 ms -0.3% +1.8%
rwlock::write::readers_then_queued_writer/16 111 ms -1.4% -3.6%
semaphore::acquire::handoff_permit 198 ms -1.5% -0.5%
semaphore::acquire::try_acquire_release 233 ms -0.4% +0.2%
semaphore::acquire::try_acquire_release_with_spare_permits 252 ms +0.2% +0.2%
semaphore::acquire::owned_try_acquire_release 252 ms +0.2% -0.8%
semaphore::acquire::queued_owned_burst/1 515 ms -4.4% -8.9%
semaphore::acquire::queued_owned_burst/4 250 ms -7.2% -7.8%
semaphore::acquire::queued_owned_burst/16 210 ms -3.6% -3.6%
semaphore::acquire::cancel_pending_acquire 170 ms -2.9% -4.4%
semaphore::acquire::owned_try_acquire_rejected 324 ms -0.6% -0.8%
semaphore::acquire::cancel_pending_owned_batch/1 392 ms -5.7% -8.8%
semaphore::acquire::cancel_pending_owned_batch/4 292 ms -5.0% -3.9%
semaphore::acquire::cancel_pending_owned_batch/16 263 ms -2.3% -3.0%
semaphore::release::fulfill_debt_repeatedly 294 ms -0.2% -0.2%
semaphore::release::release_to_waiters/1 513 ms -6.7% -9.4%
semaphore::release::release_to_waiters/2 340 ms -6.9% -9.9%
semaphore::release::release_to_waiters/32 183 ms -2.2% -2.2%
semaphore::release::release_to_waiters/256 159 ms -2.2% -3.1%
once::get_or_init::initialize_cell 243 ms -3.1% -3.3%
once::get_or_init::initialize_cell_waiter_batch/1 548 ms -2.6% -2.5%
once::get_or_init::initialize_cell_waiter_batch/4 278 ms -3.2% -3.8%
once::get_or_init::initialize_cell_waiter_batch/16 221 ms -0.5% -2.9%
pool::bounded::bounded_warm_get_and_return 312 ms -1.4% -2.1%
pool::bounded::bounded_warm_get_and_return_with_spare_capacity 268 ms +0.7% +0.7%
pool::bounded::bounded_contended_handoff 348 ms -2.0% -2.2%
pool::bounded::cancel_bounded_waiter 208 ms -2.9% -3.6%
broadcast::mpmc::bounded::try_send_and_try_recv 262 ms +1.3% -0.6%
broadcast::mpmc::bounded::try_send_and_drain_fanout/1 573 ms -0.4% -3.2%
broadcast::mpmc::bounded::try_send_and_drain_fanout/4 219 ms +0.0% -2.7%
broadcast::mpmc::bounded::try_send_and_drain_fanout/16 143 ms +0.0% -2.1%
broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/1 247 ms +0.6% -2.0%
broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/4 158 ms -0.9% -1.3%
broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/16 126 ms -0.8% +0.8%
broadcast::mpmc::bounded::deliver_to_waiting_receiver 280 ms -1.6% -1.8%
broadcast::mpmc::bounded::send_without_receivers 298 ms -2.0% -2.0%
broadcast::mpmc::bounded::try_send_when_full 252 ms +0.0% +0.0%
broadcast::mpmc::bounded::cancel_blocked_send 236 ms -2.1% +1.5%
broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/1 658 ms +1.4% -11.0%
broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/2 454 ms -3.4% -8.2%
broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/32 214 ms +2.8% -4.0%
broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/256 193 ms +0.8% -1.8%
condvar::wait::notify_waiter 324 ms +0.2% -2.0%
condvar::wait::notify_waiter_batch/1 466 ms -6.5% -9.3%
condvar::wait::notify_waiter_batch/4 252 ms -2.0% -6.0%
condvar::wait::notify_waiter_batch/16 208 ms +1.9% +1.9%
condvar::wait::cancel_pending 300 ms -0.7% -3.2%

Validation

  • cargo x lint, cargo x check (26 feature configurations), and cargo x test (685 passed)
  • cargo x miri with CI's MIRIFLAGS. Miri can't run Tokio's kqueue driver on macOS, so broadcast_test, phaser_test, and semaphore_ordering_test ran with --target aarch64-unknown-linux-gnu.

@orthur2 orthur2 changed the title perf(semaphore): return the acquisition future directly perf(semaphore): shrink the permit acquisition future Nov 3, 2026
@orthur2
orthur2 force-pushed the perf/semaphore-acquire-future branch from 28b21de to 20d58ba Compare November 3, 2026 15:48
@orthur2
orthur2 marked this pull request as ready for review November 3, 2026 15:59
@orthur2
orthur2 requested review from QwQBiG and tisonkun November 3, 2026 16:00

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One minor comment we may review other handmade Futures later.

Comment on lines -334 to -336
if *done {
return Poll::Ready(());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we may assume that caller know when not to poll again and make this future not Fused.

This can be OK but I wonder if we should review other handmade Future's contract later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I opened #408 to go through them.

@QwQBiG QwQBiG left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

The bounded broadcast path creates a fresh Acquire if it needs to retry after Ready, and all other callers await it directly. We can review how the other hand-written futures handle completion separately.

All 685 tests passed with cargo x test. I reproduced the reported reductions in future size on arm64 macOS with Rust 1.99.0.

@orthur2
orthur2 merged commit 2487ee2 into apache:main Nov 4, 2026
9 checks passed
@orthur2
orthur2 deleted the perf/semaphore-acquire-future branch November 4, 2026 15:28
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.

3 participants