Skip to content

Futex: skip timer path and redundant bucket-lock for untimed waits - #536

Merged
lalinsky merged 1 commit into
mainfrom
futex-untimed-shortcircuit
Jul 3, 2026
Merged

lalinsky merged 1 commit into
mainfrom
futex-untimed-shortcircuit

Conversation

@lalinsky

@lalinsky lalinsky commented Jul 3, 2026

Copy link
Copy Markdown
Owner

What

Futex.timedWaitClock always ran removeFromBucket after a wake to decide the timeout-vs-wake winner (a second bucket-mutex acquisition), and drove the timed waiter path — even when the caller passed no deadline (.none).

For an untimed wait there is no timer, so that post-wake removeFromBucket can never observe a timeout. It's pure waste on the hottest blocking path: every untimed wait took the bucket mutex twice (push + removeFromBucket) instead of once.

This short-circuits .none to the existing untimed wait(), which parks without a timer and lets wake() dequeue the waiter — one bucket-mutex acquisition instead of two.

pub fn timedWaitClock(ptr: *const u32, expect: u32, timeout: Timeout, clock: Clock) (Timeoutable || Cancelable)!void {
    if (timeout == .none) return wait(ptr, expect);
    ...
}

Why it matters broadly

Every std.Io synchronization primitive on zio issues untimed io.futexWait calls (Mutex.lock under contention, Condition.wait, Queue, Semaphore), and they all flow through Futex.timedWaitClock via the io vtable. So this one line benefits the whole std.Io sync layer, not just direct Futex users.

Measurement

A two-task 100k-message queue ping-pong, normalized against a native-Channel run as an ambient-load control (native parks via Waiter directly and does not hit Futex.timedWaitClock):

before after
std.Io.Queue (relative to control) 1.61× 1.47× (−8%)
futex-based channel (relative to control) 1.40× 1.29× (−8%)

Testing

./check.sh — 507/507 unit tests pass. No behavioral change for timed waits; .none waits keep identical semantics (cancelable, spurious-wake-safe), just without the timer setup and the extra bucket-mutex round-trip.

timedWaitClock unconditionally ran removeFromBucket after every wake to
determine the timeout-vs-wake winner. For an untimed (.none) wait there is
no timer, so that check can never observe a timeout — it is a pure-waste
second bucket-mutex acquisition on the hottest blocking path.

Short-circuit .none to the untimed wait(), which parks without a timer and
relies on wake() to dequeue the waiter (one bucket-mutex acquisition instead
of two). This speeds up every std.Io futex waiter on zio (Mutex, Condition,
Queue, Semaphore), since they all issue untimed io.futexWait calls: ~8% on a
two-task queue ping-pong for both std.Io.Queue and a hand-rolled channel.
@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 71ec4626-c709-451b-b591-c18a303c5669

📥 Commits

Reviewing files that changed from the base of the PR and between e75542c and 4fa4813.

📒 Files selected for processing (1)
  • src/sync/Futex.zig

📝 Walkthrough

Walkthrough

timedWaitClock in src/sync/Futex.zig now checks for timeout == .none and, if so, calls wait(ptr, expect) directly, skipping the timed-wait timer setup and the subsequent unconditional removeFromBucket call.

Changes

Futex Timed Wait Short-Circuit

Layer / File(s) Summary
None-timeout early return
src/sync/Futex.zig
timedWaitClock adds a branch that delegates to wait(ptr, expect) when timeout == .none, avoiding timer setup and the post-wake removeFromBucket call used in the timed path.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • lalinsky/zio#180: Rewrites wait/timedWait mechanics including enqueue/removal logic around the same timed-wait path this PR short-circuits.
  • lalinsky/zio#239: Updates timeout-handling logic in Futex.timedWait directly related to the branch added here.
  • lalinsky/zio#515: Introduces the timedWaitClock API path that this PR's short-circuit branch modifies.

Poem

Six lines added, nothing torn,
a .none check, a path reborn.
No timer set, no bucket chase,
just wait() called, straight to base.
Small diff, clean fix — job's done. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: untimed waits skip the timer path and extra bucket lock.
Description check ✅ Passed The description matches the code change and explains the motivation, impact, and testing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch futex-untimed-shortcircuit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lalinsky
lalinsky merged commit 5e59a67 into main Jul 3, 2026
25 of 27 checks passed
@lalinsky
lalinsky deleted the futex-untimed-shortcircuit branch July 3, 2026 08:34
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