-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
Signed-off-by: Levi Harrison <git@leviharrison.dev>
c1b4d09
to
b9fba73
Compare
There was a problem hiding this 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.
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Indeed there was, a new reader was being allocated for every iteration. Fixed that, and here are the new numbers:
|
Signed-off-by: Levi Harrison <git@leviharrison.dev>
memcache/memcache_test.go
Outdated
return &testPool{ | ||
pool: sync.Pool{ | ||
New: func() interface{} { | ||
b := make([]byte, 0, dataSize+2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why +2 ?
There was a problem hiding this comment.
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.
gomemcache/memcache/memcache.go
Line 510 in 3a2f55c
it.Value = make([]byte, size+2) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
Now:
|
Signed-off-by: Levi Harrison <git@leviharrison.dev>
Signed-off-by: Levi Harrison <git@leviharrison.dev>
There was a problem hiding this 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.
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.
Ref: grafana/mimir#285