Skip to content

perf(lru): switch internal/cache/lru to github.com/google/go-lru ArenaRadixCache - #5250

Draft
kislaykishore wants to merge 1 commit into
perf/lookup-count-memory-optfrom
perf/lru-memory-opt
Draft

kislaykishore wants to merge 1 commit into
perf/lookup-count-memory-optfrom
perf/lru-memory-opt

Conversation

@kislaykishore

@kislaykishore kislaykishore commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Switch internal/cache/lru from a custom container/list + map[string]*list.Element implementation to github.com/google/go-lru (v1.2.0) ArenaRadixCache, and deduplicate cached object/folder names in internal/cache/metadata:

  • Replace the pointer-heavy per-entry representation (map[string]*list.Element and container/list nodes) with golru.NewArenaRadixCache[entry], significantly reducing heap object count, GC pointer-scanning overhead, and per-entry memory footprint while providing O(subtree) prefix deletion via DeletePrefix.
  • Clear MinObject.Name in statCacheBucketView.Insert and Folder.Name in statCacheBucketView.InsertFolder (on shallow copies so caller structs are never mutated), and reconstruct .Name from the lookup key in LookUpDetail and LookUpFolderDetail. This allows the uncompressed GCS JSON response strings (encoding/json) to be garbage-collected once key bytes are stored in ArenaRadixCache's compressed prefixArena.
  • Scale golru capacity and entry weights by 1 << 30 when 0 < maxSize < 1 << 30 (and use minGolruMaxSize = 1 << 30 when maxSize == 0) so zero-size entries never hit go-lru's zero-weight entry count cap while large capacities (>= 1 << 30, including math.MaxUint64) use scale = 1 without overflow.
  • Preserve GCSFuse's UpdateSize deferred-overflow semantics (hasDeferredOverflow, deferredGolruSize, reconcileDeferred) so incremental size growth (e.g., sparse file downloads) defers eviction until the next Insert() without altering LRU order.
  • Preserve SizeObserver accounting across Insert, Erase, UpdateWithoutChangingOrder, UpdateSize, and EraseEntriesWithGivenPrefix without registering go-lru eviction callbacks, avoiding evictCoordinator overhead on hot paths.

Link to the issue in case of a bug fix.

b/585048023

Testing details

  1. Manual - Verified compilation and static analysis via make build.
  2. Unit tests - Added unit tests in internal/cache/lru/lru_test.go covering zero-size entries exceeding maxSize, maxSize == 0 (with and without invariant checks), large capacities (1 << 30 and math.MaxUint64), production-mode Erase + UpdateSize + Insert, and UpdateSize deferred overflow across Insert, overwrite, Erase, EraseEntriesWithGivenPrefix, and WithSizeObserver; and added Test_InsertClearsCachedNameAndLookUpRestoresIt and Test_InsertFolderClearsCachedNameAndLookUpFolderRestoresIt in internal/cache/metadata/stat_cache_test.go. Verified go test -race ./internal/cache/lru/..., go test -race ./internal/cache/metadata/..., and go test -race ./internal/storage/caching/....
  3. Integration tests - N/A

Any backward incompatible change? If so, please explain.

N/A

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread internal/cache/lru/lru.go
Comment on lines +284 to +287
var keys []string
for k := range c.cache.Keys() {
keys = append(keys, k)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.87755% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.21%. Comparing base (619b0b5) to head (cebe590).

Files with missing lines Patch % Lines
internal/cache/lru/lru.go 93.12% 9 Missing ⚠️
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              
Flag Coverage Δ
unittests 85.21% <93.87%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant