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

Fix JSON perf regressions reported by #73242. #73306

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,8 @@ private static async IAsyncEnumerable<TValue> CreateAsyncEnumerableDeserializer<
Debug.Assert(queueTypeInfo.IsConfigured);
JsonSerializerOptions options = queueTypeInfo.Options;
var bufferState = new ReadBufferState(options.DefaultBufferSize);
ReadStack readStack = new ReadStack
{
SupportContinuation = true
};

readStack.Initialize(queueTypeInfo);
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
ReadStack readStack = default;
readStack.Initialize(queueTypeInfo, supportContinuation: true);

var jsonReaderState = new JsonReaderState(options.GetReaderOptions());

Expand Down Expand Up @@ -475,11 +471,8 @@ private static async IAsyncEnumerable<TValue> CreateAsyncEnumerableDeserializer<
Debug.Assert(jsonTypeInfo.IsConfigured);
JsonSerializerOptions options = jsonTypeInfo.Options;
var bufferState = new ReadBufferState(options.DefaultBufferSize);
ReadStack readStack = new ReadStack
{
SupportContinuation = true
};
readStack.Initialize(jsonTypeInfo);
ReadStack readStack = default;
readStack.Initialize(jsonTypeInfo, supportContinuation: true);
var jsonReaderState = new JsonReaderState(options.GetReaderOptions());

try
Expand Down Expand Up @@ -508,11 +501,8 @@ private static async IAsyncEnumerable<TValue> CreateAsyncEnumerableDeserializer<
Debug.Assert(jsonTypeInfo.IsConfigured);
JsonSerializerOptions options = jsonTypeInfo.Options;
var bufferState = new ReadBufferState(options.DefaultBufferSize);
ReadStack readStack = new ReadStack
{
SupportContinuation = true
};
readStack.Initialize(jsonTypeInfo);
ReadStack readStack = default;
readStack.Initialize(jsonTypeInfo, supportContinuation: true);
var jsonReaderState = new JsonReaderState(options.GetReaderOptions());

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,10 @@ private static async Task WriteStreamAsync<TValue>(
using (var bufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize))
using (var writer = new Utf8JsonWriter(bufferWriter, writerOptions))
{
WriteStack state = new WriteStack
{
CancellationToken = cancellationToken,
SupportContinuation = true,
SupportAsync = true,
};

WriteStack state = default;
jsonTypeInfo = ResolvePolymorphicTypeInfo(value, jsonTypeInfo, out state.IsPolymorphicRootValue);
state.Initialize(jsonTypeInfo);
state.Initialize(jsonTypeInfo, supportAsync: true, supportContinuation: true);
state.CancellationToken = cancellationToken;

bool isFinalBlock;

Expand Down Expand Up @@ -395,13 +390,9 @@ private static void WriteStream<TValue>(
using (var bufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize))
using (var writer = new Utf8JsonWriter(bufferWriter, writerOptions))
{
WriteStack state = new WriteStack
{
SupportContinuation = true
};

WriteStack state = default;
jsonTypeInfo = ResolvePolymorphicTypeInfo(value, jsonTypeInfo, out state.IsPolymorphicRootValue);
state.Initialize(jsonTypeInfo);
state.Initialize(jsonTypeInfo, supportContinuation: true, supportAsync: false);

bool isFinalBlock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void EnsurePushCapacity()
}
}

internal void Initialize(JsonTypeInfo jsonTypeInfo)
internal void Initialize(JsonTypeInfo jsonTypeInfo, bool supportContinuation = false)
{
JsonSerializerOptions options = jsonTypeInfo.Options;
if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.Preserve)
Expand All @@ -106,6 +106,7 @@ internal void Initialize(JsonTypeInfo jsonTypeInfo)
Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
Current.NumberHandling = Current.JsonPropertyInfo.EffectiveNumberHandling;
Current.CanContainMetadata = PreserveReferences || jsonTypeInfo.PolymorphicTypeResolver?.UsesTypeDiscriminators == true;
SupportContinuation = supportContinuation;
}

public void Push()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ private void EnsurePushCapacity()
}
}

internal void Initialize(JsonTypeInfo jsonTypeInfo)
internal void Initialize(JsonTypeInfo jsonTypeInfo, bool supportContinuation = false, bool supportAsync = false)
{
Debug.Assert(!supportAsync || supportContinuation, "supportAsync must imply supportContinuation");
Debug.Assert(!IsContinuation);
Debug.Assert(CurrentDepth == 0);

Current.JsonTypeInfo = jsonTypeInfo;
Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
Current.NumberHandling = Current.JsonPropertyInfo.EffectiveNumberHandling;
SupportContinuation = supportContinuation;
SupportAsync = supportAsync;

JsonSerializerOptions options = jsonTypeInfo.Options;
if (options.ReferenceHandlingStrategy != ReferenceHandlingStrategy.None)
Expand Down