Hacker News new | ask | show | jobs
by Insanity 2622 days ago
Speaking of interfaces in Go, can anyone explain this particular result? We've done a benchmark recently at work to test which way we would best represent a set. (Linux, go1.12)

      map[string]bool
      map[string]struct{}
      map[string]interface{} // where we put an empty struct in it.
results:

     BenchmarkBoolSet-4              5886572           223 ns/op          39 B/op           0 allocs/op
     BenchmarkStructSet-4            6716685           206 ns/op          32 B/op           0 allocs/op
     BenchmarkInterfaceSet-4         4299072           313 ns/op         115 B/op           0 allocs/op

(I'm aware this is a bit of a tangent to TFA)
1 comments

Can’t really say what’s going on here without seeing some code.
Well the code is the same for all of them (but when using a bool map we assign true instead of empty struct). So we run this code inside a benchmarking function:

      m := map[int]interface{}
      // or m := map[int]struct{}
      for i := 0; i < t.N; i++ {
           m[i] = struct{}{}
      }
edit: fix code.
I'm speculating, but sizeof(bool) == 1, sizeof(struct{}) == 0 and sizeof(interface{}) == 8, so might be as simple as that.

https://play.golang.org/p/5acy87JLD3i

Yup that well could be it!