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,15 @@ private static void ClearAndReturn(ArraySegment<byte> rented)
}
}

internal void WritePropertyName(int index, Utf8JsonWriter writer)
{
CheckNotDisposed();
karakasa marked this conversation as resolved.
Show resolved Hide resolved

DbRow row = _parsedData.Get(index - DbRow.Size);
Debug.Assert(row.TokenType == JsonTokenType.PropertyName);
WritePropertyName(row, writer);
}

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
Loading