Skip to content

refactor: simplify OnceCell insertion and owned waits in Latch and AutoResetEvent - #498

Open
nanami-0713 wants to merge 2 commits into
apache:mainfrom
nanami-0713:deslopify/once-event-latch
Open

nanami-0713 wants to merge 2 commits into
apache:mainfrom
nanami-0713:deslopify/once-event-latch

Conversation

@nanami-0713

Copy link
Copy Markdown
Contributor

Summary

  • Implement OnceCell::{try_insert, set} directly over the initialized-state fast path and acquire_init, instead of routing an already-built value through get_or_init and an async closure that hands it over through a side Option. Signatures, wake-ups on publication, and cancellation behavior are unchanged.
  • Make the internal AutoResetEvent::wait and Latch::wait futures generic over Borrow<T>, so wait_owned awaits the same future that holds the Arc instead of awaiting a borrowed wait() from a second state machine. This matches what ManualResetEvent already does and also shrinks ShutdownGuard::shutdown_requested_owned and ShutdownWatch::shutdown_requested_owned, which build on Latch::wait_owned.
  • Drop the stale #[allow(dead_code)] on OnceCell::initialized, which set now calls.
  • Add integration coverage for contended and cancelled try_insert/set and for cancelling Latch::wait_owned.

No public API or observable behavior changes, so CHANGELOG.md is untouched and cargo x semver was not run.

Validation

Local validation on macOS (aarch64-apple-darwin, stable rustc 1.99.1, nightly 1.102.0-nightly from 2026-11-08):

  • cargo x check
  • cargo x test — 786 tests passed, including 177 doc tests
  • cargo x lint — clippy, taplo, typos, hawkeye, and the docsrs rustdoc check pass. The rustfmt step stops on two doc-comment diffs in asyncband/src/mpsc/bounded/receiver.rs, which is unchanged here and shows the same diffs on main (b6b8adf) with this nightly; the files touched by this PR are rustfmt-clean.
  • cargo x miri — 572 tests passed; 14 existing tests are ignored under Miri

Future sizes measured with std::mem::size_of_val in the test profile immediately before (b6b8adf) and after this change on the same toolchain. Borrowed wait futures are unchanged.

Future Before After
OnceCell<u64>::try_insert 136 B 64 B
OnceCell<u64>::set 160 B 64 B
OnceCell<String>::try_insert 160 B 96 B
OnceCell<String>::set 200 B 96 B
OnceCell<[u8; 64]>::try_insert 624 B 560 B
OnceCell<[u8; 64]>::set 896 B 560 B
AutoResetEvent::wait_owned 56 B 32 B
Latch::wait_owned 56 B 32 B
ShutdownGuard::shutdown_requested_owned 56 B 32 B
ShutdownWatch::shutdown_requested_owned 56 B 32 B

These are layout numbers for this target and toolchain only. No benchmark was run because the change is a simplification rather than a speed optimization.

This branch has not been deployed

No deployments
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