4 ms·
I've been looking into false sharing and other pitfalls of parallelism on multi-core systems. http://en.wikipedia.org/wiki/False_sharing http://en.wikipedia.or
by stcredzero 13y ago
I've been looking into false sharing and other pitfalls of parallelism on multi-core systems.
http://en.wikipedia.org/wiki/False_sharing http://en.wikipedia.org/wiki/False_sharing
It seems like our multi-core hardware isn't that well thought out yet. In particular, locks can cause false sharing. Even go channels are based on locks!
I would love to see some kind of annotation in golang for padding certain structs to cache-line boundaries. This value can be read from most processors, so it could be done in a cross-platform fashion. The gringo ring-channel library has to do its own padding to avoid false sharing.
https://github.com/textnode/gringo/blob/master/gringo.go https://github.com/textnode/gringo/blob/master/gringo.go
type Gringo struct {
padding1 [8]uint64
lastCommittedIndex uint64
padding2 [8]uint64
nextFreeIndex uint64
padding3 [8]uint64
readerIndex uint64
padding4 [8]uint64
contents [queueSize]Payload
padding5 [8]uint64
}
There was a similar technique for padding Java objects, but it now turns out that the JVM is smart enough to optimize the padding out!
- jamesmiller5 13y agoInteresting point. I doubt go would let something as low level like struct padding bubble up into the language though, perhaps there could be some kind of library level "Unsafe" field annotation, though I'd prefer the compiler to optimize this automatically.
- stcredzero 13y agoMy first thought is that it's going to be pretty challenging for a compiler to optimize this without runtime information.
- tjaerv 13y agoHere's a good article on solving false sharing in Java with cache-line padding: http://mechanical-sympathy.blogspot.com/2011/07/false-sharing.html http://mechanical-sympathy.blogspot.com/2011/07/false-sharin...