Skip to content

Static OS check to select clock #577

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

Merged
merged 3 commits into from
May 10, 2024
Merged

Static OS check to select clock #577

merged 3 commits into from
May 10, 2024

Conversation

bitfaster
Copy link
Owner

@bitfaster bitfaster commented May 10, 2024

This brings a more noticeable gain, building on #540. The OS check is not a JIT intrinsic until .NET8, so repeated checks are a penalty. As a follow up, need to verify whether windows bench was regressed and whether the OS check is ultimately elided by the JIT.

With this fix (static readonly bool):

Method Mean Error StdDev Ratio Allocated
ConcurrentDictionary 4.023 ns 0.0114 ns 0.0089 ns 1.00 -
FastConcurrentTLru 14.995 ns 0.0046 ns 0.0036 ns 3.73 -
FastConcLruAfterAccess 17.534 ns 0.0465 ns 0.0388 ns 4.36 -
FastConcLruAfter 20.035 ns 0.0061 ns 0.0051 ns 4.98 -
ConcurrentTLru 19.744 ns 0.0047 ns 0.0037 ns 4.91 -

v2.5.0 baseline

Method Mean Error StdDev Ratio Allocated
ConcurrentDictionary 3.935 ns 0.0771 ns 0.0721 ns 1.00 -
FastConcurrentTLru 16.610 ns 0.0064 ns 0.0054 ns 4.22 -
FastConcLruAfterAccess 18.822 ns 0.0069 ns 0.0058 ns 4.79 -
FastConcLruAfter 21.149 ns 0.0166 ns 0.0139 ns 5.38 -
ConcurrentTLru 21.449 ns 0.0067 ns 0.0052 ns 5.46 -
BenchmarkDotNet v0.13.12, macOS Sonoma 14.1.1 (23B81) [Darwin 23.1.0]
Apple M2, 1 CPU, 8 logical and 8 physical cores
.NET SDK 8.0.100
  [Host]   : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD
  .NET 6.0 : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD
Job=.NET 6.0  Runtime=.NET 6.0  

@bitfaster bitfaster changed the title Static OS check Static OS check to select clock May 10, 2024
@bitfaster
Copy link
Owner Author

bitfaster commented May 10, 2024

Confirmed branch based on static variable is elided for .NET6.

Baseline:

.NET 6.0.29 (6.0.2924.17105), X64 RyuJIT AVX2

; BitFaster.Caching.Benchmarks.TimeBenchmarks.EnvironmentTickCount64()
;             return Environment.TickCount64;
;             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       jmp       near ptr System.Environment.get_TickCount64()
; Total bytes of code 5

Extern method
System.Environment.get_TickCount64()

With static bool, comparing windows disassembly:

.NET 6.0.29 (6.0.2924.17105), X64 RyuJIT AVX2

; BitFaster.Caching.Benchmarks.TimeBenchmarks.DurationSinceEpoch()
;             return Duration.SinceEpoch();
;             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       sub       rsp,28
       call      System.Environment.get_TickCount64()
       nop
       add       rsp,28
       ret
; Total bytes of code 15

Extern method
System.Environment.get_TickCount64()

Without static bool (no branch, original code):

.NET 6.0.29 (6.0.2924.17105), X64 RyuJIT AVX2

; BitFaster.Caching.Benchmarks.TimeBenchmarks.DurationSinceEpoch()
;             return Duration.SinceEpoch();
;             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       sub       rsp,28
       call      System.Environment.get_TickCount64()
       nop
       add       rsp,28
       ret
; Total bytes of code 15

Extern method
System.Environment.get_TickCount64()

@bitfaster bitfaster merged commit a825808 into main May 10, 2024
13 checks passed
@bitfaster bitfaster deleted the users/alexpeck/macos2 branch May 10, 2024 19:49
@coveralls
Copy link

Coverage Status

coverage: 99.318%. remained the same
when pulling 02c676f on users/alexpeck/macos2
into 4c91b83 on main.

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.

2 participants