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

Allow specifying IndentCharacter and IndentSize when writing JSON #95292

Merged
merged 29 commits into from
Jan 8, 2024

Conversation

manandre
Copy link
Contributor

Closes #63882

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Nov 27, 2023
@ghost
Copy link

ghost commented Nov 27, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Closes #63882

Author: manandre
Assignees: -
Labels:

area-System.Text.Json, new-api-needs-documentation

Milestone: -

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding logic to the JSON writer hot path, so we need to verify it doesn't introduce any unintentional performance regressions. Could you try running the benchmarks found in

https://github.com/dotnet/performance/tree/main/src/benchmarks/micro/libraries/System.Text.Json/Utf8JsonWriter

comparing the current main with your PR branch. Instructions on how to do this can be found here.

manandre and others added 2 commits November 28, 2023 22:00
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is getting really close. Would it be possible to share updated benchmark numbers with the newer implementation?

@manandre
Copy link
Contributor Author

New benchmarks results summary: better: 20, geomean: 1.090 worse: 31, geomean: 1.124 total diff: 51
Slower diff/base Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Tests.Perf_Guids.WriteGuids(Formatted: False, SkipValidation: T 1.73 1084991.13 1881111.69
System.Text.Json.Tests.Perf_Ctor.Ctor(Formatted: False, SkipValidation: False) 1.60 14.68 23.47
System.Text.Json.Serialization.Tests.WriteJson.SerializeToUtf8Bytes(Mode: 1.20 85.67 103.17
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromString(Mode: 1.15 104.29 120.22
System.Text.Json.Tests.Perf_Reader.ReadSingleSpanSequenceEmptyLoop(IsDataCompact 1.15 1331.87 1527.20
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromString(Mode: 1.14 105.52 120.52
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.14 119.30 136.12
System.Text.Json.Document.Tests.Perf_EnumerateArray.EnumerateUsingIndexer(TestCa 1.14 1254.13 1425.34
System.Text.Json.Tests.Perf_Reader.ReadSingleSpanSequenceEmptyLoop(IsDataCompact 1.13 2750.80 3096.63
System.Text.Json.Tests.Perf_Ctor.Ctor(Formatted: False, SkipValidation: True) 1.11 14.46 16.10
System.Text.Json.Serialization.Tests.WriteJson.Serial 1.11 350.55 387.47
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.10 1721.12 1891.75
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: False, SkipValidatio 1.10 537.72 589.71
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mo 1.09 301.87 328.84
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.08 193.20 209.51
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf16(Formatted: False, SkipValidati 1.08 623.08 673.54
System.Text.Json.Document.Tests.Perf_ParseThenWrite.ParseThenWrite(IsDataIndente 1.08 4900.26 5291.65
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mode: Re 1.08 186.65 200.94
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: True, SkipValidation 1.08 2205729.09 2374611.54
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mode: So 1.08 190.44 204.97
System.Text.Json.Tests.Perf_Reader.ReadReturnBytes(IsDataCompact: True, TestCase 1.08 66.44 71.48
System.Text.Json.Tests.Perf_Reader.ReadReturnBytes(IsDataCompact: False, TestCas 1.07 10109.00 10861.52
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: True, SkipValidation 1.07 2216908.85 2369424.04
System.Text.Json.Serialization.Tests.WriteJson.SerializeToString(Mo 1.07 363.03 387.82
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf16(Formatted: True, SkipValidatio 1.07 2218130.36 2367656.60
System.Text.Json.Reader.Tests.Perf_Base64.ReadBase64EncodedByteArray_NoEscaping( 1.06 76.25 81.05
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.06 188.73 200.20
System.Text.Json.Serialization.Tests.ReadJson<Nullable>.Deserial 1.06 320.92 340.19
System.Text.Json.Reader.Tests.Perf_Base64.ReadBase64EncodedByteArray_HeavyEscapi 1.06 76.27 80.72
System.Text.Json.Serialization.Tests.WriteJson.Seria 1.06 187.85 198.51
System.Text.Json.Serialization.Tests.WriteJson<HashSet>.SerializeObjectP 1.06 4630.13 4892.40
Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Serialization.Tests.ReadJson.Deseri 1.23 547.64 446.59
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.12 2704.65 2416.30
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromStrin 1.11 660.34 592.57
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.10 3486.84 3173.37
System.Text.Json.Serialization.Tests.ReadJson<Nullable>.Deserial 1.10 177.76 162.07
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromUtf8Byte 1.09 16925.82 15491.40
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.09 74.74 68.44
System.Text.Json.Serialization.Tests.WriteJson.SerializeToWriter(Mode: Re 1.09 28.75 26.36
System.Text.Json.Tests.Perf_Guids.WriteGuids(Formatted: False, SkipValidation: F 1.09 1202539.56 1103092.92
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mode: 1.09 603.37 555.91
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromUtf8B 1.08 547.94 506.66
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.08 74.51 68.94
System.Text.Json.Tests.Perf_Reader.ReadReturnBytes(IsDataCompact: False, TestCas 1.08 79.61 73.78 bimodal
System.Text.Json.Serialization.Tests.ReadMissingAndCaseInsensitive.Cas 1.08 1026.32 951.98
System.Text.Json.Tests.Perf_Depth.ReadSpanEmptyLoop(Depth: 64) 1.07 1756.64 1639.75
System.Text.Json.Document.Tests.Perf_ParseThenWrite.ParseThenWrite(IsDataIndente 1.07 300.72 281.33
System.Text.Json.Serialization.Tests.WriteJson.SerializeToUtf8Bytes 1.07 312.57 292.93
System.Text.Json.Serialization.Tests.WriteJson<ImmutableSortedDictionary<String, 1.07 8509.42 7989.29
System.Text.Json.Tests.Perf_Get.GetInt32 1.06 782.77 738.39
System.Text.Json.Serialization.Tests.WriteJson<ImmutableSortedDictionary<String, 1.06 7665.66 7261.13

I do not know how to interpret these results, as even Read-based and deserialization tests are impacted !?!

@manandre manandre changed the title Allow specifying IndentText when writing JSON Allow specifying IndentCharacter and IndentSize when writing JSON Dec 28, 2023
@manandre
Copy link
Contributor Author

Benchmarks results summary: better: 17, geomean: 1.111 worse: 21, geomean: 1.089 total diff: 38
Slower diff/base Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.20 2704.65 3259.01
System.Text.Json.Serialization.Tests.WriteJson.Seria 1.17 187.85 220.11
System.Text.Json.Tests.Perf_Reader.ReadSpanEmptyLoop(IsDataCompact: True, TestCa 1.12 32493.49 36383.36
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromString(Mode: 1.11 104.29 115.53
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf16(Formatted: False, SkipValidati 1.10 623.08 685.92
System.Text.Json.Serialization.Tests.WriteJson.SerializeToUtf8Bytes(Mode: 1.10 85.67 94.01
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mo 1.09 301.87 328.22
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: True, SkipValidation 1.08 2216908.85 2396831.73
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: False, SkipValidatio 1.08 537.72 580.41
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.08 188.73 203.58
System.Text.Json.Reader.Tests.Perf_Base64.ReadBase64EncodedByteArray_NoEscaping( 1.08 76.25 82.17
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.08 119.30 128.26
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf16(Formatted: True, SkipValidatio 1.07 2218130.36 2381707.62
System.Text.Json.Serialization.Tests.WriteJson.SerializeToWriter(Mode: Re 1.07 28.75 30.81
System.Text.Json.Document.Tests.Perf_ParseThenWrite.ParseThenWrite(IsDataIndente 1.07 4900.26 5242.63
System.Text.Json.Serialization.Tests.WriteJson.SerializeToString(Mo 1.07 280.26 299.54
System.Text.Json.Serialization.Tests.WriteJson<ImmutableSortedDictionary<String, 1.07 8194.94 8755.16
System.Text.Json.Tests.Perf_Basic.WriteBasicUtf8(Formatted: True, SkipValidation 1.07 2205729.09 2355302.83
System.Text.Json.Document.Tests.Perf_ParseThenWrite.ParseThenWrite(IsDataIndente 1.07 17376.31 18536.19
System.Text.Json.Tests.Perf_Reader.ReadSpanEmptyLoop(IsDataCompact: True, TestCa 1.06 60.02 63.71
System.Text.Json.Node.Tests.Perf_ParseThenWrite.ParseThenWrite(IsDataIndented: F 1.06 8732.17 9248.11
Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Tests.Perf_Base64.WriteByteArrayAsBase64_NoEscaping(NumberOfByt 1.59 107.09 67.49
System.Text.Json.Serialization.Tests.ReadJson.Deseri 1.18 547.64 465.08
System.Text.Json.Serialization.Tests.WriteJson.Seria 1.13 155.31 137.54
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromStrin 1.12 660.34 587.55
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.09 3486.84 3188.09
System.Text.Json.Serialization.Tests.WriteJson.SerializeToStream(Mode: 1.09 603.37 552.69
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.09 74.74 68.62
System.Text.Json.Tests.Perf_Guids.WriteGuids(Formatted: False, SkipValidation: F 1.08 1202539.56 1108971.24
System.Text.Json.Serialization.Tests.WriteJson<Nullable>.Seriali 1.08 74.51 69.14
System.Text.Json.Serialization.Tests.ReadJson.DeserializeFromUtf8B 1.07 547.94 510.00
System.Text.Json.Serialization.Tests.WriteJson.SerializeToString(Mod 1.07 587.49 547.54
System.Text.Json.Serialization.Tests.WriteJson.SerializeToString(Mode: So 1.07 111.16 103.63
System.Text.Json.Serialization.Tests.ReadJson<Nullable>.Deserial 1.07 177.76 166.24
System.Text.Json.Serialization.Tests.ReadJson.Deseria 1.06 885.20 832.84
System.Text.Json.Tests.Perf_Get.GetInt32 1.06 782.77 737.68
System.Text.Json.Serialization.Tests.WriteJson.SerializeToUtf8By 1.06 313.90 297.13
System.Text.Json.Tests.Perf_Reader.ReadMultiSpanSequenceEmptyLoop(IsDataCompact: 1.06 10122.40 9586.38

Still no clue how to interpret these results...

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your work @manandre!

@eiriktsarpalis eiriktsarpalis merged commit 37ed0ee into dotnet:main Jan 8, 2024
108 of 110 checks passed
@manandre manandre deleted the json-indent-text branch January 8, 2024 19:13
@eiriktsarpalis eiriktsarpalis added this to the 9.0.0 milestone Jan 26, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Allow specifying indent size and whitespace character when writing JSON with Utf8JsonWriter
3 participants