2 ms·
My favorite example is https://golang.org/pkg/sync/#Map https://golang.org/pkg/sync/#Map, which should have been a typesafe drop-in replacement for map[K]V, but
by erik_seaberg 7y ago
My favorite example is https://golang.org/pkg/sync/#Map https://golang.org/pkg/sync/#Map, which should have been a typesafe drop-in replacement for map[K]V, but that isn't possible even within the standard library.
- throwaway894345 7y agoYeah, I tend not to write or use these data structures. If I want a concurrent map, I usually write my own anyway. It’s easier to add the locks I want than to keep track of how sync.Map thinks about locking a map. I understand that this attitude angers a lot of people who believe that everything should be as DRY as possible, but this way works better for me.
- vardump 7y agoI don't know about sync.Map, but many concurrent maps don't require any locking (or require rarely). Protecting map with a lock is a very naive solution, it'll perform badly with a lot of concurrent writes.
- erik_seaberg 7y agohttps://golang.org/src/sync/map.go https://golang.org/src/sync/map.go is not completely lock-free but yeah, I think it's trying to batch structural changes to the map (and atomic compare-and-set on existing entries) which isn't something I would rewrite casually. There's just no good reason that you can't reuse it without rewriting all call sites. []T and map[K]V and chan T should implement interfaces, everyone should always use those interfaces, and monomorphization should skip the runtime dispatch.