Skip to content
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

Avoid string allocation and improve performance of JsonProperty.WriteTo #90074

Merged
merged 13 commits into from
Sep 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,55 @@ private static void ClearAndReturn(ArraySegment<byte> rented)
}
}

internal void WritePropertyName(int index, Utf8JsonWriter writer)
{
CheckNotDisposed();

WritePropertyName(_parsedData.Get(index - DbRow.Size), writer);
}

private void WritePropertyNameNew(in DbRow row, Utf8JsonWriter writer)
{
// To be determined.
// This method is ~10% faster than the original WritePropertyName
// when the property name contains escaped/Unicode characters.

Debug.Assert(row.TokenType == JsonTokenType.PropertyName);
int loc = row.Location;
int length = row.SizeOrLength;
ReadOnlySpan<byte> rawName = _utf8Json.Slice(loc, length).Span;

int firstBackSlashIndex = rawName.IndexOf(JsonConstants.BackSlash);

if (firstBackSlashIndex < 0)
{
writer.WritePropertyName(rawName);
return;
}

// If the name needs unescaping

ArraySegment<byte> rented = default;

Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(rented = new ArraySegment<byte>(ArrayPool<byte>.Shared.Rent(length), 0, length));

try
{
int written = 0;

JsonReaderHelper.Unescape(rawName, utf8Unescaped, firstBackSlashIndex, out written);
ReadOnlySpan<byte> propertyName = utf8Unescaped.Slice(0, written);

writer.WritePropertyName(propertyName);
}
finally
{
ClearAndReturn(rented);
}
}

private void WritePropertyName(in DbRow row, Utf8JsonWriter writer)
{
ArraySegment<byte> rented = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,13 @@ public void WriteTo(Utf8JsonWriter writer)
_parent.WriteElementTo(_idx, writer);
}

internal void WritePropertyNameTo(Utf8JsonWriter writer)
{
CheckValidInstance();

_parent.WritePropertyName(_idx, writer);
}

/// <summary>
/// Get an enumerator to enumerate the values in the JSON array represented by this JsonElement.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ public void WriteTo(Utf8JsonWriter writer)
ThrowHelper.ThrowArgumentNullException(nameof(writer));
}

writer.WritePropertyName(Name);
if (_name is null)
{
Value.WritePropertyNameTo(writer);
}
else
{
writer.WritePropertyName(_name);
}

Value.WriteTo(writer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public static void WriteSimpleObject()
}
}

[Fact]
public static void WriteEscapedNames()
{
var buffer = new ArrayBufferWriter<byte>(1024);
const string json = """{"q\t\\mm\t":1,"":2}""";
using (JsonDocument doc = JsonDocument.Parse(json))
{
using var writer = new Utf8JsonWriter(buffer);
writer.WriteStartObject();
foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
{
prop.WriteTo(writer);
}
writer.WriteEndObject();
writer.Flush();

AssertContents(json, buffer);
}
}
private static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
{
Assert.Equal(
Expand Down