feat: return nanoseconds from monotonic_clock_stop#1
feat: return nanoseconds from monotonic_clock_stop#1bikallem wants to merge 2 commits intomoonbitlang:mainfrom
Conversation
Change `moonbit_monotonic_clock_stop` to return elapsed time in nanoseconds instead of microseconds. This gives `moon bench` the resolution needed to measure sub-microsecond operations accurately. Windows: multiply by 1_000_000_000 instead of 1_000_000. Linux/macOS: compute `tv_sec * 1_000_000_000 + tv_nsec` directly instead of converting nanoseconds to microseconds. Coordinated with: - moonbitlang/core: update bench library thresholds and conversion factors - moonbitlang/moon: update display code to use ns as base unit
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 64d1a158f7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
runtime.c
Outdated
| return (double)((counter.QuadPart - ts->ts.QuadPart) * 1000000000LL) / | ||
| freq.QuadPart; |
There was a problem hiding this comment.
Avoid int64 overflow in Windows ns conversion
The Windows implementation now computes (counter.QuadPart - ts->ts.QuadPart) * 1000000000LL in 64-bit integer arithmetic before casting to double, which can overflow for ordinary long-running intervals (e.g., around 15 minutes at a 10 MHz performance counter) and produce corrupted elapsed times. This is a regression from the previous microsecond scaling, which had a much larger safe range. Convert to floating-point before scaling (or reorder operations) so the intermediate does not overflow.
Useful? React with 👍 / 👎.
Cast tick delta to double before scaling by 1e9 to prevent integer overflow for intervals longer than ~15 minutes at typical 10 MHz performance counter frequencies.
Summary
Change
moonbit_monotonic_clock_stopto return elapsed time in nanoseconds instead of microseconds. This givesmoon benchthe resolution needed to measure sub-microsecond operations accurately.Changes
Windows (
QueryPerformanceCounter):doublebefore scaling by1e9to avoid int64 overflow for intervals longer than ~15 minutesLinux/macOS (
clock_gettime):tv_sec * 1_000_000_000LL + tv_nsecdirectly, instead oftv_sec * 1_000_000 + tv_nsec / 1000.0Merge order
These three PRs must land in this order:
nstier toauto_select_unitBefore / After
Before (µs resolution):
After (ns resolution):
Test plan
moon benchshows nanosecond values for fast benchmarks on native targetdoublecast to avoid overflow)clock_gettimepath returns correct elapsed nanoseconds