Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit cce8a97

Browse files
committed
N allocs -> 1 alloc -> bit faster still
taskset --cpu-list 5 go test -bench Benchmark . -benchmem goos: linux goarch: amd64 pkg: github.com/grafana/metrictank/mdata/cache BenchmarkAddAsc 1000000 1053 ns/op 145 B/op 3 allocs/op BenchmarkAddDesc1 10000 1456538 ns/op 21720 B/op 7 allocs/op BenchmarkAddDesc4 10000 1452870 ns/op 21729 B/op 9 allocs/op BenchmarkAddDesc64 9984 1449829 ns/op 21700 B/op 9 allocs/op BenchmarkAddRangeAsc 3000000 366 ns/op 117 B/op 0 allocs/op BenchmarkAddRangeDesc4 10000 363554 ns/op 5503 B/op 1 allocs/op BenchmarkAddRangeDesc64 99968 277954 ns/op 3276 B/op 0 allocs/op PASS ok github.com/grafana/metrictank/mdata/cache 84.321s benchcmp post2.txt post3.txt benchmark old ns/op new ns/op delta BenchmarkAddAsc 1229 1053 -14.32% BenchmarkAddDesc1 1454771 1456538 +0.12% BenchmarkAddDesc4 1459969 1452870 -0.49% BenchmarkAddDesc64 1453576 1449829 -0.26% BenchmarkAddRangeAsc 945 366 -61.27% BenchmarkAddRangeDesc4 363506 363554 +0.01% BenchmarkAddRangeDesc64 278120 277954 -0.06% benchmark old allocs new allocs delta BenchmarkAddAsc 3 3 +0.00% BenchmarkAddDesc1 7 7 +0.00% BenchmarkAddDesc4 9 9 +0.00% BenchmarkAddDesc64 9 9 +0.00% BenchmarkAddRangeAsc 1 0 -100.00% BenchmarkAddRangeDesc4 2 1 -50.00% BenchmarkAddRangeDesc64 1 0 -100.00% benchmark old bytes new bytes delta BenchmarkAddAsc 145 145 +0.00% BenchmarkAddDesc1 21720 21720 +0.00% BenchmarkAddDesc4 21729 21729 +0.00% BenchmarkAddDesc64 21700 21700 +0.00% BenchmarkAddRangeAsc 137 117 -14.60% BenchmarkAddRangeDesc4 5502 5503 +0.02% BenchmarkAddRangeDesc64 3276 3276 +0.00%
1 parent fce7836 commit cce8a97

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

mdata/cache/ccache_metric.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
8686
mc.Lock()
8787
defer mc.Unlock()
8888

89+
// pre-allocate 1 slice, cheaper than allocating one by one
90+
chunks := make([]CCacheChunk, 0, len(itergens))
91+
8992
// handle the first one
9093
itergen := itergens[0]
9194
ts := itergen.Ts
@@ -111,12 +114,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
111114

112115
// add chunk if we don't have it yet (most likely)
113116
if _, ok := mc.chunks[ts]; !ok {
114-
mc.chunks[ts] = &CCacheChunk{
117+
chunks = append(chunks, CCacheChunk{
115118
Ts: ts,
116119
Prev: prev,
117120
Next: itergens[1].Ts,
118121
Itgen: itergen,
119-
}
122+
})
123+
mc.chunks[ts] = &chunks[len(chunks)-1]
120124
if addKeysDirect {
121125
mc.keys = append(mc.keys, ts)
122126
}
@@ -130,12 +134,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
130134
ts := itergen.Ts
131135
// add chunk if we don't have it yet (most likely)
132136
if _, ok := mc.chunks[ts]; !ok {
133-
mc.chunks[ts] = &CCacheChunk{
137+
chunks = append(chunks, CCacheChunk{
134138
Ts: ts,
135139
Prev: prev,
136140
Next: itergens[i+1].Ts,
137141
Itgen: itergen,
138-
}
142+
})
143+
mc.chunks[ts] = &chunks[len(chunks)-1]
139144
if addKeysDirect {
140145
mc.keys = append(mc.keys, ts)
141146
}
@@ -162,12 +167,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
162167

163168
// add chunk if we don't have it yet (most likely)
164169
if _, ok := mc.chunks[ts]; !ok {
165-
mc.chunks[ts] = &CCacheChunk{
170+
chunks = append(chunks, CCacheChunk{
166171
Ts: ts,
167172
Prev: prev,
168173
Next: next,
169174
Itgen: itergen,
170-
}
175+
})
176+
mc.chunks[ts] = &chunks[len(chunks)-1]
171177
if addKeysDirect {
172178
mc.keys = append(mc.keys, ts)
173179
}

0 commit comments

Comments
 (0)