Repository navigation
perf(core): skip the entry deep-size traversal for size-blind backends - #9522
Conversation
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.
# Conflicts: # rust/lance-core/src/cache/backend.rs # rust/lance-core/src/cache/quick.rs
There was a problem hiding this comment.
✅ 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.
Xuanwo
left a comment
There was a problem hiding this comment.
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.
CacheState::entry_sizecomputed aDeepSizeOftraversal 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.CacheBackendgains a defaulteduses_entry_sizes(), whichMokaCacheBackendandQuickCacheBackendoverride ascapacity > 0, andentry_sizereturns 0 early when it is false. The gate sits insideentry_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, andSerializingCacheBackendreportssize_bytes()by summing them.Fixes #9521.
How was this patch tested
backends_that_ignore_entry_sizes_skip_the_traversalinrust/lance-core/src/cache/mod.rsloads one value through four caches and asserts the traversal count for each: zero forno_cache()and for a zero-capacity quick backend, one forwith_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 onQuickCacheBackend, andSession::new(0, 0, registry)orCacheSpec::Size(0)is what configures it with zero capacity.Out of scope, noticed while checking the zero-capacity paths: moka short-circuits
get_or_insertat zero capacity to avoid its single-flight waiters, while quick still goes throughget_value_or_guard_asyncand 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.