Last active
September 28, 2018 13:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Author
narqo
commented
Sep 28, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment