-
Notifications
You must be signed in to change notification settings - Fork 76
opt: add more fast paths when reading vector from JSON #1415
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
Merged
Conversation
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
There are already fast paths for certain types, but floats and various
others were missing, so we were falling back to a very allocation-heavy
interface{} method where every value was boxed then unboxed later.
Now that we have generic vectors we can read directly into them and
avoid a ton of allocations, which speeds things up dramatically,
especially for larger vectors.
Benchmarks for JSON unmarshalling:
```
goos: linux
goarch: amd64
pkg: github.com/grafana/grafana-plugin-sdk-go/data
cpu: AMD Ryzen 9 7950X 16-Core Processor
│ benchmark-baseline.txt │ benchmark-final.txt │
│ sec/op │ sec/op vs base │
FrameUnmarshalJSON-32 77.72µ ± ∞ ¹ 76.13µ ± ∞ ¹ ~ (p=0.421 n=5)
FrameUnmarshalJSON_FromFrameToJSON-32 74.86µ ± ∞ ¹ 76.81µ ± ∞ ¹ +2.60% (p=0.032 n=5)
FrameUnmarshalJSON_Sizes/Rows_10-32 5.345µ ± ∞ ¹ 5.065µ ± ∞ ¹ -5.24% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_100-32 25.44µ ± ∞ ¹ 22.27µ ± ∞ ¹ -12.44% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_1000-32 231.4µ ± ∞ ¹ 199.0µ ± ∞ ¹ -13.99% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10000-32 2.523m ± ∞ ¹ 2.090m ± ∞ ¹ -17.19% (p=0.008 n=5)
FrameUnmarshalJSON_Parallel-32 25.92µ ± ∞ ¹ 25.42µ ± ∞ ¹ ~ (p=1.000 n=5)
geomean 73.85µ 68.36µ -7.43%
¹ need >= 6 samples for confidence interval at level 0.95
│ benchmark-baseline.txt │ benchmark-final.txt │
│ B/s │ B/s vs base │
FrameUnmarshalJSON-32 69.90Mi ± ∞ ¹ 71.37Mi ± ∞ ¹ ~ (p=0.421 n=5)
FrameUnmarshalJSON_FromFrameToJSON-32 72.45Mi ± ∞ ¹ 70.61Mi ± ∞ ¹ -2.54% (p=0.040 n=5)
FrameUnmarshalJSON_Sizes/Rows_10-32 93.67Mi ± ∞ ¹ 98.85Mi ± ∞ ¹ +5.53% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_100-32 118.8Mi ± ∞ ¹ 135.6Mi ± ∞ ¹ +14.21% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_1000-32 129.4Mi ± ∞ ¹ 150.5Mi ± ∞ ¹ +16.26% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10000-32 125.4Mi ± ∞ ¹ 151.4Mi ± ∞ ¹ +20.75% (p=0.008 n=5)
geomean 98.52Mi 107.5Mi +9.07%
¹ need >= 6 samples for confidence interval at level 0.95
│ benchmark-baseline.txt │ benchmark-final.txt │
│ B/op │ B/op vs base │
FrameUnmarshalJSON-32 24.84Ki ± ∞ ¹ 24.54Ki ± ∞ ¹ -1.23% (p=0.008 n=5)
FrameUnmarshalJSON_FromFrameToJSON-32 24.84Ki ± ∞ ¹ 24.54Ki ± ∞ ¹ -1.23% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10-32 3.328Ki ± ∞ ¹ 2.953Ki ± ∞ ¹ -11.27% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_100-32 15.50Ki ± ∞ ¹ 12.67Ki ± ∞ ¹ -18.25% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_1000-32 132.44Ki ± ∞ ¹ 98.73Ki ± ∞ ¹ -25.45% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10000-32 1.822Mi ± ∞ ¹ 1.320Mi ± ∞ ¹ -27.59% (p=0.008 n=5)
FrameUnmarshalJSON_Parallel-32 24.84Ki ± ∞ ¹ 24.54Ki ± ∞ ¹ -1.22% (p=0.008 n=5)
geomean 41.02Ki 35.69Ki -13.00%
¹ need >= 6 samples for confidence interval at level 0.95
│ benchmark-baseline.txt │ benchmark-final.txt │
│ allocs/op │ allocs/op vs base │
FrameUnmarshalJSON-32 770.0 ± ∞ ¹ 757.0 ± ∞ ¹ -1.69% (p=0.008 n=5)
FrameUnmarshalJSON_FromFrameToJSON-32 770.0 ± ∞ ¹ 757.0 ± ∞ ¹ -1.69% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10-32 100.00 ± ∞ ¹ 77.00 ± ∞ ¹ -23.00% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_100-32 373.0 ± ∞ ¹ 170.0 ± ∞ ¹ -54.42% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_1000-32 3.076k ± ∞ ¹ 1.073k ± ∞ ¹ -65.12% (p=0.008 n=5)
FrameUnmarshalJSON_Sizes/Rows_10000-32 30.09k ± ∞ ¹ 10.08k ± ∞ ¹ -66.50% (p=0.008 n=5)
FrameUnmarshalJSON_Parallel-32 770.0 ± ∞ ¹ 757.0 ± ∞ ¹ -1.69% (p=0.008 n=5)
geomean 1.067k 671.3 -37.10%
¹ need >= 6 samples for confidence interval at level 0.95
```
toddtreece
approved these changes
Oct 7, 2025
Member
toddtreece
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
There are already fast paths for certain types, but floats and various
others were missing, so we were falling back to a very allocation-heavy
interface{} method where every value was boxed then unboxed later.
Now that we have generic vectors we can read directly into them and
avoid a ton of allocations, which can improve things,
especially for larger vectors.
I think the 10_000 benches are dominated by string allocations so the sec/op
doesn't change much but there are 3x fewer allocs which will reduce GC
pressure significantly for larger frames.
Interesting benchmark results:
all: