Skip to content

Add (*Set).FromJSON benchmarks and switch to post-accumulation sort.#310

Open
benluddy wants to merge 1 commit into
kubernetes-sigs:masterfrom
benluddy:fromjson-post-accumulation-sort
Open

Add (*Set).FromJSON benchmarks and switch to post-accumulation sort.#310
benluddy wants to merge 1 commit into
kubernetes-sigs:masterfrom
benluddy:fromjson-post-accumulation-sort

Conversation

@benluddy

@benluddy benluddy commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

Set deserialization with children in reverse order performs an insertion sort. Sets serialized by structured-merge-diff are sorted, but in practice the serialization of sets is controlled by clients and they can (intentionally or not) exercise this worst case. Switching to a bulk insert followed by a sort appears to perform no worse in the best case (sorted) and is significantly better in the worst case:

                                              │     master     │                 bulk                 │
                                              │     sec/op     │    sec/op     vs base                │
SetFromJSON/children_in_ascending_order            1.286m ± 3%   1.341m ±  1%   +4.27% (p=0.000 n=10)
SetFromJSON/children_in_descending_order          20.189m ± 0%   1.385m ±  1%  -93.14% (p=0.000 n=10)
SetFromJSON/grandchildren_in_ascending_order       7.821m ± 1%   8.211m ±  1%   +4.98% (p=0.000 n=10)
SetFromJSON/grandchildren_in_descending_order      75.16m ± 4%   13.31m ± 13%  -82.28% (p=0.000 n=10)
geomean                                            11.11m        3.774m        -66.04%

                                              │     master     │                 bulk                 │
                                              │      B/op      │     B/op      vs base                │
SetFromJSON/children_in_ascending_order           1.320Mi ± 0%   1.320Mi ± 0%        ~ (p=1.000 n=10)
SetFromJSON/children_in_descending_order          1.320Mi ± 0%   1.320Mi ± 0%   +0.00% (p=0.000 n=10)
SetFromJSON/grandchildren_in_ascending_order      5.395Mi ± 0%   5.395Mi ± 0%   +0.00% (p=0.000 n=10)
SetFromJSON/grandchildren_in_descending_order     6.187Mi ± 0%   5.395Mi ± 0%  -12.80% (p=0.000 n=10)
geomean                                           2.761Mi        2.668Mi        -3.37%

                                              │     master     │                 bulk                  │
                                              │   allocs/op    │  allocs/op   vs base                  │
SetFromJSON/children_in_ascending_order            25.99k ± 0%   25.99k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/children_in_descending_order           25.99k ± 0%   25.99k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/grandchildren_in_ascending_order       138.4k ± 0%   138.4k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/grandchildren_in_descending_order      155.7k ± 0%   138.4k ± 0%  -11.11% (p=0.000 n=10)
geomean                                            61.77k        59.98k        -2.90%
¹ all samples are equal

This was originally proposed in #292. All I've done here is added benchmarks to demonstrate the worst case performance and separated it from the json-iterator migration.

@k8s-ci-robot
k8s-ci-robot requested a review from yliaog December 18, 2025 20:33
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: benluddy
Once this PR has been reviewed and has the lgtm label, please assign jpbetz for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 18, 2025
@benluddy

Copy link
Copy Markdown
Contributor Author

/cc @jpbetz @liggitt @inteon

Comment thread fieldpath/serialize.go
@benluddy
benluddy force-pushed the fromjson-post-accumulation-sort branch from 6c869cc to 367149b Compare December 19, 2025 15:58
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 19, 2025
Set deserialization with children in reverse order performs an insertion sort. Sets serialized by
structured-merge-diff are sorted, but in practice the serialization of sets is controlled by clients
and they can (intentionally or not) exercise this worst case. Switching to a bulk insert followed by
a sort appears to perform no worse in the best case (sorted) and is significantly better in the
worst case:

                                              │     master     │                 bulk                 │
                                              │     sec/op     │    sec/op     vs base                │
SetFromJSON/children_in_ascending_order            1.286m ± 3%   1.341m ±  1%   +4.27% (p=0.000 n=10)
SetFromJSON/children_in_descending_order          20.189m ± 0%   1.385m ±  1%  -93.14% (p=0.000 n=10)
SetFromJSON/grandchildren_in_ascending_order       7.821m ± 1%   8.211m ±  1%   +4.98% (p=0.000 n=10)
SetFromJSON/grandchildren_in_descending_order      75.16m ± 4%   13.31m ± 13%  -82.28% (p=0.000 n=10)
geomean                                            11.11m        3.774m        -66.04%

                                              │     master     │                 bulk                 │
                                              │      B/op      │     B/op      vs base                │
SetFromJSON/children_in_ascending_order           1.320Mi ± 0%   1.320Mi ± 0%        ~ (p=1.000 n=10)
SetFromJSON/children_in_descending_order          1.320Mi ± 0%   1.320Mi ± 0%   +0.00% (p=0.000 n=10)
SetFromJSON/grandchildren_in_ascending_order      5.395Mi ± 0%   5.395Mi ± 0%   +0.00% (p=0.000 n=10)
SetFromJSON/grandchildren_in_descending_order     6.187Mi ± 0%   5.395Mi ± 0%  -12.80% (p=0.000 n=10)
geomean                                           2.761Mi        2.668Mi        -3.37%

                                              │     master     │                 bulk                  │
                                              │   allocs/op    │  allocs/op   vs base                  │
SetFromJSON/children_in_ascending_order            25.99k ± 0%   25.99k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/children_in_descending_order           25.99k ± 0%   25.99k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/grandchildren_in_ascending_order       138.4k ± 0%   138.4k ± 0%        ~ (p=1.000 n=10) ¹
SetFromJSON/grandchildren_in_descending_order      155.7k ± 0%   138.4k ± 0%  -11.11% (p=0.000 n=10)
geomean                                            61.77k        59.98k        -2.90%
¹ all samples are equal
@benluddy
benluddy force-pushed the fromjson-post-accumulation-sort branch from 367149b to 26509bc Compare December 19, 2025 16:05
@liggitt

liggitt commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

cc @jpbetz for detailed review

@k8s-triage-robot

Copy link
Copy Markdown

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 20, 2026
@inteon

inteon commented Apr 21, 2026

Copy link
Copy Markdown
Member

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 21, 2026
@k8s-triage-robot

Copy link
Copy Markdown

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@kubernetes-prow kubernetes-prow Bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants