perf: hoist newValue allocations out of typed map decode loop (#65)#69
Merged
Conversation
Closes #65. decodeTypedMapValue called d.newValue() per map entry, producing 2N reflect.New() allocations per N-entry typed map. Hoist the key and value reflect.Value slots outside the loop and reuse them across iterations, zero-resetting before each use. Takes typed-map decode from 2N reflect.New() calls to 2 per map. The zeroing is a correctness requirement, not cosmetic: decodeSliceValue reuses an existing slice's backing array when v.Cap() >= n, so without zeroing between iterations, iteration 2 would clobber the slice iteration 1 already stored in the map (SetMapIndex copies the slice header but the backing array is shared). Two regression tests cover this: map[int][]int and map[int]struct{A int; B []string} — both fail without the per-iteration Set(zero) lines. BenchmarkLargeMapIntInt (1000-entry map[int]int): before: 4000 allocs/op, 32006 B/op, ~140us/op after: 2002 allocs/op, 16021 B/op, ~124us/op (-50% allocs, -50% bytes, -10% ns)
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
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.
Summary
Closes #65.
decodeTypedMapValuecalledd.newValue()per map entry — 2Nreflect.New()allocations per N-entry typed map. Hoist the key/valuereflect.Valueslots outside the loop and reuse them, zero-resetting before each iteration.Zeroing is a correctness requirement, not cosmetic.
decodeSliceValuereuses an existing slice's backing array whenv.Cap() >= n; without zeroing the hoisted slot, iteration 2 of amap[K][]Vdecode would clobber iteration 1's already-stored slice.SetMapIndexcopies the slice header but the backing array is shared.Measured impact
BenchmarkLargeMapIntInt(1000-entrymap[int]int):Test plan
go test ./...passesgo test -race ./...passesgo vet ./...cleanSet(zero)lines):TestDecodeTypedMapSliceValuesAreIndependent—map[int][]intbacking-array aliasingTestDecodeTypedMapStructValuesAreIndependent— struct with[]stringfield aliasingtypes_test.gotypeTest additions:map[int][]int,map[string][]string,map[string]map[string]intBenchmarkLargeMapIntIntinbench_test.gofor future perf regression detection