Repository navigation
perf(lru): switch internal/cache/lru to github.com/google/go-lru ArenaRadixCache - #5250
kislaykishore wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request replaces the custom LRU cache implementation in internal/cache/lru with the external github.com/google/go-lru library. It introduces a scaling mechanism for small cache capacities and a deferred overflow tracking system to handle temporary size violations during UpdateSize operations. The test suite is also expanded with several new test cases covering edge cases like zero-size entries, zero max size, and deferred overflow scenarios. The reviewer suggested optimizing the deferred overflow path in Insert by pre-allocating the keys slice with the exact capacity to avoid unnecessary slice re-allocations.
| var keys []string | ||
| for k := range c.cache.Keys() { | ||
| keys = append(keys, k) | ||
| } |
There was a problem hiding this comment.
In the deferred overflow path, copying all keys from the cache into a slice can be expensive. Pre-allocating the slice with the exact capacity using c.cache.Stats().Len avoids multiple slice re-allocations and copying overhead during the loop.
| var keys []string | |
| for k := range c.cache.Keys() { | |
| keys = append(keys, k) | |
| } | |
| keys := make([]string, 0, c.cache.Stats().Len) | |
| for k := range c.cache.Keys() { | |
| keys = append(keys, k) | |
| } |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## perf/lookup-count-memory-opt #5250 +/- ##
================================================================
+ Coverage 85.13% 85.21% +0.07%
================================================================
Files 181 181
Lines 19850 19946 +96
================================================================
+ Hits 16900 16997 +97
+ Misses 2949 2948 -1
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
4161765 to
cebe590
Compare
Description
Switch
internal/cache/lrufrom a customcontainer/list+map[string]*list.Elementimplementation togithub.com/google/go-lru(v1.2.0)ArenaRadixCache, and deduplicate cached object/folder names ininternal/cache/metadata:map[string]*list.Elementandcontainer/listnodes) withgolru.NewArenaRadixCache[entry], significantly reducing heap object count, GC pointer-scanning overhead, and per-entry memory footprint while providingO(subtree)prefix deletion viaDeletePrefix.MinObject.NameinstatCacheBucketView.InsertandFolder.NameinstatCacheBucketView.InsertFolder(on shallow copies so caller structs are never mutated), and reconstruct.Namefrom the lookup key inLookUpDetailandLookUpFolderDetail. This allows the uncompressed GCS JSON response strings (encoding/json) to be garbage-collected once key bytes are stored inArenaRadixCache's compressedprefixArena.golrucapacity and entry weights by1 << 30when0 < maxSize < 1 << 30(and useminGolruMaxSize = 1 << 30whenmaxSize == 0) so zero-size entries never hitgo-lru's zero-weight entry count cap while large capacities (>= 1 << 30, includingmath.MaxUint64) usescale = 1without overflow.UpdateSizedeferred-overflow semantics (hasDeferredOverflow,deferredGolruSize,reconcileDeferred) so incremental size growth (e.g., sparse file downloads) defers eviction until the nextInsert()without altering LRU order.SizeObserveraccounting acrossInsert,Erase,UpdateWithoutChangingOrder,UpdateSize, andEraseEntriesWithGivenPrefixwithout registeringgo-lrueviction callbacks, avoidingevictCoordinatoroverhead on hot paths.Link to the issue in case of a bug fix.
b/585048023
Testing details
make build.internal/cache/lru/lru_test.gocovering zero-size entries exceedingmaxSize,maxSize == 0(with and without invariant checks), large capacities (1 << 30andmath.MaxUint64), production-modeErase+UpdateSize+Insert, andUpdateSizedeferred overflow acrossInsert, overwrite,Erase,EraseEntriesWithGivenPrefix, andWithSizeObserver; and addedTest_InsertClearsCachedNameAndLookUpRestoresItandTest_InsertFolderClearsCachedNameAndLookUpFolderRestoresItininternal/cache/metadata/stat_cache_test.go. Verifiedgo test -race ./internal/cache/lru/...,go test -race ./internal/cache/metadata/..., andgo test -race ./internal/storage/caching/....Any backward incompatible change? If so, please explain.
N/A