grafana / loki

Like Prometheus, but for logs.
https://grafana.com/loki
GNU Affero General Public License v3.0
22.73k stars 3.31k forks source link

perf(blooms): mempool no longer zeroes out buffers unnecessarily #13282

Closed owen-d closed 1 week ago

owen-d commented 1 week ago

Dramatically improves the speed of mempool usage. I noticed some code in the mempool which iterated over buffers on every get() call. Guessing this to be expensive, I added a few benchmarks -- this is one of those rare -100% speed improvements. The reason this is safe because only the length of the provided []byte is important -- NOT it's contents. I verified this everywhere it was used (I knew this was the expected contract already), added some benchmarks, and we saw a great improvement.

goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/util/mempool
              │   /tmp/old.txt   │             /tmp/new.txt             │
              │      sec/op      │   sec/op     vs base                 │
Slab/1KB-10         121.35n ± 1%   33.80n ± 1%   -72.15% (p=0.000 n=10)
Slab/1MB-10       11222.00n ± 2%   33.82n ± 0%   -99.70% (p=0.000 n=10)
Slab/128MB-10   1420680.00n ± 2%   33.75n ± 0%  -100.00% (p=0.000 n=10)
geomean              12.46µ        33.79n        -99.73%

              │ /tmp/old.txt │            /tmp/new.txt             │
              │     B/op     │    B/op     vs base                 │
Slab/1KB-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Slab/1MB-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Slab/128MB-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                    ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

              │ /tmp/old.txt │            /tmp/new.txt             │
              │  allocs/op   │ allocs/op   vs base                 │
Slab/1KB-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Slab/1MB-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Slab/128MB-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                    ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean