Skip to content

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

Merged
tisonkun merged 4 commits into
apache:mainfrom
nanami-0713:deslopify/once-event-latch
Dec 3, 2026
Merged

tisonkun merged 4 commits into
apache:mainfrom
nanami-0713:deslopify/once-event-latch

Conversation

@nanami-0713

@nanami-0713 nanami-0713 commented Dec 1, 2026 •

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.
  • Use Deref<Target = T> for the private waiters of AutoResetEvent, ManualResetEvent, and Latch, so each can poll and unregister through either &T or Arc<T>. The owned waits for AutoResetEvent and Latch now store their Arc alongside their registration. This 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 240 B 176 B
OnceCell<[u8; 64]>::set 320 B 176 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.

nanami-0713 and others added 4 commits December 2, 2026 01:06
The wait future stores either a reference or an Arc to the primitive.
Use the handle's dereference target so polling and cancellation can
call the primitive's methods directly.

Signed-off-by: tison <wander4096@gmail.com>
Both wait APIs store a handle to the event and use it to poll or
unregister a waiter. Express that access through the handle's
dereference target, as in the auto-reset event.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun enabled auto-merge (squash) December 3, 2026 07:51
@tisonkun
tisonkun merged commit 3aac6ef into apache:main Dec 3, 2026
9 checks passed
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.

2 participants