Skip to content

Release placeholder keys when unsync loaders fail - #132

Merged
arthurprs merged 5 commits into
masterfrom
research/loader-cleanup
Nov 22, 2026
Merged

arthurprs merged 5 commits into
masterfrom
research/loader-cleanup

Conversation

@arthurprs

@arthurprs arthurprs commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

An unsync convenience loader creates a placeholder that owns the key before calling the loader. If the loader returns an error or panics, the placeholder can retain its key until a retry, removal, clear, or cache drop. The placeholder counts toward neither len() nor resident capacity.

Both convenience loaders now hold the placeholder in the existing Guard while calling the loader. Dropping the guard removes the unfilled placeholder on error or unwind. The regression tests check that both loaders release the owned key and can retry successfully. An immediate retry creates a fresh placeholder and records a miss when statistics are enabled.

On a resident hit, get_or_insert_with returns the reference from the resident lookup and skips the follow-up slab access by token. After inserting on a miss, it checks the slot to see whether the value was admitted. The mutable loader looks up the slot to construct its weight-tracking RefMut.

RefMut caches a raw pointer to the resident value so dereferencing the guard can skip the slab lookup. The exclusive shard borrow keeps the resident in place while the guard is live. Drop uses the slot token to recompute weight. The guard stores one additional pointer.

In five local interleaved runs on Rust 1.97.1, 9e173dd lowered median latency for repeated get_or_insert_with hits on one resident u64 key by 10.4–10.9% (0.63–0.66 ns/op) versus the token-lookup revision (e334d8d). Each pair timed 8,192 hits per revision on an AMD Ryzen AI Max+ Pro 395, with the order reversed between pairs.

Deref borrows the resident value through the cached pointer. The weight guard uses the slot token to recompute weight on drop.

Exercise shared and mutable reborrows on a guard moved by value and check weight accounting when a live guard drops during unwind.
Keep the cached value pointer with the PlaceholderId bound. Wrap each loader placeholder in the current Guard representation so drop releases the key on failure.
@arthurprs
arthurprs marked this pull request as ready for review November 22, 2026 12:06
@arthurprs
arthurprs merged commit df135cf into master Nov 22, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant