Skip to content

std: cache mach_timebase_info on Apple#156915

Open
mysma-9403 wants to merge 1 commit into
rust-lang:mainfrom
mysma-9403:fix/apple-timebase-cache-regression
Open

std: cache mach_timebase_info on Apple#156915
mysma-9403 wants to merge 1 commit into
rust-lang:mainfrom
mysma-9403:fix/apple-timebase-cache-regression

Conversation

@mysma-9403
Copy link
Copy Markdown

@mysma-9403 mysma-9403 commented May 25, 2026

Fixes #156914

mach_timebase_info returns boot-time constants that never change for the lifetime of the process. The current code path on every Apple target invokes it on every call to Instant::into_mach_absolute_time_ceil, which is on the hot path of std::thread::sleep_until.

A cache existed for exactly this reason in #77727 (2020). It was removed in #116238 (2023) when clock_gettime replaced mach_timebase_info for Instant::now. The trap was re-introduced uncached in #151004 (2026) when sleep_until was implemented for Apple.

This PR restores the cache using OnceLock<(u32, u32)> in a private helper, scoped to the existing #[cfg(target_vendor = "apple")] gate.

No API change, no behavior change. Per-call cost on x86_64-apple-darwin / macOS 15.7.7 / rustc -O: ~3.4 ns → ~1.1 ns (10M-iter microbench in #156914). Mirrors the pattern from #77727.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 25, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

@rustbot

This comment has been minimized.

`mach_timebase_info` returns boot-time constants that never change for the
lifetime of the process. The current code path on every Apple target invokes
it on every call to `Instant::into_mach_absolute_time_ceil`, which is on the
hot path of `std::thread::sleep_until`.

A cache for exactly this value existed in the original implementation
(rust-lang/rust PR 77727, 2020). It was removed in the `clock_gettime`
migration (PR 116238, 2023) when `mach_timebase_info` stopped being used by
`Instant::now`. The trap was re-introduced uncached when `sleep_until` was
implemented for Apple (PR 151004, 2026).

Restore the cache using `OnceLock<(u32, u32)>` in a private helper, scoped
to the existing `#[cfg(target_vendor = "apple")]` gate. No API change, no
behavior change.
@mysma-9403 mysma-9403 force-pushed the fix/apple-timebase-cache-regression branch from 3390395 to d0d3861 Compare May 25, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instant::into_mach_absolute_time_ceil calls mach_timebase_info on every invocation

3 participants