-
Notifications
You must be signed in to change notification settings - Fork 123
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
base: v3
Are you sure you want to change the base?
Make Cache.Get allocation-free #162
Conversation
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.
Thank you for your contribution.
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)
2614d4e
to
826a563
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.
I think we can merge this? @davseby
Well done @yiftachkarkason
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 | ||
} |
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.
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. 🚀
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() | |
})) | |
} |
Changes
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!