|
1 |
| -using System; |
2 |
| -using System.Diagnostics; |
3 |
| -using System.Runtime.CompilerServices; |
4 |
| -using BitFaster.Caching.Lru; |
5 |
| - |
6 |
| -namespace BitFaster.Caching |
7 |
| -{ |
8 |
| - /// <summary> |
9 |
| - /// Represents a fixed length of time. |
10 |
| - /// </summary> |
11 |
| - /// <remarks> |
12 |
| - /// This struct is used to abstract away the use of different time sources with different precision. |
13 |
| - /// This enables use of native time values (which may be ticks or millisecs), only converting |
14 |
| - /// to TimeSpan for non perf critical user code. Using long without a mul/div makes cache lookups |
15 |
| - /// about 30% faster on .NET6. |
16 |
| - /// </remarks> |
17 |
| - [DebuggerDisplay("{ToTimeSpan()}")] |
18 |
| - public readonly struct Duration |
19 |
| - { |
20 |
| - internal readonly long raw; |
21 |
| - |
22 |
| - // MacOS Stopwatch adjustment factor is 100, giving lowest maximum TTL on mac platform - use same upper limit on all platforms for consistency |
23 |
| - // this also avoids overflow when multipling long.MaxValue by 1.0 |
24 |
| - internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks((long)(long.MaxValue / 100.0d)); |
25 |
| - |
26 |
| - internal static readonly Duration Zero = new Duration(0); |
27 |
| - |
28 |
| - internal Duration(long raw) |
29 |
| - { |
30 |
| - this.raw = raw; |
31 |
| - } |
32 |
| - |
33 |
| - /// <summary> |
34 |
| - /// Gets the time since the system epoch. |
35 |
| - /// </summary> |
36 |
| - /// <returns>A duration</returns> |
37 |
| - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
38 |
| - public static Duration SinceEpoch() |
39 |
| - { |
40 |
| -#if NETCOREAPP3_0_OR_GREATER |
41 |
| - return new Duration(Environment.TickCount64); |
42 |
| -#else |
43 |
| - return new Duration(Stopwatch.GetTimestamp()); |
44 |
| -#endif |
45 |
| - } |
46 |
| - |
47 |
| - /// <summary> |
48 |
| - /// Converts the duration to a TimeSpan. |
49 |
| - /// </summary> |
50 |
| - /// <returns></returns> |
51 |
| - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
52 |
| - public TimeSpan ToTimeSpan() |
53 |
| - { |
54 |
| -#if NETCOREAPP3_0_OR_GREATER |
55 |
| - return TimeSpan.FromMilliseconds(raw); |
56 |
| -#else |
57 |
| - return StopwatchTickConverter.FromTicks(raw); |
58 |
| -#endif |
59 |
| - } |
60 |
| - |
61 |
| - /// <summary> |
62 |
| - /// Returns a Duration that represents a specified TimeSpan. |
63 |
| - /// </summary> |
64 |
| - /// <param name="timeSpan">The TimeSpan to convert.</param> |
65 |
| - /// <returns>A duration.</returns> |
66 |
| - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
67 |
| - public static Duration FromTimeSpan(TimeSpan timeSpan) |
68 |
| - { |
69 |
| -#if NETCOREAPP3_0_OR_GREATER |
70 |
| - return new Duration((long)timeSpan.TotalMilliseconds); |
71 |
| -#else |
72 |
| - return new Duration(StopwatchTickConverter.ToTicks(timeSpan)); |
73 |
| -#endif |
74 |
| - } |
75 |
| - |
76 |
| - /// <summary> |
77 |
| - /// Returns a Duration that represents a specified number of milliseconds. |
78 |
| - /// </summary> |
79 |
| - /// <param name="value">A number of milliseconds</param> |
80 |
| - /// <returns></returns> |
81 |
| - public static Duration FromMilliseconds(double value) |
82 |
| - { |
83 |
| - return FromTimeSpan(TimeSpan.FromMilliseconds(value)); |
84 |
| - } |
85 |
| - |
86 |
| - /// <summary> |
87 |
| - /// Returns a Duration that represents a specified number of seconds. |
88 |
| - /// </summary> |
89 |
| - /// <param name="value">A number of seconds</param> |
90 |
| - /// <returns></returns> |
91 |
| - public static Duration FromSeconds(double value) |
92 |
| - { |
93 |
| - return FromTimeSpan(TimeSpan.FromSeconds(value)); |
94 |
| - } |
95 |
| - |
96 |
| - /// <summary> |
97 |
| - /// Returns a Duration that represents a specified number of minutes. |
98 |
| - /// </summary> |
99 |
| - /// <param name="value">A number of minutes</param> |
100 |
| - /// <returns></returns> |
101 |
| - public static Duration FromMinutes(double value) |
102 |
| - { |
103 |
| - return FromTimeSpan(TimeSpan.FromMinutes(value)); |
104 |
| - } |
105 |
| - |
106 |
| - /// <summary> |
107 |
| - /// Returns a Duration that represents a specified number of hours. |
108 |
| - /// </summary> |
109 |
| - /// <param name="value">A number of hours</param> |
110 |
| - /// <returns></returns> |
111 |
| - public static Duration FromHours(double value) |
112 |
| - { |
113 |
| - return FromTimeSpan(TimeSpan.FromHours(value)); |
114 |
| - } |
115 |
| - |
116 |
| - /// <summary> |
117 |
| - /// Returns a Duration that represents a specified number of days. |
118 |
| - /// </summary> |
119 |
| - /// <param name="value">A number of days</param> |
120 |
| - /// <returns></returns> |
121 |
| - public static Duration FromDays(double value) |
122 |
| - { |
123 |
| - return FromTimeSpan(TimeSpan.FromDays(value)); |
124 |
| - } |
125 |
| - |
126 |
| - /// <summary> |
127 |
| - /// Adds two specified Duration instances. |
128 |
| - /// </summary> |
129 |
| - /// <param name="a">The first duration to add.</param> |
130 |
| - /// <param name="b">The second duration to add.</param> |
131 |
| - /// <returns>An duration whose value is the sum of the values of a and b.</returns> |
132 |
| - public static Duration operator +(Duration a, Duration b) => new Duration(a.raw + b.raw); |
133 |
| - |
134 |
| - /// <summary> |
135 |
| - /// Subtracts a specified Duration from another specified Duration. |
136 |
| - /// </summary> |
137 |
| - /// <param name="a">The minuend.</param> |
138 |
| - /// <param name="b">The subtrahend.</param> |
139 |
| - /// <returns>An duration whose value is the result of the value of a minus the value of b.</returns> |
140 |
| - public static Duration operator -(Duration a, Duration b) => new Duration(a.raw - b.raw); |
141 |
| - |
142 |
| - /// <summary> |
143 |
| - /// Returns a value that indicates whether a specified Duration is greater than another specified Duration. |
144 |
| - /// </summary> |
145 |
| - /// <param name="a">The first duration to compare.</param> |
146 |
| - /// <param name="b">The second duration to compare.</param> |
147 |
| - /// <returns>true if the value of a is greater than the value of b; otherwise, false.</returns> |
148 |
| - public static bool operator >(Duration a, Duration b) => a.raw > b.raw; |
149 |
| - |
150 |
| - /// <summary> |
151 |
| - /// Returns a value that indicates whether a specified Duration is less than another specified Duration. |
152 |
| - /// </summary> |
153 |
| - /// <param name="a">The first duration to compare.</param> |
154 |
| - /// <param name="b">The second duration to compare.</param> |
155 |
| - /// <returns>true if the value of a is less than the value of b; otherwise, false.</returns> |
156 |
| - public static bool operator <(Duration a, Duration b) => a.raw < b.raw; |
157 |
| - } |
158 |
| -} |
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using BitFaster.Caching.Lru; |
| 6 | + |
| 7 | +namespace BitFaster.Caching |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a fixed length of time. |
| 11 | + /// </summary> |
| 12 | + /// <remarks> |
| 13 | + /// This struct is used to abstract away the use of different time sources with different precision. |
| 14 | + /// This enables use of native time values (which may be ticks or millisecs), only converting |
| 15 | + /// to TimeSpan for non perf critical user code. Using long without a mul/div makes cache lookups |
| 16 | + /// about 30% faster on .NET6. |
| 17 | + /// </remarks> |
| 18 | + [DebuggerDisplay("{ToTimeSpan()}")] |
| 19 | + public readonly struct Duration |
| 20 | + { |
| 21 | + internal readonly long raw; |
| 22 | + |
| 23 | + // MacOS Stopwatch adjustment factor is 100, giving lowest maximum TTL on mac platform - use same upper limit on all platforms for consistency |
| 24 | + // this also avoids overflow when multipling long.MaxValue by 1.0 |
| 25 | + internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks((long)(long.MaxValue / 100.0d)); |
| 26 | + |
| 27 | + internal static readonly Duration Zero = new Duration(0); |
| 28 | + |
| 29 | + internal Duration(long raw) |
| 30 | + { |
| 31 | + this.raw = raw; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the time since the system epoch. |
| 36 | + /// </summary> |
| 37 | + /// <returns>A duration</returns> |
| 38 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 39 | + public static Duration SinceEpoch() |
| 40 | + { |
| 41 | +#if NETCOREAPP3_0_OR_GREATER |
| 42 | + return new Duration(Environment.TickCount64); |
| 43 | +#else |
| 44 | + return new Duration(Stopwatch.GetTimestamp()); |
| 45 | +#endif |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Converts the duration to a TimeSpan. |
| 50 | + /// </summary> |
| 51 | + /// <returns></returns> |
| 52 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 53 | + public TimeSpan ToTimeSpan() |
| 54 | + { |
| 55 | +#if NETCOREAPP3_0_OR_GREATER |
| 56 | + return TimeSpan.FromMilliseconds(raw); |
| 57 | +#else |
| 58 | + return StopwatchTickConverter.FromTicks(raw); |
| 59 | +#endif |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Returns a Duration that represents a specified TimeSpan. |
| 64 | + /// </summary> |
| 65 | + /// <param name="timeSpan">The TimeSpan to convert.</param> |
| 66 | + /// <returns>A duration.</returns> |
| 67 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 68 | + public static Duration FromTimeSpan(TimeSpan timeSpan) |
| 69 | + { |
| 70 | +#if NETCOREAPP3_0_OR_GREATER |
| 71 | + return new Duration((long)timeSpan.TotalMilliseconds); |
| 72 | +#else |
| 73 | + return new Duration(StopwatchTickConverter.ToTicks(timeSpan)); |
| 74 | +#endif |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Returns a Duration that represents a specified number of milliseconds. |
| 79 | + /// </summary> |
| 80 | + /// <param name="value">A number of milliseconds</param> |
| 81 | + /// <returns></returns> |
| 82 | + public static Duration FromMilliseconds(double value) |
| 83 | + { |
| 84 | + return FromTimeSpan(TimeSpan.FromMilliseconds(value)); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Returns a Duration that represents a specified number of seconds. |
| 89 | + /// </summary> |
| 90 | + /// <param name="value">A number of seconds</param> |
| 91 | + /// <returns></returns> |
| 92 | + public static Duration FromSeconds(double value) |
| 93 | + { |
| 94 | + return FromTimeSpan(TimeSpan.FromSeconds(value)); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Returns a Duration that represents a specified number of minutes. |
| 99 | + /// </summary> |
| 100 | + /// <param name="value">A number of minutes</param> |
| 101 | + /// <returns></returns> |
| 102 | + public static Duration FromMinutes(double value) |
| 103 | + { |
| 104 | + return FromTimeSpan(TimeSpan.FromMinutes(value)); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Returns a Duration that represents a specified number of hours. |
| 109 | + /// </summary> |
| 110 | + /// <param name="value">A number of hours</param> |
| 111 | + /// <returns></returns> |
| 112 | + public static Duration FromHours(double value) |
| 113 | + { |
| 114 | + return FromTimeSpan(TimeSpan.FromHours(value)); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Returns a Duration that represents a specified number of days. |
| 119 | + /// </summary> |
| 120 | + /// <param name="value">A number of days</param> |
| 121 | + /// <returns></returns> |
| 122 | + public static Duration FromDays(double value) |
| 123 | + { |
| 124 | + return FromTimeSpan(TimeSpan.FromDays(value)); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Adds two specified Duration instances. |
| 129 | + /// </summary> |
| 130 | + /// <param name="a">The first duration to add.</param> |
| 131 | + /// <param name="b">The second duration to add.</param> |
| 132 | + /// <returns>An duration whose value is the sum of the values of a and b.</returns> |
| 133 | + public static Duration operator +(Duration a, Duration b) => new Duration(a.raw + b.raw); |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Subtracts a specified Duration from another specified Duration. |
| 137 | + /// </summary> |
| 138 | + /// <param name="a">The minuend.</param> |
| 139 | + /// <param name="b">The subtrahend.</param> |
| 140 | + /// <returns>An duration whose value is the result of the value of a minus the value of b.</returns> |
| 141 | + public static Duration operator -(Duration a, Duration b) => new Duration(a.raw - b.raw); |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Returns a value that indicates whether a specified Duration is greater than another specified Duration. |
| 145 | + /// </summary> |
| 146 | + /// <param name="a">The first duration to compare.</param> |
| 147 | + /// <param name="b">The second duration to compare.</param> |
| 148 | + /// <returns>true if the value of a is greater than the value of b; otherwise, false.</returns> |
| 149 | + public static bool operator >(Duration a, Duration b) => a.raw > b.raw; |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Returns a value that indicates whether a specified Duration is less than another specified Duration. |
| 153 | + /// </summary> |
| 154 | + /// <param name="a">The first duration to compare.</param> |
| 155 | + /// <param name="b">The second duration to compare.</param> |
| 156 | + /// <returns>true if the value of a is less than the value of b; otherwise, false.</returns> |
| 157 | + public static bool operator <(Duration a, Duration b) => a.raw < b.raw; |
| 158 | + } |
| 159 | +} |
0 commit comments