Repository navigation
perf(semaphore): shrink the permit acquisition future - #394
Conversation
28b21de to
20d58ba
Compare
tisonkun
left a comment
There was a problem hiding this comment.
LGTM. One minor comment we may review other handmade Futures later.
| if *done { | ||
| return Poll::Ready(()); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed. I opened #408 to go through them.
QwQBiG
left a comment
There was a problem hiding this comment.
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.
Summary
The first commit makes
internal::Semaphore::acquirereturn itsAcquirefuture instead of awaiting it inside anasync fn. Onmain, thatasync fnkeeps its own copy of&Semaphoreandnnext to theAcquire, 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 sameAcquirefor the bounded broadcast sender, goes away.Acquiregets the crate's usualmust_usemessage, since it no longer comes from anasync fn.The second commit removes
Acquire::done, whose padding makesAcquire32 bytes instead of 24. The flag only made a completedAcquirereturnReadyagain without taking more permits, and no caller polls one again: every caller but the broadcast sender awaits it inside anasync fn, and the sender replaces it once it completes. Polling a completedAcquirenow acquires again, as Tokio'sAcquiredoes.Public APIs and their semantics are unchanged. Sizes from
size_of_valon aarch64-apple-darwin with Rust 1.96.0, usingu64values and initializers that returnready(..):mainMutex::lock,Mutex::lock_ownedRwLock::read,write,read_owned,write_ownedCondvar::waitSemaphore::acquire,Semaphore::acquire_ownedOnce::call_onceOnceCell::get_or_initOnceCell::try_insertUpgradableRwLock::writeBoundedSender::sendOnceandOnceCellfutures shrink only when the permit wait is their largest state, so an initializer that keeps 1 KiB across an.awaitleavescall_onceat 1072 bytes andget_or_initat 1088 on all three versions.Design Notes
Resetting
requested_permitsto 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::lockfuture (poll_fnoverAcquireand&Mutex) would be 32 bytes against 48 with both commits, because theasync fnkeepsselftwice and a state tag. I left it alone since it would change the public signatures fromasync fn.Benchmarks
mainis36ba6f2. Apple M5, macOS 26.4, Rust 1.96.0.cargo x bench --bench primitives -- <filter> --repeat 3for themutex::lock,rwlock::,semaphore::,once::get_or_init,pool::bounded,broadcast::mpmc::bounded, andcondvar::waitscenarios, with the three versions rotating for 9 rounds.mainis the median elapsed time of the 24 runs after the first round, and the other columns are the change of their medians.mainmutex::lock::uncontended_reusemutex::lock::queued_handoff_cycle/16rwlock::read::read_reuserwlock::read::eight_reads_then_one_writerwlock::upgradable::read_reuserwlock::upgradable::eight_reads_then_one_writesemaphore::acquire::queued_owned_burst/4condvar::wait::notify_waiter_batch/1condvar::wait::notify_waiter_batch/16once::get_or_init::initialize_cell_waiter_batch/4pool::bounded::bounded_warm_get_and_returnsemaphore::acquire::try_acquire_releasetry_acquire_releasenever builds anAcquire, so its row is the drift between binaries: +0.2% by median, yet slower thanmainin 6 of the 8 rounds.The first commit alone is slower than
mainonrwlock::read::eight_reads_then_one_writein all 8 rounds, and I haven't found why. On an earlier base, forcingAcquire::poll_onceout of line or aligning functions and blocks to 64 bytes didn't remove it. With the second commit it is +1.2%, faster thanmainin 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, butmain'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
mainmutex::lock::uncontended_reusemutex::lock::queued_handoff_cycle/1mutex::lock::queued_handoff_cycle/4mutex::lock::queued_handoff_cycle/16rwlock::upgradable::read_reuserwlock::upgradable::write_reuserwlock::upgradable::eight_reads_then_one_writerwlock::upgradable::upgradable_read_reuserwlock::upgradable::upgradable_read_then_upgraderwlock::upgradable::upgradable_read_then_try_upgraderwlock::upgradable::upgradable_read_then_downgraderwlock::upgradable::readers_then_queued_upgrade/1rwlock::upgradable::readers_then_queued_upgrade/4rwlock::upgradable::readers_then_queued_upgrade/16rwlock::read::eight_reads_then_one_writerwlock::read::read_reuserwlock::write::readers_then_queued_writer/1rwlock::write::readers_then_queued_writer/4rwlock::write::readers_then_queued_writer/16semaphore::acquire::handoff_permitsemaphore::acquire::try_acquire_releasesemaphore::acquire::try_acquire_release_with_spare_permitssemaphore::acquire::owned_try_acquire_releasesemaphore::acquire::queued_owned_burst/1semaphore::acquire::queued_owned_burst/4semaphore::acquire::queued_owned_burst/16semaphore::acquire::cancel_pending_acquiresemaphore::acquire::owned_try_acquire_rejectedsemaphore::acquire::cancel_pending_owned_batch/1semaphore::acquire::cancel_pending_owned_batch/4semaphore::acquire::cancel_pending_owned_batch/16semaphore::release::fulfill_debt_repeatedlysemaphore::release::release_to_waiters/1semaphore::release::release_to_waiters/2semaphore::release::release_to_waiters/32semaphore::release::release_to_waiters/256once::get_or_init::initialize_cellonce::get_or_init::initialize_cell_waiter_batch/1once::get_or_init::initialize_cell_waiter_batch/4once::get_or_init::initialize_cell_waiter_batch/16pool::bounded::bounded_warm_get_and_returnpool::bounded::bounded_warm_get_and_return_with_spare_capacitypool::bounded::bounded_contended_handoffpool::bounded::cancel_bounded_waiterbroadcast::mpmc::bounded::try_send_and_try_recvbroadcast::mpmc::bounded::try_send_and_drain_fanout/1broadcast::mpmc::bounded::try_send_and_drain_fanout/4broadcast::mpmc::bounded::try_send_and_drain_fanout/16broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/1broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/4broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/16broadcast::mpmc::bounded::deliver_to_waiting_receiverbroadcast::mpmc::bounded::send_without_receiversbroadcast::mpmc::bounded::try_send_when_fullbroadcast::mpmc::bounded::cancel_blocked_sendbroadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/1broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/2broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/32broadcast::mpmc::bounded::drop_lagging_receiver_wakes_senders/256condvar::wait::notify_waitercondvar::wait::notify_waiter_batch/1condvar::wait::notify_waiter_batch/4condvar::wait::notify_waiter_batch/16condvar::wait::cancel_pendingValidation
cargo x lint,cargo x check(26 feature configurations), andcargo x test(685 passed)cargo x miriwith CI'sMIRIFLAGS. Miri can't run Tokio'skqueuedriver on macOS, sobroadcast_test,phaser_test, andsemaphore_ordering_testran with--target aarch64-unknown-linux-gnu.