Skip to content

Commit f7e6e04

Browse files
committed
Use MemoryMarshal.GetReference.
"If in your code by construction you know that the span will not be empty, you can choose to instead use MemoryMarshal.GetReference, which performs the same operation but without the length check." https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-core-3-0/
1 parent a1d32f9 commit f7e6e04

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/MySqlConnector/Utilities/Utility.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ public static string GetString(this Encoding encoding, ReadOnlySpan<byte> span)
3838
#else
3939
unsafe
4040
{
41-
fixed (byte* ptr = span)
41+
fixed (byte* ptr = &MemoryMarshal.GetReference(span))
4242
return encoding.GetString(ptr, span.Length);
4343
}
4444
#endif
4545
}
4646

4747
public static unsafe void GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes)
4848
{
49-
fixed (char* charsPtr = chars)
50-
fixed (byte* bytesPtr = bytes)
49+
fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
50+
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
5151
{
5252
encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
5353
}
@@ -57,8 +57,8 @@ public static unsafe void GetBytes(this Encoding encoding, ReadOnlySpan<char> ch
5757
#if NET461 || NET471 || NETSTANDARD2_0
5858
public static unsafe void Convert(this Encoder encoder, ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
5959
{
60-
fixed (char* charsPtr = chars)
61-
fixed (byte* bytesPtr = bytes)
60+
fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
61+
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
6262
{
6363
// MemoryMarshal.GetNonNullPinnableReference is internal, so fake it by using an invalid but non-null pointer; this
6464
// prevents Convert from throwing an exception when the output buffer is empty

0 commit comments

Comments
 (0)