Repository navigation
fix(cache): count key footprint in MokaCacheBackend eviction weight - #7573
Merged
wjones127 merged 3 commits intoJul 2, 2026
Merged
Conversation
The moka weigher only charged the value's `size_bytes` and ignored the key. Workloads storing many small values under long keys — e.g. full dataset/index URIs used as key prefixes — therefore under-count memory and can grow well past the configured `max_capacity`, since moka evicts by weighted size. Charge each entry for its key too: add a `DeepSizeOf` impl for `InternalCacheKey` (the `&'static str` type name is a borrow and costs nothing) plus a `key_footprint` helper summing the struct, its owned string bytes, and moka's wrapping `Arc`. The weigher and the synchronous `approx_size_bytes` both use it so eviction and reported size stay consistent. Co-authored-by: Cursor <[email protected]>
The prefix `Arc<str>` is shared by every entry under a cache handle and is not freed when a single entry is evicted, so counting it per entry (via `DeepSizeOf`) systematically over-counted and made the weighted size diverge from real memory, evicting too early. Charge only the reclaimable per-entry cost: the `InternalCacheKey` struct plus the unique `key` string's bytes. Drops the now-unused `DeepSizeOf` impl. Co-authored-by: Cursor <[email protected]>
Assert the exact `weighted_size` for a known key/value instead of loose bounds, so both the unique-key charge and the zero prefix contribution are pinned. Trim `key_footprint`'s doc to two lines. Co-authored-by: Cursor <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
wjones127
self-requested a review
July 2, 2026 15:52
wjones127
approved these changes
Jul 2, 2026
wjones127
left a comment
Contributor
There was a problem hiding this comment.
This is a clean fix. Thank you!
6 tasks done
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
MokaCacheBackend's weigher charged only the value'ssize_bytesand ignored the key entirely:Since moka evicts by weighted size, workloads that store many small values under long, high-cardinality keys under-count their memory and can grow past the configured
max_capacity. The key (InternalCacheKey: twoArc<str>+ a&'static str) was effectively free.Change
A per-entry weigher should approximate the marginal, reclaimable cost of an entry — what is freed when that one entry is evicted:
key_footprint(key) = size_of::<InternalCacheKey>() + key.key().len()— the struct plus the entry's uniquekeystring.approx_size_bytes(DeepSizeOfreporting) so the two stay consistent.The
prefixis deliberately excluded.build_keyclones the prefixArc<str>(shared) and allocates a freshkeyper entry (unique). The prefix is one shared allocation per cache handle and is not reclaimed when a single entry is evicted, so chargingprefix.len()per entry would over-count in proportion to entry count (e.g. a 60-byte S3 prefix × 10k entries = 600 KB of phantom weight) and make the cache evict too early.Accounting stays in the backend that actually stores the key, so other
CacheBackendimpls are unaffected.Test plan
test_cache_weighs_key_footprint— a 10 KB key contributes tosize_bytes(); fails before this change.test_cache_shared_prefix_not_charged_per_entry— 100 entries under a 10 KB shared prefix stay well under the ~1 MB a per-entry prefix charge would produce.cargo test -p lance-core cache::), incl.test_cache_bytes.cargo fmt --allandcargo clippy --all --tests --benches -- -D warningsclean.