Skip to content

Commit e2c64ec

Browse files
CopilotDaanV2
andcommitted
Address code review feedback: improve performance and test precision
Co-authored-by: DaanV2 <2393905+DaanV2@users.noreply.github.com>
1 parent 05dd547 commit e2c64ec

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

Library/Static Classes/V7/V7 - Const.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ public static partial class V7 {
1010

1111
private static readonly Vector128<Byte> _VersionMask = Format.VersionVariantMaskNot(V7.Version, V7.Variant);
1212
private static readonly Vector128<Byte> _VersionOverlay = Format.VersionVariantOverlayer(V7.Version, V7.Variant);
13+
14+
/// <summary>Unix epoch: January 1, 1970 00:00:00 UTC</summary>
15+
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
16+
17+
/// <summary>Maximum value that can fit in 48 bits (for RFC9562 V7 timestamp)</summary>
18+
private const UInt64 Max48BitValue = 0xFFFFFFFFFFFF;
1319
}

Library/Static Classes/V7/V7 - Extract.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ public static DateTime Extract(UUID uuid) {
2323
/// <param name="unixMs">Milliseconds since Unix epoch (1970-01-01 00:00:00 UTC)</param>
2424
/// <returns>The corresponding DateTime in UTC</returns>
2525
private static DateTime UnixMillisecondsToDateTime(UInt64 unixMs) {
26-
// Unix epoch: January 1, 1970 00:00:00 UTC
27-
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
28-
2926
try {
30-
return unixEpoch.AddMilliseconds(unixMs);
27+
return UnixEpoch.AddMilliseconds(unixMs);
3128
}
3229
catch (ArgumentOutOfRangeException) {
3330
// If the milliseconds value is too large, return MaxValue

Library/Static Classes/V7/V7 - Generate.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,11 @@ public static UUID Generate(UInt64 unixMs, UInt16 randomA, UInt64 randomB) {
8585
/// <param name="timestamp">The DateTime to convert</param>
8686
/// <returns>Milliseconds since Unix epoch (1970-01-01 00:00:00 UTC)</returns>
8787
private static UInt64 DateTimeToUnixMilliseconds(DateTime timestamp) {
88-
// Unix epoch: January 1, 1970 00:00:00 UTC
89-
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
90-
var milliseconds = (timestamp.ToUniversalTime() - unixEpoch).TotalMilliseconds;
88+
var milliseconds = (timestamp.ToUniversalTime() - UnixEpoch).TotalMilliseconds;
9189

9290
// Ensure non-negative and within 48-bit range
9391
if (milliseconds < 0) milliseconds = 0;
94-
if (milliseconds > 0xFFFFFFFFFFFF) milliseconds = 0xFFFFFFFFFFFF;
92+
if (milliseconds > Max48BitValue) milliseconds = Max48BitValue;
9593

9694
return (UInt64)milliseconds;
9795
}

Tests/RFC/RFC9562.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public void RFC9562_V6_ReorderedTime() {
8181

8282
// V6 should contain a timestamp
8383
var info = V6.Extract(uuid);
84-
Assert.True(info.Timestamp >= beforeGen.AddSeconds(-1));
85-
Assert.True(info.Timestamp <= afterGen.AddSeconds(1));
84+
// V6 uses 100-nanosecond intervals, so use tighter tolerance (100ms)
85+
Assert.True(info.Timestamp >= beforeGen.AddMilliseconds(-100));
86+
Assert.True(info.Timestamp <= afterGen.AddMilliseconds(100));
8687
}
8788

8889
[Fact(DisplayName = "RFC9562 Section 5.6 - V6 UUIDs are sortable by time")]
@@ -192,6 +193,9 @@ public void RFC9562_V7_Uniqueness() {
192193
public void RFC9562_V7_MillisecondPrecision() {
193194
// Generate multiple UUIDs within the same millisecond
194195
var uuids = new List<UUID>();
196+
var startTime = DateTime.UtcNow;
197+
198+
// Generate rapidly to likely get some in the same millisecond
195199
for (int i = 0; i < 100; i++) {
196200
uuids.Add(V7.Generate());
197201
}
@@ -205,6 +209,22 @@ public void RFC9562_V7_MillisecondPrecision() {
205209

206210
// All should be unique despite potentially being generated in same millisecond
207211
Assert.Equal(uuids.Count, uuids.Distinct().Count());
212+
213+
// Verify that UUIDs with the same timestamp have identical timestamp bits but different random bits
214+
var grouped = uuids.GroupBy(u => V7.ExtractUtc(u)).Where(g => g.Count() > 1).ToList();
215+
if (grouped.Any()) {
216+
// Found UUIDs with same millisecond timestamp
217+
var group = grouped.First();
218+
var timestampMs = group.Key;
219+
220+
// All UUIDs in this group should have same timestamp value
221+
foreach (var uuid in group) {
222+
Assert.Equal(timestampMs, V7.ExtractUtc(uuid));
223+
}
224+
225+
// But all UUIDs should be unique (different random portions)
226+
Assert.Equal(group.Count(), group.Distinct().Count());
227+
}
208228
}
209229

210230
// ========== V8 Specific Tests (RFC 9562 Section 5.8) ==========

0 commit comments

Comments
 (0)