Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

sync.Map is kind of a big deal.


Also a shining example of their poor type system.


how so? how would a type system have helped here


Since everything in sync.Map appears to be laundered through interface{}, you lose compile-time type safety where you use it.


The type system issues can also be seen in:

* you get a note that a map must not be copied, but you won't actually be prevented from doing so?

* iteration can't use the native construct (no generic interface -> no generic iteration)


Another concern (big or little depending on your circumstances) many common things stored in maps, strings, primitives, incur an allocation on conversion to interface{}.

Coupled with a fairly inefficient GC it means I would need to think twice before using this.


What's inefficient about Go's GC ?


I think pcwalton expressed it clearly here

https://news.ycombinator.com/item?id=14380575


Thanks. I also found this other discussion useful: https://news.ycombinator.com/item?id=12042302


Use it, then profile later?


Well, you might not want to put it in a public API anyway since it shouldn't be copied.

Better if you wrap it somehow. Then it's similar to an unsafe section; you need to be careful but it's self-contained.


I"m not sure that it is, given that the empty interface is more annoying (IMO) than just wrapping a native map in a sync.RWMutex.

Especially considering that sync.Map just uses a mutex under the hood: https://github.com/golang/go/blob/master/src/sync/map.go#L19

edit: although reading the comments, apparently there are conditions where the mutex does not always need to be held. This would be an improvement over sync.RWMutex wrapping.


It doesn't seem to support composition in any way though, there's no "transactional" view of the map.


I don't think it's a general data structure so much as an alternative way to manage concurrency without a mess of channels. "Synchronized maps" and "communicating shared processes" being two important ways of structuring concurrent programs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: