Conversation
Measuring the current time with std::time::Instant comes with quite some overhead. On x86, we can use the TSC (Time Stamp Counter) register instead, which can be read out with a single machine instruction. This replaces all uses of `std::time::Instant` with `fastant::time::Instant` from the [fastant crate](https://crates.io/crates/fastant).
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.
This should probably not be merged, but I wanted to report this somewhere.
When profiling strobealign on my laptop, I noticed that measuring elapsed time (by calling
std::time::Instant::now()andstd::time::Instant::elapsed()) took up about 5% of the total runtime. On x86, it is possible to measure elapsed time using a special machine instruction (RDTSC) that reads out the TSC (Time Stamp Counter) register, which is very fast.The fastant crate provides a drop-in replacement for
std::time::Instantthat measures time in that way.This PR replaces all uses of
std::time::Instantwithfastant::time::Instant.Now the weird part: On my laptop, this made strobealign about 5% faster, but on my desktop PC, I measure no difference at all (both have similar CPUs). Even the profiler output is clearly different.
std::time::Instant::now()is documented to useclock_gettime, which is part of the C library. My hypothesis is that on my desktop PC,clock_gettimeactually usesRDTSCand is therefore fast, but that the laptop uses a slower implementation. I’ll need to check this when I have access to the laptop again.