Skip to content

Commit

Permalink
Fix GC hole in Guid (#99138)
Browse files Browse the repository at this point in the history
* Fix GC hole in Guid

Found in #99130.

* Add missing in

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
MichalPetryka and stephentoub committed Mar 7, 2024
1 parent 33cdab9 commit 1e3fe51
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>(in 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>(in 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>(in 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>(in this)), bigEndian);
MemoryMarshal.TryWrite(destination, in guid);
}
bytesWritten = 16;
Expand Down

0 comments on commit 1e3fe51

Please sign in to comment.