Skip to content

Instantly share code, notes, and snippets.

@narqo
Last active September 28, 2018 13:32
Show Gist options
  • Save narqo/bb71115286f8818497cfc89894a699ca to your computer and use it in GitHub Desktop.
Save narqo/bb71115286f8818497cfc89894a699ca to your computer and use it in GitHub Desktop.
An example of strings interning. See the discussion https://github.com/golang/go/issues/5160
package main
import (
"sync"
)
type oneValueK struct {
k1, k2, k3 string
}
var oneValueKeyPool = sync.Pool{
New: func() interface{} {
return make(map[oneValueK]string)
},
}
// produces key such as "tracker::id123::redirect_url".
func oneValueKey(val1Name, val1, suffix string) string {
m := oneValueKeyPool.Get().(map[oneValueK]string)
ovk := oneValueK{val1Name, val1, suffix}
k, ok := m[ovk]
if ok {
oneValueKeyPool.Put(m)
return k
}
key := val1Name + "::" + val1 + "::" + suffix
m[ovk] = key
oneValueKeyPool.Put(m)
return key
}
/*
= go test ./core/redis -run XX -bench . -count 5
goos: darwin
goarch: amd64
pkg: github.com/adjust/backend/core/redis
Benchmark_oneValueKey-4 20000000 77.3 ns/op 194.14 MB/s 0 B/op 0 allocs/op
Benchmark_oneValueKey-4 20000000 77.2 ns/op 194.19 MB/s 0 B/op 0 allocs/op
Benchmark_oneValueKey-4 20000000 76.9 ns/op 194.97 MB/s 0 B/op 0 allocs/op
Benchmark_oneValueKey-4 20000000 77.0 ns/op 194.71 MB/s 0 B/op 0 allocs/op
Benchmark_oneValueKey-4 20000000 77.1 ns/op 194.56 MB/s 0 B/op 0 allocs/op
*/
func Benchmark_oneValueKey(b *testing.B) {
k1 := "test1"
k2 := "test2"
k3 := "test3"
b.SetBytes(int64(len(k1)+len(k2)+len(k3)))
b.ReportAllocs()
var key string
for i := 0; i < b.N; i++ {
key = oneValueKey(k1, k2, k3)
if key == "" {
b.Fatal("unexpected result")
}
}
_ = key
}
@narqo
Copy link
Author

narqo commented Sep 28, 2018

vladimir [15:13]
After I re-read the issue, the point there is to reduce memory usage by sacrificing some CPU. Both are not the case for us: our hardware is too beefy to notice the benefits. It’s really an optimisation for horizontal scaling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment