-
-
Notifications
You must be signed in to change notification settings - Fork 12
Optimize core cache operations for performance, Go 1.24 #18
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
Conversation
Improve performance in frequently used cache methods: 1. Values method: - Move time.Now() outside of lock - Simplify iteration 2. DeleteExpired method: - Replace key slice allocation with direct list traversal - Cache time.Now() call for efficiency, move out of lock - Avoid redundant map lookups - Properly handle list traversal during element removal 3. Add method: - Cache time.Now() and move out of lock All optimizations preserve API compatibility while reducing allocations and CPU work. Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
|
|
Moving time.Now() before acquiring the lock reduces the critical section duration, which can significantly improve concurrency in high-contention scenarios. The exact timestamp precision is not critical (a few microseconds earlier doesn't matter), and potentially you could say that the method called time is the mark. And time.Now() can be relatively expensive (vDSO/syscall), so performing it outside the lock helps reduce contention. |
Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
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.
Pull Request Overview
This pull request optimizes core cache operations for performance improvements in Go 1.24 by reducing lock duration and avoiding redundant operations.
- Time-critical methods (Set/addWithTTL, Values, and DeleteExpired) now cache time.Now() outside of locks.
- The deletion of expired elements is streamlined by directly traversing the eviction list.
- Legacy helper removeOldestIfExpired is removed in favor of inline removal logic.
Files not reviewed (1)
- v3/go.mod: Language not supported
Comments suppressed due to low confidence (1)
v3/cache.go:207
- [nitpick] Consider using a more conventional comparison such as now.Before(expiration) for clarity.
if !now.After(ent.Value.(*cacheItem[K, V]).expiresAt) {
- Move ttl check and time.Now() outside lock to reduce lock contention - Optimize DeleteExpired with direct list traversal - Remove unused removeOldestIfExpired method - Use same inline expiration check
- Update benchmark results in main README with latest performance metrics - Update benchmarks README with current numbers - Adjust performance improvement percentages to reflect latest changes
Pull Request Test Coverage Report for Build 14173478019Details
💛 - Coveralls |
|
I've added the same optimisations to v1 and v2, re-run the benchmarks and updated the results in readme, and also reverted the upgrade of go version to 1.24 as it's up to the caller and doesn't make sense to make a requirement from the library from my perspective. I'll release the new version once it's merged. @umputun, could you please take a look? |
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.
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Improve performance in frequently used cache methods:
Values method:
DeleteExpired method:
Add method:
All optimizations preserve API compatibility while reducing allocations and CPU work.