Skip to content

perf(decode): replace goroutine-per-type value pool with sync.Pool#53

Merged
xe-nvdk merged 1 commit intov6from
perf/sync-pool-cached-values
Mar 3, 2026
Merged

perf(decode): replace goroutine-per-type value pool with sync.Pool#53
xe-nvdk merged 1 commit intov6from
perf/sync-pool-cached-values

Conversation

@xe-nvdk
Copy link
Member

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

Summary

  • Replaces cachedValues (unbounded goroutine per type + channel buffer of 256) with sync.Pool per type stored in sync.Map
  • Eliminates goroutine leak — the old approach spawned a goroutine for every unique decoded type that ran in an infinite for { ch <- reflect.New(t) } loop forever
  • Net reduction: -27 lines added, +9 lines = 18 lines fewer

Details

The old mechanism used sync.RWMutex + map[reflect.Type]chan reflect.Value with a goroutine per type that pre-generated values into a buffered channel. This leaked goroutines (one per unique type, never cleaned up) and used channel synchronization on the hot path.

The new approach uses sync.Map + sync.Pool per type. Since decoded values are mutated by the caller and not returned to the pool, sync.Pool degrades to pool.New() after GC — but this is equivalent to reflect.New(t) which is what the non-preallocate path already does.

Test plan

  • All existing tests pass
  • Race tests pass (go test -short -race)
  • No benchmark regressions

Replaces the cachedValues mechanism (unbounded goroutine per type +
channel with buffer of 256) with sync.Pool per type stored in a
sync.Map. Eliminates goroutine leak — the old approach spawned a
goroutine for every unique decoded type that ran forever in an
infinite loop. No performance regression in benchmarks.
@xe-nvdk xe-nvdk merged commit efe4285 into v6 Mar 3, 2026
3 checks passed
@xe-nvdk xe-nvdk deleted the perf/sync-pool-cached-values 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