Go Performance Tales
jmoiron.net
jmoiron.net
This is the kind of detail most developers would not be aware of, and to be fair, even now knowing it exists the only reference I can google up at golang.org is the C source code of runtime/alg.c where you will see…
23 if(use_aeshash) {
24 runtime·aeshash(h, s, a);
25 return;
26 }
… no hint that it might reduce your hosting costs by 33% or account for some huge variation in performance between one test machine and the next, or even individual runs if you are spinning up cloud instances to do your testing.␄
¹ Does your CPU have the AES, SSE3 and SSE4.1 cpu capability bits all turned on? If so, you will hash mightily! Do you even know where to look to check?
Turns out that a library I was using set `gcc -Mnative` by default, which was emitting some instructions which weren't valid everywhere. I was requesting the same virtual hardware, but the physical variations caught me off guard.
Hint: it probably isn't /r/programming :)
To be fair, HN doesn't feel like it has a hacker spirit to it. It's too preoccupied with business crap and self-promotion.
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
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!http://mechanical-sympathy.blogspot.com/2011/07/false-sharin...
Also everything he said about channels also holds true in my experience. I haven't tried writing a C library for Go yet but his discovery is pretty interesting for when I dive into that.
[1] : https://github.com/timtadh/data-structures/blob/master/trie/...
tl;dr still very polyglot here.
You already have a perfect hash function :).
If the key-space is relatively sparse, this will waste more memory than the hash, of course.
I'm a little sceptical of this -- type assertions are fast but it's an extra step to initializing a struct. It would have been nice to see tests done comparing map[string]struct{} to map[int]struct{} and comparing map[string]* Metric to map[int]* Metric.
Also, there is no way to escape an asterisk, so I apologize for the awkward space after each one.