Skip to content

Commit 9a4be5b

Browse files
Sergio0694kasperk81jkotas
authored
Optimize '[Guid]' parsing on Native AOT (#116324)
* Optimize '[Guid]' parsing on Native AOT * Avoid copying UTF8 buffer entirely * Add missing 'IsNil' check Co-authored-by: kasperk81 <[email protected]> Co-authored-by: Jan Kotas <[email protected]>
1 parent 56a3e2d commit 9a4be5b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/NativeFormat/NativeFormatRuntimeNamedTypeInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public sealed override bool IsByRefLike
7979
continue;
8080
if (guidStringArgumentHandle.HandleType != HandleType.ConstantStringValue)
8181
continue;
82-
return new Guid(guidStringArgumentHandle.ToConstantStringValueHandle(_reader).GetString(_reader));
82+
83+
ConstantStringValueHandle constantStringValueHandle = guidStringArgumentHandle.ToConstantStringValueHandle(_reader);
84+
85+
// Parse a 'Guid' directly from the encoded UTF8 buffer, instead of round-tripping through a 'string'
86+
return Guid.Parse(_reader.ReadStringAsBytes(constantStringValueHandle));
8387
}
8488
}
8589
return null;

src/coreclr/tools/Common/Internal/Metadata/NativeFormat/NativeMetadataReader.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ internal bool StringEquals(ConstantStringValueHandle handle, string value)
220220
{
221221
return _streamReader.StringEquals((uint)handle.Offset, value);
222222
}
223+
224+
internal ReadOnlySpan<byte> ReadStringAsBytes(ConstantStringValueHandle handle)
225+
{
226+
if (handle.IsNil)
227+
{
228+
return [];
229+
}
230+
231+
return _streamReader.ReadStringAsBytes((uint)handle.Offset);
232+
}
223233
}
224234

225235
internal sealed partial class MetadataHeader

src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatReader.String.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// UTF8 string reading methods
88
// ---------------------------------------------------------------------------
99

10+
using System;
1011
using System.Text;
1112

1213
namespace Internal.NativeFormat
@@ -40,6 +41,23 @@ public string ReadString(uint offset)
4041
return value;
4142
}
4243

44+
public unsafe ReadOnlySpan<byte> ReadStringAsBytes(uint offset)
45+
{
46+
uint numBytes;
47+
offset = DecodeUnsigned(offset, out numBytes);
48+
49+
if (numBytes != 0)
50+
{
51+
uint endOffset = offset + numBytes;
52+
if (endOffset < numBytes || endOffset > _size)
53+
ThrowBadImageFormatException();
54+
55+
return new(_base + offset, (int)numBytes);
56+
}
57+
58+
return [];
59+
}
60+
4361
public unsafe uint DecodeString(uint offset, out string value)
4462
{
4563
uint numBytes;

0 commit comments

Comments
 (0)