Skip to content

fix(cache): count key footprint in MokaCacheBackend eviction weight - #7573

Merged
wjones127 merged 3 commits into
lance-format:mainfrom
Ali2Arslan:fix/moka-cache-key-footprint
Jul 2, 2026
Merged

wjones127 merged 3 commits into
lance-format:mainfrom
Ali2Arslan:fix/moka-cache-key-footprint

Conversation

@Ali2Arslan

@Ali2Arslan Ali2Arslan commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

MokaCacheBackend's weigher charged only the value's size_bytes and ignored the key entirely:

.weigher(|_, v: &MokaCacheEntry| v.size_bytes.try_into().unwrap_or(u32::MAX))

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: two Arc<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 unique key string.
  • Used in both the weigher (eviction) and the synchronous approx_size_bytes (DeepSizeOf reporting) so the two stay consistent.

The prefix is deliberately excluded. build_key clones the prefix Arc<str> (shared) and allocates a fresh key per entry (unique). The prefix is one shared allocation per cache handle and is not reclaimed when a single entry is evicted, so charging prefix.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 CacheBackend impls are unaffected.

Test plan

  • test_cache_weighs_key_footprint — a 10 KB key contributes to size_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.
  • Existing cache tests still pass (cargo test -p lance-core cache::), incl. test_cache_bytes.
  • cargo fmt --all and cargo clippy --all --tests --benches -- -D warnings clean.

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]>
@github-actions github-actions Bot added the bug Something isn't working label Jul 2, 2026
Ali2Arslan and others added 2 commits July 1, 2026 21:18
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

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@wjones127 wjones127 self-assigned this Jul 2, 2026
@wjones127
wjones127 self-requested a review July 2, 2026 15:52

@wjones127 wjones127 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.

This is a clean fix. Thank you!

@wjones127
wjones127 merged commit 413ee8a into lance-format:main Jul 2, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants