Skip to content

Commit

Permalink
Fix JSON perf regressions reported by #73242. (#73306)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis authored Aug 3, 2022
1 parent bcdcaec commit 5ed2442
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
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);
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

0 comments on commit 5ed2442

Please sign in to comment.