3 ms·
Since you're in here, if I can recommend: sync.Pool with non-fixed-size objects is a source of many problems. https://wundergraph.com/blog/golang-sync-pool htt
by Groxx 2y ago
Since you're in here, if I can recommend: sync.Pool with non-fixed-size objects is a source of many problems. https://wundergraph.com/blog/golang-sync-pool https://wundergraph.com/blog/golang-sync-pool as a decent overview, and https://github.com/yarpc/yarpc-go/issues/2355 https://github.com/yarpc/yarpc-go/issues/2355 has been my recent plague.
If you browse around Go's stdlib use of sync.Pool, you'll see a variety of tiered pools with fixed sizes, and many drop anything over a large enough size (sometimes gigantic! as much as 16KB!): https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/net/http/server.go;l=832 https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/...
It's a pretty well-established gotcha, sadly, and https://github.com/teivah/100-go-mistakes/blob/master/src/12-optimizations/96-reduce-allocations/sync-pool/main.go https://github.com/teivah/100-go-mistakes/blob/master/src/12... falls right into it.
- lenkite 2y agoWish Go had introduced memory arenas. Most of the use cases for pools could have been cleanly solved by arenas.
- a-french-anon 2y agoMaybe I'm wrong, but when reading the first article, it seems like the entire thing exists mainly (not completely) because Go's GC isn't generational.
- Groxx 2y agoMaybe somewhat, but it's not hard to get pathologically bad GC behavior out of generational GCs either. Pools / manual memory management will always have some reasonable use cases.