Skip to content

perf(event): avoid locking manual-reset state reads and ready waits - #315

Closed
tisonkun wants to merge 3 commits into
apache:mainfrom
tisonkun:codex/event-state-fast-paths
Closed

tisonkun wants to merge 3 commits into
apache:mainfrom
tisonkun:codex/event-state-fast-paths

Conversation

@tisonkun

@tisonkun tisonkun commented Sep 14, 2026

Copy link
Copy Markdown
Member

Summary

Let ManualResetEvent::is_set, try_wait, and new waits that observe a set event complete without acquiring the waiter mutex. Preserve the public API and released waits across reset. Add readiness benchmarks and a cross-thread publication test over repeated set/reset cycles.

Design Notes

All flag writes and waiter-list changes hold the same mutex; lock-free paths only read the flag. set publishes with Release and lock-free reads use Acquire. reset keeps the writer lock and clears with Relaxed because clearing the flag does not publish data to successful waits.

A wait that misses the fast path rechecks the flag under the lock before registering. Registered waits complete through their retained WaitList nodes, so a later reset cannot revoke their release. The waiter list is stored directly in its mutex.

Local single-thread M4 Max measurements of the retained manual-reset implementation compared with base 87017f9 found already-set waits at 5.11 → 2.23 ns and registered wait/wake/reset cycles at 24.77 → 24.52 ns. Separate set/reset measurements showed roughly 1–2 ns of additional overhead. The optimization targets repeated readiness observations and already-set waits.

Validation: cargo x check, cargo x test, cargo x lint, cargo x bench --no-run, and the event benchmark smoke tests passed.

Closes #252.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Concurrency-sensitive synchronization changes and a remaining changelog nit warrant final human review.

Pull request overview

Optimizes ManualResetEvent and AutoResetEvent state queries and immediately successful waits with atomic fast paths while preserving waiter and signal semantics.

Changes:

  • Separates atomic event state from mutex-protected waiter management.
  • Adds concurrency, cancellation, publication, and signal-consumption tests.
  • Adds event benchmarks and changelog coverage.
File summaries
File Description
tests-integration/tests/event_test.rs Extends manual-reset concurrency and reentrancy tests.
tests-integration/tests/auto_reset_event_test.rs Adds auto-reset race and signal-consumption tests.
CHANGELOG.md Documents the performance improvement; AutoResetEvent coverage should be added.
benchmarks/asyncband/event/wait.rs Adds wait-path benchmarks.
benchmarks/asyncband/event/mod.rs Registers event benchmarks.
benchmarks/asyncband/event/auto_reset.rs Adds auto-reset benchmarks.
asyncband/src/event/manual_reset.rs Adds atomic state fast paths.
asyncband/src/event/auto_reset.rs Adds atomic signal consumption and state queries.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CHANGELOG.md
@tisonkun tisonkun changed the title perf(event): avoid locking state queries and immediate waits perf(event): avoid locking manual-reset state reads and ready waits Sep 14, 2026
@tisonkun tisonkun closed this Sep 14, 2026
@tisonkun
tisonkun deleted the codex/event-state-fast-paths branch September 14, 2026 23:08
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.

Explore ManualResetEvent waiter storage and state fast paths

2 participants