2 ms·
While I think you're right (generics might be useful there), it's fairly easy to wrap the `sync` primitives such as `sync.Pool` and `sync.Map` into your specifi
by zaphodias 2y ago
While I think you're right (generics might be useful there), it's fairly easy to wrap the `sync` primitives such as `sync.Pool` and `sync.Map` into your specific use case.
Go is pretty strict about breaking changes, so they probably won't change the current implementations; maybe we'll see a v2 version, or maybe not. The more code you have, the more code you have to maintain, and given Go's backward-compatibility promises, that's a lot of work.
- Someone 2y ago> While I think you're right (generics might be useful there), it's fairly easy to wrap the `sync` primitives such as `sync.Pool` and `sync.Map` into your specific use case. That’s not a strong argument. You can easily (but sometimes tediously) wrap any API with one that (further) restricts what types you can use with it. Generics make it possible to avoid doing that work, and code you don’t write won’t have errors.
- zaphodias 2y agoDon't get me wrong, I agree! Especially performance-wise, I'd love to have the best primitives that let me build whatever I want and not some very generic primitives that perform a bit worse and I have to tune myself so I don't shoot myself in the foot.
- aktau 2y agoUpstream thinks a type-safer `sync.Pool` is a good idea too. It's being discussed in https://go.dev/issue/71076 https://go.dev/issue/71076.
- strangelove026 2y agoSync.map is meant to have poor performance I believe https://github.com/golang/go/issues/21031 https://github.com/golang/go/issues/21031
- PhilippGille 2y agoIt depends on the use case. From the Godoc: > The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex. Source: https://pkg.go.dev/sync#Map https://pkg.go.dev/sync#Map And regarding slow writes, those were recently improved in Go 1.24: > The implementation of sync.Map has been changed, improving performance, particularly for map modifications. For instance, modifications of disjoint sets of keys are much less likely to contend on larger maps, and there is no longer any ramp-up time required to achieve low-contention loads from the map. Source: https://go.dev/doc/go1.24#minor_library_changes https://go.dev/doc/go1.24#minor_library_changes ("sync" section)