Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional buffer pool for item values #5

Merged
merged 8 commits into from
Aug 23, 2022

Conversation

LeviHarrison
Copy link

@LeviHarrison LeviHarrison commented Aug 21, 2022

This PR adds support for an optional Bytes pool to recycle the buffers used to hold cache item values. A "release" or "return" method is not provided as it is expected that the caller will have access to the pool itself.

name                 old time/op    new time/op    delta
ParseGetResponse-40    2.51µs ± 0%    2.59µs ± 0%   +3.03%  (p=1.000 n=1+1)

name                 old alloc/op   new alloc/op   delta
ParseGetResponse-40    4.32kB ± 0%    4.32kB ± 0%   -0.05%  (p=1.000 n=1+1)

name                 old allocs/op  new allocs/op  delta
ParseGetResponse-40      6.00 ± 0%      5.00 ± 0%  -16.67%  (p=1.000 n=1+1)

Ref: grafana/mimir#285

Signed-off-by: Levi Harrison <git@leviharrison.dev>
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Copy link
Collaborator

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

If we're going to make this change, it should be to reduce garbage-collection.
Your benchmark stats show zero change in bytes allocated, which is the main driver of GC overhead. Also a 3% slowdown. Suggests something is wrong, either with the code or the benchmark.

memcache/memcache.go Outdated Show resolved Hide resolved
memcache/memcache_test.go Outdated Show resolved Hide resolved
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Signed-off-by: Levi Harrison <git@leviharrison.dev>
@LeviHarrison
Copy link
Author

Suggests something is wrong, either with the code or the benchmark.

Indeed there was, a new reader was being allocated for every iteration. Fixed that, and here are the new numbers:

name                 old time/op    new time/op    delta
ParseGetResponse-40     481ns ± 0%     471ns ± 0%   -2.10%  (p=1.000 n=1+1)

name                 old alloc/op   new alloc/op   delta
ParseGetResponse-40     96.0B ± 0%     88.0B ± 0%   -8.33%  (p=1.000 n=1+1)

name                 old allocs/op  new allocs/op  delta
ParseGetResponse-40      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=1.000 n=1+1)

Signed-off-by: Levi Harrison <git@leviharrison.dev>
memcache/memcache.go Outdated Show resolved Hide resolved
memcache/memcache.go Outdated Show resolved Hide resolved
return &testPool{
pool: sync.Pool{
New: func() interface{} {
b := make([]byte, 0, dataSize+2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why +2 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the buffers are allocated with a capacity of 2 more than the data size to accommodate for the terminating \r\n (not included in the size metadata in the response). So for the case of our test pool, it will need to provide buffers than are 2 bigger than the data size.

it.Value = make([]byte, size+2)

Copy link
Collaborator

@bboreham bboreham Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you moved this up to the call, which seems more sensible to me.
Sorry, I misunderstood.
It doesn't matter hugely, since this is a test, but either passing in +2 from somewhere that knows the right size, or changing Get(sz) to call make when the pool is empty, would be better than hard-coding a +2 in the middle of the pool.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I moved it up.

Signed-off-by: Levi Harrison <git@leviharrison.dev>
@LeviHarrison
Copy link
Author

Now:

name                 old time/op    new time/op    delta
ParseGetResponse-40     780ns ± 0%     510ns ± 0%  -34.63%  (p=1.000 n=1+1)

name                 old alloc/op   new alloc/op   delta
ParseGetResponse-40      600B ± 0%       88B ± 0%  -85.33%  (p=1.000 n=1+1)

name                 old allocs/op  new allocs/op  delta
ParseGetResponse-40      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=1.000 n=1+1)

Signed-off-by: Levi Harrison <git@leviharrison.dev>
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Copy link
Collaborator

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good to go now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants