Problem
Every map decode allocates a fresh map via make(map[string]string, n) or make(map[string]interface{}, n) or reflect.MakeMapWithSize(). Under sustained high-throughput decode, these map allocations generate significant GC pressure.
Proposal
Use sync.Pool of pre-allocated maps, cleared with clear() (Go 1.21+) before reuse. This is tricky because the caller retains the map, so pooling only works if the caller explicitly returns it (opt-in API) or if we pool the intermediate decode buffer.
Alternative: provide a UnmarshalReuse(data []byte, v interface{}) API that reuses existing maps/slices in v instead of allocating new ones.
Files
decode_map.go — decodeMapStringStringValue, decodeMapStringInterfaceValue, decodeMapDefault
Expected Impact
MEDIUM-HIGH — eliminates 1 map allocation per map decode. Impact depends on map frequency in workload.
Notes
Requires careful design — maps returned to callers can't be pooled without an explicit return mechanism. Consider a Decoder.UsePreallocateValues(true) extension or reuse of existing map values.
Problem
Every map decode allocates a fresh map via
make(map[string]string, n)ormake(map[string]interface{}, n)orreflect.MakeMapWithSize(). Under sustained high-throughput decode, these map allocations generate significant GC pressure.Proposal
Use
sync.Poolof pre-allocated maps, cleared withclear()(Go 1.21+) before reuse. This is tricky because the caller retains the map, so pooling only works if the caller explicitly returns it (opt-in API) or if we pool the intermediate decode buffer.Alternative: provide a
UnmarshalReuse(data []byte, v interface{})API that reuses existing maps/slices invinstead of allocating new ones.Files
decode_map.go—decodeMapStringStringValue,decodeMapStringInterfaceValue,decodeMapDefaultExpected Impact
MEDIUM-HIGH — eliminates 1 map allocation per map decode. Impact depends on map frequency in workload.
Notes
Requires careful design — maps returned to callers can't be pooled without an explicit return mechanism. Consider a
Decoder.UsePreallocateValues(true)extension or reuse of existing map values.