Skip to content

Commit

Permalink
Avoid unnecessary array allocation in JsonHelpers.Utf8GetString on ne…
Browse files Browse the repository at this point in the history
…tstandard (dotnet#92304)
  • Loading branch information
stephentoub authored Sep 20, 2023
1 parent 5883b72 commit d411f50
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,22 @@ public static void ReadWithVerify(this ref Utf8JsonReader reader)
/// <returns></returns>
public static string Utf8GetString(ReadOnlySpan<byte> bytes)
{
return Encoding.UTF8.GetString(bytes
#if !NETCOREAPP
.ToArray()
#if NETCOREAPP
return Encoding.UTF8.GetString(bytes);
#else
if (bytes.Length == 0)
{
return string.Empty;
}

unsafe
{
fixed (byte* bytesPtr = bytes)
{
return Encoding.UTF8.GetString(bytesPtr, bytes.Length);
}
}
#endif
);
}

/// <summary>
Expand Down

0 comments on commit d411f50

Please sign in to comment.