Skip to content

Commit b19ca28

Browse files
committed
apply suggestion from comments
1 parent 9b5714a commit b19ca28

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

src/NHibernate/Id/GuidCombGenerator.cs

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,60 +43,28 @@ public object Generate(ISessionImplementor session, object obj)
4343
/// <summary>
4444
/// Generate a new <see cref="Guid"/> using the comb algorithm.
4545
/// </summary>
46-
protected static Guid GenerateComb(in Guid guid, DateTime utcNow)
46+
protected static Guid GenerateComb(Guid guid, DateTime utcNow)
4747
{
48-
#if NET8_0_OR_GREATER
48+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
4949
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);
7751
#else
78-
79-
byte[] guidArray = guid.ToByteArray();
80-
52+
var guidArray = guid.ToByteArray();
53+
#endif
8154
// 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+
8660
// 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;
9766

9867
return new Guid(guidArray);
99-
#endif
10068
}
10169

10270
#endregion

0 commit comments

Comments
 (0)