Skip to content

Commit 1cb3f3f

Browse files
Remove unnecessary properties from ReadStackFrame.
1 parent 396eb0f commit 1cb3f3f

File tree

13 files changed

+35
-22
lines changed

13 files changed

+35
-22
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/JsonCollectionConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal abstract class JsonCollectionConverter<TCollection, TElement> : JsonRes
2525
/// </summary>
2626
protected virtual void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options)
2727
{
28-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
28+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
2929
{
3030
return;
3131
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/JsonDictionaryConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected virtual void ConvertCollection(ref ReadStack state, JsonSerializerOpti
4040
/// </summary>
4141
protected virtual void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state)
4242
{
43-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
43+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
4444
{
4545
return;
4646
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ListOfTConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override void Add(in TElement value, ref ReadStack state)
2020

2121
protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options)
2222
{
23-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
23+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
2424
{
2525
return;
2626
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/QueueOfTConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void Add(in TElement value, ref ReadStack state)
1818

1919
protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options)
2020
{
21-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
21+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
2222
{
2323
return;
2424
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/StackOfTConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected override void Add(in TElement value, ref ReadStack state)
1919

2020
protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options)
2121
{
22-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
22+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
2323
{
2424
return;
2525
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/StackOrQueueConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected sealed override void Add(in object? value, ref ReadStack state)
2222

2323
protected sealed override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options)
2424
{
25-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
25+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
2626
{
2727
return;
2828
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectDefaultConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert,
3232
ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
3333
}
3434

35-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
35+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
3636
{
3737
obj = state.Current.ReturnValue!;
3838
}
@@ -108,7 +108,7 @@ internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert,
108108
return true;
109109
}
110110

111-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
111+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
112112
{
113113
obj = state.Current.ReturnValue!;
114114
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal sealed override bool OnTryRead(ref Utf8JsonReader reader, Type typeToCo
4545
ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
4646
}
4747

48-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
48+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
4949
{
5050
object populatedObject = state.Current.ReturnValue!;
5151
PopulatePropertiesFastPath(populatedObject, jsonTypeInfo, options, ref reader, ref state);
@@ -157,7 +157,7 @@ internal sealed override bool OnTryRead(ref Utf8JsonReader reader, Type typeToCo
157157
// Metadata is disallowed with Populate option and therefore ordering here is irrelevant.
158158
// Since state.Current.IsPopulating is being checked early on in this method the continuation
159159
// will be handled there.
160-
if (state.Current.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
160+
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
161161
{
162162
object populatedObject = state.Current.ReturnValue!;
163163

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSeriali
281281

282282
if (parentObj != null && propertyInfo != null && !propertyInfo.IsForTypeInfo)
283283
{
284-
state.Current.ParentObject = parentObj;
285-
state.Current.ParentProperty = propertyInfo;
284+
state.Current.HasParentObject = true;
286285
}
287286

288287
success = OnTryRead(ref reader, typeToConvert, options, ref state, out value);

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,9 @@ internal bool TryGetPrePopulatedValue(scoped ref ReadStack state)
808808
return false;
809809

810810
Debug.Assert(EffectiveConverter.CanPopulate, "Property is marked with Populate but converter cannot populate. This should have been validated in Configure");
811-
Debug.Assert(state.Current.ParentObject != null, "Parent object is null");
811+
Debug.Assert(state.Parent.ReturnValue != null, "Parent object is null");
812812
Debug.Assert(!state.Current.IsPopulating, "We've called TryGetPrePopulatedValue more than once");
813-
object? value = Get!(state.Current.ParentObject);
813+
object? value = Get!(state.Parent.ReturnValue);
814814
state.Current.ReturnValue = value;
815815
state.Current.IsPopulating = value != null;
816816
return value != null;

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Buffers;
54
using System.Collections;
65
using System.Diagnostics;
76
using System.Runtime.CompilerServices;
@@ -16,10 +15,26 @@ namespace System.Text.Json
1615
internal struct ReadStack
1716
{
1817
/// <summary>
19-
/// Exposes the stackframe that is currently active.
18+
/// Exposes the stack frame that is currently active.
2019
/// </summary>
2120
public ReadStackFrame Current;
2221

22+
/// <summary>
23+
/// Gets the parent stack frame, if it exists.
24+
/// </summary>
25+
public readonly ref ReadStackFrame Parent
26+
{
27+
get
28+
{
29+
Debug.Assert(_count > 1);
30+
Debug.Assert(_stack is not null);
31+
return ref _stack[_count - 2];
32+
}
33+
}
34+
35+
public readonly JsonPropertyInfo? ParentProperty
36+
=> Current.HasParentObject ? Parent.JsonPropertyInfo : null;
37+
2338
/// <summary>
2439
/// Buffer containing all frames in the stack. For performance it is only populated for serialization depths > 1.
2540
/// </summary>

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ public JsonTypeInfo BaseJsonTypeInfo
7474
// Every required JsonPropertyInfo has RequiredPropertyIndex property which maps to an index in this BitArray.
7575
public BitArray? RequiredPropertiesSet;
7676

77-
// Needed for populating properties
78-
public object? ParentObject;
79-
public JsonPropertyInfo? ParentProperty;
77+
// Tracks state related to property population.
78+
public bool HasParentObject;
8079
public bool IsPopulating;
8180

8281
public void EndConstructorParameter()

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ internal struct WriteStack
2020
public readonly int CurrentDepth => _count;
2121

2222
/// <summary>
23-
/// Exposes the stackframe that is currently active.
23+
/// Exposes the stack frame that is currently active.
2424
/// </summary>
2525
public WriteStackFrame Current;
2626

2727
/// <summary>
28-
/// Gets the parent stackframe, if it exists.
28+
/// Gets the parent stack frame, if it exists.
2929
/// </summary>
30-
public ref WriteStackFrame Parent
30+
public readonly ref WriteStackFrame Parent
3131
{
3232
get
3333
{

0 commit comments

Comments
 (0)