Replace ItemCache sync.Map with Otter W-TinyLFU cache for better memory management#1251
Open
zhangzhenghao wants to merge 1 commit intogorse-io:masterfrom
Open
Replace ItemCache sync.Map with Otter W-TinyLFU cache for better memory management#1251zhangzhenghao wants to merge 1 commit intogorse-io:masterfrom
zhangzhenghao wants to merge 1 commit intogorse-io:masterfrom
Conversation
…ry management This change replaces the unbounded sync.Map in ItemCache with Otter's W-TinyLFU cache, which provides automatic eviction based on access patterns. Key changes: - ItemCache now uses otter.Cache[string, *data.Item] instead of sync.Map - Cache size is calculated as max(1024, itemCount/10) where itemCount is obtained from DataClient.CountItems() - W-TinyLFU eviction policy efficiently handles long-tail distribution data by quickly evicting rarely accessed items while retaining popular ones Benefits: - Memory usage is bounded and predictable - Better hit rate for long-tail distributions compared to pure LRU - Automatic eviction of cold items reduces memory pressure
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1251 +/- ##
==========================================
- Coverage 73.01% 72.88% -0.14%
==========================================
Files 91 91
Lines 16694 16713 +19
==========================================
- Hits 12189 12181 -8
- Misses 3262 3287 +25
- Partials 1243 1245 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR replaces the unbounded
sync.MapinItemCachewith Otter's W-TinyLFU cache, providing automatic eviction based on access patterns.Changes
ItemCache structure change:
sync.Map→otter.Cache[string, *data.Item]max(1024, itemCount/10)itemCountis obtained fromDataClient.CountItems(ctx)at cache creation timeAPI changes:
NewItemCache(client data.Database)→NewItemCache(client data.Database, itemCount int)GetIfPresent()andSet()methodsW-TinyLFU eviction policy:
Benefits
Cache Size Calculation
Testing
All existing tests pass:
worker/pipeline_test.go✓worker/worker_test.go✓References