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

Mitigate JsonObject performance regression. #107944

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

eiriktsarpalis
Copy link
Member

@eiriktsarpalis eiriktsarpalis commented Sep 17, 2024

Makes the following changes:

  1. Makes the JsonValueKind computation for JsonValue instances backed by primitives a lazy operation.
  2. Updates the JsonObject indexer implementation so that duplicate lookups aren't performed in the common case where a property isn't being overwritten.

Should be backported to .NET 9. Fix #107869.

Benchmarks

Using dotnet/performance#4463 on arm64.

Method Branch Mean Error StdDev Median Min Max Ratio RatioSD Gen0 Allocated Alloc Ratio
Create_JsonBool .NET 8 266.5 ns 3.24 ns 3.03 ns 266.1 ns 262.5 ns 272.6 ns 1.00 0.02 0.4779 2000 B 1.00
Create_JsonBool PR 277.2 ns 2.87 ns 2.54 ns 277.7 ns 273.5 ns 282.0 ns 1.04 0.01 0.4781 2000 B 1.00
Create_JsonNumber .NET 8 268.0 ns 3.33 ns 3.11 ns 268.9 ns 260.5 ns 271.3 ns 1.00 0.02 0.4773 2000 B 1.00
Create_JsonNumber PR 285.9 ns 15.21 ns 17.52 ns 275.6 ns 269.9 ns 328.5 ns 1.07 0.06 0.5732 2400 B 1.20
Create_JsonString .NET 8 366.3 ns 3.87 ns 3.62 ns 366.9 ns 361.1 ns 372.9 ns 1.00 0.01 0.5738 2400 B 1.00
Create_JsonString PR 281.5 ns 2.38 ns 1.98 ns 280.8 ns 278.4 ns 286.1 ns 0.77 0.01 0.6689 2800 B 1.17
Create_JsonArray .NET 8 486.5 ns 6.29 ns 5.89 ns 486.5 ns 477.5 ns 495.4 ns 1.00 0.02 0.7263 3040 B 1.00
Create_JsonArray PR 463.9 ns 4.96 ns 4.64 ns 463.1 ns 455.2 ns 471.9 ns 0.95 0.01 0.7258 3040 B 1.00
Create_JsonObject_Small .NET 8 236.1 ns 1.31 ns 1.16 ns 236.1 ns 233.7 ns 237.8 ns 1.00 0.01 0.1618 680 B 1.00
Create_JsonObject_Small PR 198.7 ns 2.13 ns 1.99 ns 198.2 ns 195.9 ns 202.0 ns 0.84 0.01 0.2503 1048 B 1.54
Create_JsonObject_Large .NET 8 639.4 ns 6.81 ns 6.37 ns 638.6 ns 631.1 ns 652.8 ns 1.00 0.01 0.5612 2352 B 1.00
Create_JsonObject_Large PR 393.0 ns 2.78 ns 2.60 ns 393.0 ns 389.8 ns 397.8 ns 0.61 0.01 0.5102 2136 B 0.91

@stephentoub
Copy link
Member

Revert delayed JsonValueKind derivation

How much of the improvement came from this? There are ways to make it safe.

@eiriktsarpalis
Copy link
Member Author

Revert delayed JsonValueKind derivation

How much of the improvement came from this? There are ways to make it safe.

Not a huge amount. I figured I would rather take the regression for the sake of simplicity.

@eiriktsarpalis eiriktsarpalis changed the title Mitigate JsonObject and JsonValue performance regressions. Mitigate JsonObject performance regression. Sep 18, 2024
@eiriktsarpalis eiriktsarpalis merged commit e8613a6 into dotnet:main Sep 19, 2024
85 checks passed
@eiriktsarpalis
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10940503124

sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* Mitigate JsonObject and JsonValue performance regressions.

* Revert delayed JsonValueKind derivation.
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance regression with JsonObject creation by +70%
4 participants