@@ -43,60 +43,28 @@ public object Generate(ISessionImplementor session, object obj)
43
43
/// <summary>
44
44
/// Generate a new <see cref="Guid"/> using the comb algorithm.
45
45
/// </summary>
46
- protected static Guid GenerateComb ( in Guid guid , DateTime utcNow )
46
+ protected static Guid GenerateComb ( Guid guid , DateTime utcNow )
47
47
{
48
- #if NET8_0_OR_GREATER
48
+ #if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
49
49
Span < byte > guidArray = stackalloc byte [ 16 ] ;
50
- Span < byte > msecsArray = stackalloc byte [ sizeof ( long ) ] ;
51
- Span < byte > daysArray = stackalloc byte [ sizeof ( int ) ] ;
52
-
53
- var bytesWritten = guid . TryWriteBytes ( guidArray ) ;
54
- Debug . Assert ( bytesWritten ) ;
55
-
56
- // Get the days and milliseconds which will be used to build the byte string
57
- TimeSpan days = new TimeSpan ( utcNow . Ticks - BaseDateTicks ) ;
58
- TimeSpan msecs = utcNow . TimeOfDay ;
59
-
60
- // Convert to a byte array
61
- // Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
62
-
63
- bytesWritten = BitConverter . TryWriteBytes ( daysArray , days . Days )
64
- && BitConverter . TryWriteBytes ( msecsArray , ( long ) ( msecs . TotalMilliseconds / 3.333333 ) ) ;
65
- Debug . Assert ( bytesWritten ) ;
66
-
67
- msecsArray . Reverse ( ) ;
68
-
69
- // Copy the bytes into the guid
70
- //Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
71
- guidArray [ 10 ] = daysArray [ 1 ] ;
72
- guidArray [ 11 ] = daysArray [ 0 ] ;
73
-
74
- //Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
75
- msecsArray [ ^ 4 ..] . CopyTo ( guidArray [ ^ 4 ..] ) ;
76
- return new Guid ( guidArray ) ;
50
+ guid . TryWriteBytes ( guidArray ) ;
77
51
#else
78
-
79
- byte [ ] guidArray = guid . ToByteArray ( ) ;
80
-
52
+ var guidArray = guid . ToByteArray ( ) ;
53
+ #endif
81
54
// Get the days and milliseconds which will be used to build the byte string
82
- TimeSpan days = new TimeSpan ( utcNow . Ticks - BaseDateTicks ) ;
83
- TimeSpan msecs = utcNow . TimeOfDay ;
84
-
85
- // Convert to a byte array
55
+ var ts = new TimeSpan ( utcNow . Ticks - BaseDateTicks ) ;
56
+ var days = ts . Days ;
57
+ guidArray [ 10 ] = ( byte ) ( days >> 8 ) ;
58
+ guidArray [ 11 ] = ( byte ) days ;
59
+
86
60
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
87
- byte [ ] daysArray = BitConverter . GetBytes ( days . Days ) ;
88
- byte [ ] msecsArray = BitConverter . GetBytes ( ( long ) ( msecs . TotalMilliseconds / 3.333333 ) ) ;
89
-
90
- // Reverse the bytes to match SQL Servers ordering
91
- Array . Reverse ( daysArray ) ;
92
- Array . Reverse ( msecsArray ) ;
93
-
94
- // Copy the bytes into the guid
95
- Array . Copy ( daysArray , daysArray . Length - 2 , guidArray , guidArray . Length - 6 , 2 ) ;
96
- Array . Copy ( msecsArray , msecsArray . Length - 4 , guidArray , guidArray . Length - 4 , 4 ) ;
61
+ var msecs = ( long ) ( utcNow . TimeOfDay . TotalMilliseconds / 3.333333 ) ;
62
+ guidArray [ 12 ] = ( byte ) ( msecs >> 24 ) ;
63
+ guidArray [ 13 ] = ( byte ) ( msecs >> 16 ) ;
64
+ guidArray [ 14 ] = ( byte ) ( msecs >> 8 ) ;
65
+ guidArray [ 15 ] = ( byte ) msecs ;
97
66
98
67
return new Guid ( guidArray ) ;
99
- #endif
100
68
}
101
69
102
70
#endregion
0 commit comments