- Notifications
You must be signed in to change notification settings - Fork5.2k
Improve throughput / allocations of JsonNode.GetPath#92284
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
Improve throughput / allocations of JsonNode.GetPath
The current implementation is creating a `List<string>` and appending each segment to it, which in most of the cases is allocating a `string`. Then it iterates through that list in reverse order appending to a newly-created `StringBuilder`, which it then `ToString`s. In this change, it instead just uses `ValueStringBuilder`, appending to it as it goes.In doing so, it does reverse the order of enumeration. Previously each node would effectively do:```C#void GetPath(){ AddNode(this); parent?.GetPath();}```and now it's doing:```C#void GetPath(){ parent?.GetPath(); AddNode(this);}```While C# doesn't emit tail calls, with optimizations enabled, it's feasible the JIT might emit the recursive call as a jmp rather than a call, in which case it would avoid possible stack dives. However, that's not guaranteed, and doesn't happen today in tier 0 and other unoptimized code. On top of that, to get such a deep nesting in a JsonNode, you need to either go out of your way to create one manually using the JsonNode/Object/Array/Value constructors, or you need to use JsonSerializer.Deserializer, overriding its default MaxDepth, and in the case of a really deep input, it's also recursive and will stack overflow in smaller situations. I have a different version of this change that keeps the same ordering, passing around a span and a length separately, and prepending to the end of the span, but it results in more complicated code, so I'd prefer this variation that just uses ValueStringBuilder unless we have real concerns.- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitf838a8417bd8795c3e13cad39d42d055e10c37a3
There are no files selected for viewing
2 changes: 1 addition & 1 deletionsrc/libraries/Common/src/System/Text/ValueStringBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/libraries/System.Text.Json/src/System.Text.Json.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
18 changes: 14 additions & 4 deletionssrc/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonArray.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -202,15 +202,25 @@ internal void SetItem(int index, JsonNode? value) | ||
| List[index] = value; | ||
| } | ||
| internal override void GetPath(ref ValueStringBuilder path, JsonNode? child) | ||
| { | ||
| Parent?.GetPath(ref path, this); | ||
| if (child != null) | ||
| { | ||
| int index = List.IndexOf(child); | ||
| Debug.Assert(index >= 0); | ||
| path.Append('['); | ||
| #if NETCOREAPP | ||
| Span<char> chars = stackalloc char[JsonConstants.MaximumFormatUInt32Length]; | ||
| ((uint)index).TryFormat(chars, out int charsWritten); | ||
stephentoub marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| path.Append(chars.Slice(0, charsWritten)); | ||
| #else | ||
| path.Append(index.ToString()); | ||
| #endif | ||
| path.Append(']'); | ||
| } | ||
| } | ||
| /// <inheritdoc/> | ||
16 changes: 5 additions & 11 deletionssrc/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonNode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 8 additions & 5 deletionssrc/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionssrc/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.