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

Make Cache.Get allocation-free #162

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from

Conversation

yiftachkarkason
Copy link

Changes

  • Changed applyOptions and the Option[K,V] to be non-allocating by avoiding pointers
  • Added the Test_Get_DoesNotAllocate to make sure that Get does not allocate (it fails before this commit)

Why
We encountered a performance issue when we heavily used the Cache, and narrowed it down to the allocation made by Cache.Get as part of the options processing.

Thank you for the great package!

Copy link
Contributor

@davseby davseby left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.

cache_test.go Outdated Show resolved Hide resolved
Changed applyOptions and the Option[K,V] to be non-allocating by avoiding pointers

Added the Test_Get_DoesNotAllocate to make sure that Get does not allocate (it fails before this commit)
Copy link
Contributor

@swithek swithek left a comment

Choose a reason for hiding this comment

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

I think we can merge this? @davseby

Well done @yiftachkarkason

Comment on lines +1312 to +1325
func allocsForSingleRun(f func()) (numAllocs int) {
// `testing.AllocsPerRun` "warms up" the function for a single run before
// measuring allocations, so we need do nothing on the first run.
firstRun := false
numAllocs = int(testing.AllocsPerRun(1, func() {
if !firstRun {
firstRun = true
return
}

f()
}))
return
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the package's naming, I think the allocsPerSingleRun would be a more appropriate function name. What is more, I don't see a point in using a named return here, the function scope is quite small, so something like this should be sufficient. Thanks, after this I will merge the PR. 🚀

Suggested change
func allocsForSingleRun(f func()) (numAllocs int) {
// `testing.AllocsPerRun` "warms up" the function for a single run before
// measuring allocations, so we need do nothing on the first run.
firstRun := false
numAllocs = int(testing.AllocsPerRun(1, func() {
if !firstRun {
firstRun = true
return
}
f()
}))
return
}
func allocsPerSingleRun(f func()) int {
// `testing.AllocsPerRun` "warms up" the function for a single run before
// measuring allocations, so we need to do nothing on the first run.
var firstRun bool
return int(testing.AllocsPerRun(1, func() {
if !firstRun {
firstRun = true
return
}
f()
}))
}

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