std: cache mach_timebase_info on Apple#156915
Open
mysma-9403 wants to merge 1 commit into
Open
Conversation
Collaborator
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
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.
3390395 to
d0d3861
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #156914
mach_timebase_inforeturns 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 toInstant::into_mach_absolute_time_ceil, which is on the hot path ofstd::thread::sleep_until.A cache existed for exactly this reason in #77727 (2020). It was removed in #116238 (2023) when
clock_gettimereplacedmach_timebase_infoforInstant::now. The trap was re-introduced uncached in #151004 (2026) whensleep_untilwas 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.