Skip to content

perf(encode): pool sorted map key slices#54

Merged
xe-nvdk merged 1 commit intov6from
perf/sorted-map-pool-keys
Mar 3, 2026
Merged

perf(encode): pool sorted map key slices#54
xe-nvdk merged 1 commit intov6from
perf/sorted-map-pool-keys

Conversation

@xe-nvdk
Copy link
Member

@xe-nvdk xe-nvdk commented Mar 3, 2026

Summary

  • Pools []string key slices via sync.Pool in the three sorted map encode functions (EncodeMapSorted, encodeSortedMapStringBool, encodeSortedMapStringString)
  • Eliminates 1 allocation per sorted map encode for SetSortMapKeys(true) callers
  • Oversized slices (>1024 cap) are dropped to prevent unbounded pool growth

Benchmark (no regression on default benchmarks; sorted-map alloc savings not measured by existing benchmarks)

Benchmark ns/op B/op allocs/op
StructMarshal 386 1224 4
StructUnmarshal 353 48 7
MapStringString 180 16 4
MapStringInterface 395 402 12

Test plan

  • go test -count=1 ./... — all pass
  • go test -short -race -count=1 -timeout=5m ./... — no races
  • Benchmarks — no regressions

Replaces per-call make([]string, 0, len(m)) with a sync.Pool of
reusable key slices in EncodeMapSorted, encodeSortedMapStringBool,
and encodeSortedMapStringString. Eliminates 1 alloc per sorted map
encode for callers using SetSortMapKeys(true).
@xe-nvdk xe-nvdk merged commit 03453cc into v6 Mar 3, 2026
3 checks passed
@xe-nvdk xe-nvdk deleted the perf/sorted-map-pool-keys branch March 3, 2026 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant