perf(decode): replace goroutine-per-type value pool with sync.Pool#53
Merged
perf(decode): replace goroutine-per-type value pool with sync.Pool#53
Conversation
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.
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
cachedValues(unbounded goroutine per type + channel buffer of 256) withsync.Poolper type stored insync.Mapfor { ch <- reflect.New(t) }loop foreverDetails
The old mechanism used
sync.RWMutex+map[reflect.Type]chan reflect.Valuewith 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.Poolper type. Since decoded values are mutated by the caller and not returned to the pool,sync.Pooldegrades topool.New()after GC — but this is equivalent toreflect.New(t)which is what the non-preallocate path already does.Test plan
go test -short -race)