perf(cas): LRU eviction instead of arbitrary HashMap iter order#167
Merged
Conversation
When the reconstruction cache overflows and only full plans remain, the previous code evicted via cache.keys().next() — HashMap iteration order, which can drop a hot recently-fetched plan and keep a cold one. Add last_accessed: Instant to CacheEntry, bumped on every cache hit. Eviction picks min(last_accessed). inserted_at stays for TTL (presigned URL expiry, which is relative to issue time, not access time). Adds a test (cache_evicts_lru_full_plan_on_overflow) that fills the cache, bumps entry 0, overflows, and asserts entry 0 survives while entry 1 (true LRU) is evicted.
Contributor
POSIX Compliance (pjdfstest) |
Contributor
Benchmark Results |
XciD
marked this pull request as ready for review
May 20, 2026 19:05
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
When the reconstruction cache hits
MAX_CACHE_ENTRIESand all remaining entries are full plans (range entries already purged in pass 1), the previous code picked the eviction victim via:```rust
cache.keys().next().copied()
```
HashMap::keys()returns entries in hash-bucket order, which is unrelated to access patterns. A plan we just fetched and are actively reading from can be the first one returned, while an entry that's been cold for an hour survives.This PR adds a
last_accessed: Instantfield toCacheEntry, bumped on every cache hit. The overflow path picks the entry with the oldestlast_accessed. Hot plans stay warm even when they were inserted long ago.Why not reuse
inserted_at?inserted_atis the time the CAS response arrived. Its only consumer is the TTL check — presigned URLs insidetermsexpire ~1h after the server issued them, not after we last touched the entry. Bumpinginserted_aton hit would let stale URLs slip past the TTL.Tests
New
cache_evicts_lru_full_plan_on_overflow:last_accessed.All 328 lib + 338 nfs tests pass, clippy clean.