Repository navigation
time: round Duration.to*() conversions up instead of truncating - #499
Conversation
A sub-unit duration was floored, so a sub-millisecond timer deadline became
a 0ms timeout. Timeout-based polls (epoll_wait, poll, IOCP) then return
immediately and busy-spin until the deadline actually arrives, burning CPU.
kqueue/io_uring use nanosecond timespecs and were unaffected, which is why
high CPU was only observed on the epoll backend.
Round up in to{Microseconds,Milliseconds,Seconds,Minutes}() so a non-zero
duration never converts down to a zero wait. The addition saturates so the
maximum value does not overflow.
Refs #493
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
Duration ceiling rounding
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/time.zig`:
- Around line 803-805: The test for maximum Duration value in the ceiling
saturation boundary is incomplete. The call to Duration.max.toMilliseconds()
only verifies the function executes without crashing but does not assert the
actual returned value. Capture the return value from the toMilliseconds() method
call on Duration.max and add an explicit assertion to verify it matches the
expected maximum milliseconds value, ensuring the saturation behavior is
correctly tested for regressions.
- Around line 84-85: The ceiling division logic at lines 84-85 (and repeated at
lines 93-94, 102-103, and 111-112) uses saturating addition before division,
which produces incorrect results when the saturating pre-add overflows, making
the result 1 unit too small. Replace the saturating add approach with
quotient/remainder ceiling logic instead: calculate the quotient of self.value
divided by divisor, then calculate the remainder; if the remainder is non-zero,
add 1 to the quotient to get the correct ceiling value. Apply this fix pattern
to all four affected division blocks that currently use the (self.value +|
(divisor - 1)) / divisor pattern.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: caa09d8e-78da-4f7b-ba00-6e10daa1d2e7
📒 Files selected for processing (1)
src/time.zig
The (value +| (divisor - 1)) / divisor form saturates the pre-add near TimeInt max, swallowing the rounding term and yielding floor instead of ceiling for the largest durations. Replace with quotient/remainder ceiling (value / divisor + (value % divisor != 0)), which never overflows, and assert the exact Duration.max result in the test. Addresses CodeRabbit review on #499. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Problem
Duration.to{Microseconds,Milliseconds,Seconds,Minutes}()floored when converting to a coarser unit. As a result a sub-millisecond timer deadline (e.g. ~0.2ms) converted to a 0ms timeout.The blocking event-loop polls take a millisecond timeout:
epoll_wait/poll(Linux) and IOCP (Windows) — all millisecond-granularity.So when the next timer was less than 1ms away, the loop was handed
timeout = 0,epoll_waitreturned immediately, and the loop busy-spun (~400k iterations/sec in the repro) until the deadline actually arrived.kqueueandio_uringuse nanosecondtimespecs and were unaffected — which is exactly why the high CPU was only reported on the epoll backend.Fix
Round these conversions up so a non-zero duration never collapses to a zero wait. The saturating add keeps
Duration.maxfrom overflowing.This eliminates the truncation regardless of timer distribution. In the repro from the issue, epoll wakeups drop ~1000× and CPU returns to normal.
Added a unit test covering the round-up behavior (sub-unit, exact, and saturation cases).
Refs #493