Skip to content

perf(core): skip the entry deep-size traversal for size-blind backends - #9522

Merged
Xuanwo merged 5 commits into
lance-format:mainfrom
LuciferYang:fix/no-cache-deep-size
Nov 3, 2026
Merged

Xuanwo merged 5 commits into
lance-format:mainfrom
LuciferYang:fix/no-cache-deep-size

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

CacheState::entry_size computed a DeepSizeOf traversal for every value on its way into the cache, including when the backend was configured with zero capacity and would discard the entry without accounting it.

CacheBackend gains a defaulted uses_entry_sizes(), which MokaCacheBackend and QuickCacheBackend override as capacity > 0, and entry_size returns 0 early when it is false. The gate sits inside entry_size, so all four of its call sites are covered at once. Both overrides can only be false at zero capacity, where neither backend retains anything: moka treats a zero max capacity as a disabled map, and quick's shards reject any entry once the weight target is zero. The declared size therefore cannot change an admission or eviction decision for them, and the size reporting built on retained entries stays correct because the retained set is empty.

Adding a defaulted trait method leaves the six other in-tree implementors on true, which is right for each of them: the ones that retain entries need the sizes, and SerializingCacheBackend reports size_bytes() by summing them.

Fixes #9521.

How was this patch tested

backends_that_ignore_entry_sizes_skip_the_traversal in rust/lance-core/src/cache/mod.rs loads one value through four caches and asserts the traversal count for each: zero for no_cache() and for a zero-capacity quick backend, one for with_capacity(1024) and for a quick backend with capacity. The counter lives inside the value rather than in a static, so a later test reusing the type cannot disturb the count. Removing either override makes the matching phase fail.

The quick backend cases are the ones that matter in production: Session's index and metadata caches run on QuickCacheBackend, and Session::new(0, 0, registry) or CacheSpec::Size(0) is what configures it with zero capacity.

Out of scope, noticed while checking the zero-capacity paths: moka short-circuits get_or_insert at zero capacity to avoid its single-flight waiters, while quick still goes through get_value_or_guard_async and builds a placeholder. The two backends therefore differ in how they treat concurrent loads when nothing can be cached. That is existing behaviour and untouched here.

LanceCache::get_or_insert and the insert paths computed the loaded
value's deep size on every load, but a zero-capacity (no_cache)
MokaCacheBackend drops the entry and its size without accounting, so
the traversal fed a number nothing consumed. Every value loaded
through a disabled cache paid a full DeepSizeOf walk (fresh Context
per call, O(size) for nested values) on hot uncached paths such as
benchmarks and memory-constrained scans.

Add CacheBackend::uses_entry_sizes (default true) and have
CacheState::entry_size return 0 without traversing when the backend
reports it does not consume sizes. MokaCacheBackend reports its
capacity; the quick backend still accounts and keeps the default.
… test counter

The override was only on the moka backend, but Session's index and metadata
caches run on QuickCacheBackend, which is the one a caller can configure with
zero capacity. Move the traversal counter into the value so a second test
cannot disturb the count, and say in the trait doc that returning false also
gives up per-entry size reporting.
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 23, 2026
# Conflicts:
#	rust/lance-core/src/cache/backend.rs
#	rust/lance-core/src/cache/quick.rs
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 27, 2026

@lance-gatekeeper lance-gatekeeper 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.

✅ Gate recommendation: approve.

The shared guard removes wasted deep-size traversal for zero-capacity Moka and Quick caches. Quick’s priority tier also rejects entries at zero capacity, while retaining caches continue to calculate sizes for admission, eviction, and deep-size reporting.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 27, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed the final merge head and the main integration. The zero-capacity gate remains limited to non-retaining Moka and Quick caches; custom backends keep the size-aware default, and positive-capacity caches retain their existing admission, eviction, and size-reporting behavior.

The focused traversal regression and the complete cache test suite pass (1/1 and 100/100), as do formatting, the module-size delta check, root ci-profile Clippy, and all applicable hosted CI. No blocking findings.

@Xuanwo
Xuanwo merged commit a7fcb5e into lance-format:main Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: a zero-capacity cache still pays a deep-size traversal on every load

2 participants