-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
encoding/json: Unmarshal to a typed struct uses 50% more bytes in go 1.21 (and current versions) vs go 1.20 #69828
Comments
Seen when updating kubernetes-sigs/json#19 (comment) to update stdlib decoding code from 1.19 → 1.23. cc @dsnet for visibility |
It's a bit buried, so calling out that the report is saying that the increased memory use is due to https://go.dev/cl/471175 "encoding/json: rely on reflect.Value.Grow". The 1.20 code always increases the size of a slice by 50% of the previous cap; if that is less than 4 it goes straight to 4. The 1.21 code grows the slice as though it is appending a single element. In this case we have small slices, for which it will double the size of a slice. The effect is to allocate space for 1 element, then 2, then 4. As the slice elements are fairly large, and the slice winds up with 3 elements, the extra allocation of size 2 likely accounts for the change in memory use. This seems like a fairly special case. The new code is normally better. In this particular case it is worse. I don't think there is anything to do here. |
yeah, I was trying to figure out if the test just happened to hit a ~worst-case scenario... the test data only has a single array (https://gist.github.com/liggitt/bf28159afdaabf52b78ac43d86bc6362#file-bench-json-L12-L46), which has three items I think the old code grew from 0 → 4 directly I think the new code grows from 0 → 1, 1 → 2, 2 → 3 I'm still surprised at the size jump for those allocs on that struct, but maybe that fully explains it. If so, I'll probably just adjust the benchmark to exercise arrays of a few different sizes to better represent the general performance |
Let me dig another day and I'll self close by EOW if a better distributed benchmark indicates this isn't a general issue |
Yeah, with better distributed testdata, the alloc delta ~disappeared |
Go version
go 1.21+ (past ac27b4d)
Output of
go env
in your module/workspace:What did you do?
Benchmarked json.Unmarshal into a typed go struct using stdlib code from 1.19 / 1.20 / 1.21 / 1.22 / 1.23
Benchmark code and results are at https://gist.github.com/liggitt/bf28159afdaabf52b78ac43d86bc6362
What did you see happen?
According to benchstat, an unmarshal of https://gist.github.com/liggitt/bf28159afdaabf52b78ac43d86bc6362#file-bench-json into https://gist.github.com/liggitt/bf28159afdaabf52b78ac43d86bc6362#file-regression_test-go-L32 used 50% more memory after ac27b4d
What did you expect to see?
Minimal variance in memory usage minor-over-minor (low single-digit changes)
The text was updated successfully, but these errors were encountered: