Skip to content

Commit

Permalink
Fix GC hole in Guid
Browse files Browse the repository at this point in the history
Found in dotnet#99130.
  • Loading branch information
MichalPetryka committed Mar 1, 2024
1 parent 24a9ec6 commit eaf046c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,10 +841,6 @@ private static bool IsHexPrefix(ReadOnlySpan<char> str, int i) =>
str[i] == '0' &&
(str[i + 1] | 0x20) == 'x';

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ReadOnlySpan<byte> AsBytes(in Guid source) =>
new ReadOnlySpan<byte>(Unsafe.AsPointer(ref Unsafe.AsRef(in source)), sizeof(Guid));

// Returns an unsigned byte array containing the GUID.
public byte[] ToByteArray()
{
Expand All @@ -856,7 +852,7 @@ public byte[] ToByteArray()
else
{
// slower path for BigEndian
Guid guid = new Guid(AsBytes(this), false);
Guid guid = new Guid(MemoryMarshal.AsBytes(new ReadOnlySpan<Guid>(this)), false);
MemoryMarshal.TryWrite(g, in guid);
}
return g;
Expand All @@ -874,7 +870,7 @@ public byte[] ToByteArray(bool bigEndian)
else
{
// slower path for Reverse
Guid guid = new Guid(AsBytes(this), bigEndian);
Guid guid = new Guid(MemoryMarshal.AsBytes(new ReadOnlySpan<Guid>(this)), bigEndian);
MemoryMarshal.TryWrite(g, in guid);
}
return g;
Expand All @@ -893,7 +889,7 @@ public bool TryWriteBytes(Span<byte> destination)
else
{
// slower path for BigEndian
Guid guid = new Guid(AsBytes(this), false);
Guid guid = new Guid(MemoryMarshal.AsBytes(new ReadOnlySpan<Guid>(this)), false);
MemoryMarshal.TryWrite(destination, in guid);
}
return true;
Expand All @@ -915,7 +911,7 @@ public bool TryWriteBytes(Span<byte> destination, bool bigEndian, out int bytesW
else
{
// slower path for Reverse
Guid guid = new Guid(AsBytes(this), bigEndian);
Guid guid = new Guid(MemoryMarshal.AsBytes(new ReadOnlySpan<Guid>(this)), bigEndian);
MemoryMarshal.TryWrite(destination, in guid);
}
bytesWritten = 16;
Expand Down

0 comments on commit eaf046c

Please sign in to comment.