Go EP17: The Architecture of Go's sync.Pool
Instead of throwing these objects away after each use, which would just make the garbage collector work harder, we’re better off keeping them in a pool.
Go's sync.Pool
was introduced to solve a pretty common challenge in concurrent programming.
In high-concurrency situations, when you're creating and destroying objects all the time, it can lead to a bunch of allocation overhead and, frankly, put unnecessary pressure on the garbage collector. sync.Pool
offers a better solution by letting us stash temporary objects in a pool, instead of constantly allocating new ones.
Now, to make this pooling work smoothly, sync.Pool
makes good use of Go’s runtime scheduler.
It creates a local pool for each P (processor) in the Go runtime, which helps minimize contention between goroutines running on different processors. Each local pool has two parts: a private object that doesn't need any synchronization (so no locking needed there), and a shared pool chain for storing more objects.
The shared pool chain is actually a pretty clever lock-free data structure called poolChain
.
Basically, it’s a linked list where each node is a poolDequeue
, which is a ring buffer sized as a power of 2. This data structure allows you to push and pop objects from both ends:
The owning processor (P) works on the head.
Other processors (Ps) work on the tail.
Now, of course, you can’t just keep storing objects forever, that would eventually cause memory leaks.
To prevent that, sync.Pool
has a built-in cleanup mechanism that works alongside Go’s garbage collector. Before each garbage collection cycle, the pool moves its contents into a ‘victim’ cache, which then gets wiped out in the next cycle.
But we need to keep in mind that objects in the pool can disappear at any time, so it’s best for short-term and frequently use, not for caching expensive resources you plan to hold onto.
If you're interested in diving deeper into the content, you can follow the links https://victoriametrics.com/blog/go-sync-pool/ to read the full posts.