Skip to content

Fix GC hole in Guid #99138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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