Skip to content

Commit a825808

Browse files
authored
Static OS check to select clock (#577)
* static check * move epsilon * missing file ---------
1 parent 4c91b83 commit a825808

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

BitFaster.Caching.Benchmarks/TimeBenchmarks.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace BitFaster.Caching.Benchmarks
88
{
99
#if Windows
10+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
1011
[SimpleJob(RuntimeMoniker.Net48)]
1112
#endif
1213
[SimpleJob(RuntimeMoniker.Net60)]

BitFaster.Caching.UnitTests/DurationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace BitFaster.Caching.UnitTests
1010
{
1111
public class DurationTests
1212
{
13+
public static readonly ulong epsilon = (ulong)Duration.FromMilliseconds(20).raw;
14+
1315
private readonly ITestOutputHelper testOutputHelper;
1416

1517
public DurationTests(ITestOutputHelper testOutputHelper)

BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void CreateItemInitializesTimestampToNow()
5353
{
5454
var item = this.policy.CreateItem(1, 2);
5555

56-
item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, Duration.epsilon);
56+
item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon);
5757
}
5858

5959
[Fact]

BitFaster.Caching.UnitTests/Lru/DiscretePolicyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void CreateItemInitializesKeyValueAndTicks()
4848
item.Key.Should().Be(1);
4949
item.Value.Should().Be(2);
5050

51-
item.TickCount.Should().BeCloseTo(timeToExpire.raw + Duration.SinceEpoch().raw, Duration.epsilon);
51+
item.TickCount.Should().BeCloseTo(timeToExpire.raw + Duration.SinceEpoch().raw, DurationTests.epsilon);
5252
}
5353

5454
[Fact]

BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void CreateItemInitializesTimestampToNow()
5656
{
5757
var item = this.policy.CreateItem(1, 2);
5858

59-
item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, Duration.epsilon);
59+
item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon);
6060
}
6161

6262
[Fact]

BitFaster.Caching/Duration.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public readonly struct Duration
2626

2727
internal static readonly Duration Zero = new Duration(0);
2828

29-
internal static readonly ulong epsilon = (ulong)Duration.FromMilliseconds(20).raw;
29+
#if NETCOREAPP3_0_OR_GREATER
30+
private static readonly bool IsMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
31+
#endif
3032

3133
internal Duration(long raw)
3234
{
@@ -41,7 +43,7 @@ internal Duration(long raw)
4143
public static Duration SinceEpoch()
4244
{
4345
#if NETCOREAPP3_0_OR_GREATER
44-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
46+
if (IsMacOS)
4547
{
4648
return new Duration(Stopwatch.GetTimestamp());
4749
}
@@ -61,8 +63,8 @@ public static Duration SinceEpoch()
6163
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6264
public TimeSpan ToTimeSpan()
6365
{
64-
#if NETCOREAPP3_0_OR_GREATER
65-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
66+
#if NETCOREAPP3_0_OR_GREATER
67+
if (IsMacOS)
6668
{
6769
return StopwatchTickConverter.FromTicks(raw);
6870
}
@@ -72,7 +74,7 @@ public TimeSpan ToTimeSpan()
7274
}
7375
#else
7476
return StopwatchTickConverter.FromTicks(raw);
75-
#endif
77+
#endif
7678
}
7779

7880
/// <summary>
@@ -84,7 +86,7 @@ public TimeSpan ToTimeSpan()
8486
public static Duration FromTimeSpan(TimeSpan timeSpan)
8587
{
8688
#if NETCOREAPP3_0_OR_GREATER
87-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
89+
if (IsMacOS)
8890
{
8991
return new Duration(StopwatchTickConverter.ToTicks(timeSpan));
9092
}
@@ -94,7 +96,7 @@ public static Duration FromTimeSpan(TimeSpan timeSpan)
9496
}
9597
#else
9698
return new Duration(StopwatchTickConverter.ToTicks(timeSpan));
97-
#endif
99+
#endif
98100
}
99101

100102
/// <summary>

0 commit comments

Comments
 (0)