reduce bucket tail latency with 2 map rotation
Summary
Replace synchronous hash-map cleanup during bucket generation update (when chunks are full) with 2 rotating maps to improve the tail latency.
Previously, Set called cleanLocked whenever a bucket’s chunks are full. cleanLocked scanned the bucket index, allocated a replacement map, and copied the live entries while holding the write lock. The resulting pause scaled with the no. of entries in the bucket. It blocked both reads and writes to that bucket. This caused bad tail latency for read requests, especially when the no. of entries is high.
The proposed implementation maintains 2 maps:
mstores entries written during the current generation.mPrevretains entries from the previous generation that may still be live.
When the chunk ring wraps at gen+1, the maps rotate in O(1):
mbecomesmPrev,- the old
mPrevis discarded, - and a new empty
mis created.
This removes the full-map scan and reconstruction from the Set hot path (in other words, Set need not to call cleanLocked anymore, we only need cleanLocked before Save). In the benchmark below, average new-gen write latency decreases from 3ms ms to 258 ns - this is over 10,000x lower.
The trade-off is that Get may need to check two maps - m then mPrev - when an entry is not found in m. This may result in slightly higher read latency. Based on the benchmark below, which shows the write latency of the same generation, we can proxy that the read-latency degradation is less than 15%.
Benchmark
Apple M4 Max
Go 1.24.13.
Benchmark run prior to this change
> go test -run '^$' -bench '^BenchmarkBucketSetAtNextGen$' -benchtime=5000000x -count=5 -benchmem
goos: darwin
goarch: arm64
pkg: github.com/VictoriaMetrics/fastcache
cpu: Apple M4 Max
BenchmarkBucketSetAtNextGen-16 5000000 115.2 ns/op 4999943 cur-gen/count 47.14 cur-gen/ns/avg 108125 cur-gen/ns/max 57.00 next-gen/count 3175191 next-gen/ns/avg 3946000 next-gen/ns/max 80 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 116.6 ns/op 4999943 cur-gen/count 48.47 cur-gen/ns/avg 127000 cur-gen/ns/max 57.00 next-gen/count 3202607 next-gen/ns/avg 4006500 next-gen/ns/max 80 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 114.3 ns/op 4999943 cur-gen/count 47.01 cur-gen/ns/avg 123625 cur-gen/ns/max 57.00 next-gen/count 3153882 next-gen/ns/avg 3365667 next-gen/ns/max 80 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 115.3 ns/op 4999943 cur-gen/count 47.80 cur-gen/ns/avg 132625 cur-gen/ns/max 57.00 next-gen/count 3185975 next-gen/ns/avg 3460541 next-gen/ns/max 80 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 114.5 ns/op 4999943 cur-gen/count 47.19 cur-gen/ns/avg 126584 cur-gen/ns/max 57.00 next-gen/count 3163214 next-gen/ns/avg 3297459 next-gen/ns/max 80 B/op 0 allocs/op
PASS
ok github.com/VictoriaMetrics/fastcache 3.505s
Benchmark run with this change
>go test -run '^$' -bench '^BenchmarkBucketSetAtNextGen$' -benchtime=5000000x -count=5 -benchmem
goos: darwin
goarch: arm64
pkg: github.com/VictoriaMetrics/fastcache
cpu: Apple M4 Max
BenchmarkBucketSetAtNextGen-16 5000000 85.80 ns/op 4999943 cur-gen/count 55.94 cur-gen/ns/avg 138291 cur-gen/ns/max 57.00 next-gen/count 258.0 next-gen/ns/avg 709.0 next-gen/ns/max 54 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 83.65 ns/op 4999943 cur-gen/count 54.69 cur-gen/ns/avg 122000 cur-gen/ns/max 57.00 next-gen/count 233.2 next-gen/ns/avg 666.0 next-gen/ns/max 54 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 83.99 ns/op 4999943 cur-gen/count 54.84 cur-gen/ns/avg 91042 cur-gen/ns/max 57.00 next-gen/count 571.6 next-gen/ns/avg 19291 next-gen/ns/max 54 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 83.30 ns/op 4999943 cur-gen/count 54.48 cur-gen/ns/avg 145334 cur-gen/ns/max 57.00 next-gen/count 209.1 next-gen/ns/avg 333.0 next-gen/ns/max 54 B/op 0 allocs/op
BenchmarkBucketSetAtNextGen-16 5000000 83.58 ns/op 4999943 cur-gen/count 54.68 cur-gen/ns/avg 97625 cur-gen/ns/max 57.00 next-gen/count 511.0 next-gen/ns/avg 16167 next-gen/ns/max 54 B/op 0 allocs/op
PASS
ok github.com/VictoriaMetrics/fastcache 2.697s