encoding/json/v2: reduce allocations when checking IsZero - #80879
encoding/json/v2: reduce allocations when checking IsZero#80879philpearl wants to merge 1 commit into
Conversation
… omitzero field implements IsZero
Where I work we have structs with many small fields with IsZero methods. Marshalling these structs with json/v2 causes many allocations, one per such field. I tracked the allocation down to the code that checks for and calls the IsZero method. Calling reflect.Value.Interface{} causes an allocation when the value is not a pointer type.
Removing the excess allocation is as easy as switching the order of two lines in a switch statement so that the case that tries to use a pointer to the field is tried first.
Before:
BenchmarkMarshal/OmitZeroMethod-10 14441822 83.81 ns/op 23.86 MB/s 16 B/op 2 allocs/op
After
BenchmarkMarshal/OmitZeroMethod-10 14114629 80.38 ns/op 24.88 MB/s 8 B/op 1 allocs/op
Fixes golang#80868
Change-Id: I3c1da4946e16a94e70658cdfa02e2b5e29b5c6a5
|
This PR (HEAD: e3c0c26) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815400. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Phil Pearl: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from t hepudds: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Phil Pearl: Patch Set 2: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from qiu laidongfeng: Patch Set 3: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 3: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2026-08-14T14:50:59Z","revision":"b965966123b9079bf09cf89ba8d276b624ecea46"} Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from qiu laidongfeng: Patch Set 3: -Commit-Queue (Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_64033>) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 3: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 3: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Sean Liao: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Phil Pearl: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Sean Liao: Patch Set 3: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Removed this from the PR description in an attempt to fix some Gerrit stuff that I don't understand Change-Id: I3c1da4946e16a94e70658cdfa02e2b5e29b5c6a5 |
|
Does kicking this again do anything? |
|
Come on gerrit bot. Do something! |
|
Message from Phil Pearl: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from Phil Pearl: Patch Set 5: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from t hepudds: Patch Set 5: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
|
Message from t hepudds: Patch Set 6: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815400. |
When checking whether an omitzero field is zero via an IsZero method,
calling Interface() on a value type causes a heap allocation due to
interface boxing.
Reorder the switch cases to check if reflect.PointerTo(sf.Type)
implements the isZeroer interface before checking sf.Type directly.
Because pointer method sets include value methods, this allows using
va.Addr().Interface(), which avoids boxing value types and eliminates
the extra allocation per field.
Fixes #80868