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

introduce GetFn and PeekFn methods, for zero-copy access. #92

Merged
merged 1 commit into from
Nov 16, 2020

Conversation

raulk
Copy link
Contributor

@raulk raulk commented Nov 15, 2020

This commit introduces two methods GetFn and PeekFn. These methods provide zero-copy access to cache entries, avoiding allocs unless absolutely necessary.

They accept a function, and call it with a slice over the current underlying value of the key in memory. The slice is constrained in length and capacity. The RingBuf struct has been augmented with a Slice() method to support these operations.

The only case where these variants allocate is when the value wraps around the ring buffer.

This commit introduces two methods GetFn and PeekFn. These methods
provide zero-copy access to cache entries, avoiding allocs unless
absolutely necessary.

They accept a function, and call it with a slice over the current
underlying value of the key in memory. The slice is constrained
in length and capacity. The RingBuf struct has been augmented with
a Slice() method to support these operations.

The only case where these variants allocate is when the value wraps
around the ring buffer.
@coocood
Copy link
Owner

coocood commented Nov 16, 2020

Good job!
Thank you!

@coocood coocood merged commit b0e1f92 into coocood:master Nov 16, 2020
@basovnik
Copy link

basovnik commented Jul 7, 2022

Hi @raulk,

it seems that this commit caused degradation of performance. Could you check it please?

Reproducer: BenchmarkCacheGetWithBuf

Before:

BenchmarkCacheGetWithBuf
BenchmarkCacheGetWithBuf-8   	 2769759	       429.3 ns/op	       0 B/op	       0 allocs/op
PASS

After:

BenchmarkCacheGetWithBuf
BenchmarkCacheGetWithBuf-8   	 2484422	       528.1 ns/op	      24 B/op	       1 allocs/op
PASS

Shall I create an issue for it?

FYI @coocood

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

Successfully merging this pull request may close these issues.

3 participants