gomemcache icon indicating copy to clipboard operation
gomemcache copied to clipboard

Faster scanning of memcache responses

Open bboreham opened this issue 2 years ago • 2 comments

Motivation for this change is at https://github.com/grafana/mimir/issues/285#issuecomment-1029143100, where a large program spends 12% of its CPU in this one function.

Re-coded scanGetResponseLine() using IndexByte() instead of SScanf(). Also added a benchmark.

name                   old time/op    new time/op    delta
ScanGetResponseLine-4    4.06µs ± 1%    0.13µs ± 4%  -96.89%  (p=0.008 n=5+5)

name                   old alloc/op   new alloc/op   delta
ScanGetResponseLine-4      128B ± 0%       24B ± 0%  -81.25%  (p=0.008 n=5+5)

name                   old allocs/op  new allocs/op  delta
ScanGetResponseLine-4      7.00 ± 0%      1.00 ± 0%  -85.71%  (p=0.008 n=5+5)

This PR is broadly similar to #74, but about twice as fast on my benchmark. The benchmark in #74 spends <2% of its time in scanGetResponseLine() so doesn't show the effect so well.

The partial copy of strings.Cut() in this PR allows the code to still build with pre-1.18 Go.

bboreham avatar Aug 15 '22 14:08 bboreham

I added some tests and checked for int overflow.

I'm not too familiar with what memcached responses look like; I made a brief attempt to find similar tests for other languages and came up blank, so did the edge cases.

bboreham avatar Nov 02 '22 17:11 bboreham