Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace most uses of quanta::Instant with std::time::Instant to increase the accuracy of time measurements #481

Merged
merged 4 commits into from
Jan 5, 2025

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Jan 5, 2025

This PR does the following:

  • Replace most uses of quanta::Instant with std::time::Instant to increase the accuracy of time measurements.
    • When the quanta feature of moka is disabled, std::time::Instant is used for all time measurements.
  • Refactor time-related code to reduce memory overhead when using std::time::Instant.
    • Now the memory overhead of using std::time::Instant is the same as using quanta::Instant.
    • moka keeps three timestamps for each cache entry. (last accessed time, last modified time, and expiration time)
    • Now each timestamp is stored as AtomicU64 (8 bytes) in nanoseconds.
    • Before, each timestamp was stored as Mutex<std::time::Instant>, which is 24 bytes on Rust 1.83 for Linux x86_64 target.
  • To avoid unexpected panics on some platforms, use saturated arithmetic for time calculations whenever possible.

quanta is a high-performance time measurement library and will be great for some use cases such as collecting performance metrics. However, it is not very suitable for tracking expiration times of cache entries because the time measurement may drift over time, and it may not be monotonic on some specific hardware (#472). Therefore, using std::time::Instant will be more appropriate for tracking expiration times of cache entries.

(I created PR #482 to disable the quanta feature of moka by default)

@tatsuya6502 tatsuya6502 self-assigned this Jan 5, 2025
@tatsuya6502 tatsuya6502 added this to the v0.12.10 milestone Jan 5, 2025
Copy link

codecov bot commented Jan 5, 2025

Codecov Report

Attention: Patch coverage is 96.91011% with 11 lines in your changes missing coverage. Please review.

Project coverage is 94.11%. Comparing base (dd17cc1) to head (5d03e02).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #481      +/-   ##
==========================================
- Coverage   94.18%   94.11%   -0.07%     
==========================================
  Files          44       43       -1     
  Lines       20558    20486      -72     
==========================================
- Hits        19362    19281      -81     
- Misses       1196     1205       +9     

@tatsuya6502 tatsuya6502 force-pushed the increase-time-accuracy branch from e681c1f to 668bdc0 Compare January 5, 2025 07:22
Base automatically changed from portable-atomic to main January 5, 2025 09:11
@tatsuya6502 tatsuya6502 marked this pull request as ready for review January 5, 2025 09:12
@tatsuya6502
Copy link
Member Author

I ran short benchmarks using mokabench on moka@v0.12.9 and one on disable-quanta-by-default branch. I ran them on my home computers; Linux x86_64 (WSL2) and macOS arm64. I do not see any meaningful performance difference between moka@v0.12.9 and one on disable-quanta-by-default branch. (I only used the default features of moka, so quanta was disabled on the benchmark using disable-quanta-by-default branch)

mokabench-linux-x86_64

mokabench-macos-arm64

Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging.

@tatsuya6502 tatsuya6502 merged commit 9764df5 into main Jan 5, 2025
43 checks passed
@tatsuya6502 tatsuya6502 deleted the increase-time-accuracy branch January 5, 2025 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant