Skip to content

perf(cache): allocate the fill outboard buffer lazily, off the registry lock (#1790 item 1) - #1884

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/1790-item1-outboard-alloc-off-lock
Aug 31, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/1790-item1-outboard-alloc-off-lock

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Item 1 of #1790 (Group C serve-path hot-path audit), landed as its own PR — same per-item cadence as the merged item 7 (#1879), item 5 (#1882), and open item 3 (#1883).

Problem

HashOutboard::new zeroed the whole tree.outboard_size() buffer (~2.7 MB for a 700 MB blob) at construction, and make_session builds that outboard under the global FillRegistry map lock. So a single large-blob miss's claim held the process-wide registry mutex across an MB-scale vec![0u8; …], stalling every other miss's claim, parked-reader wakeup, and teardown for the allocation's duration. Concurrent large-blob misses convoyed on the registry — miss-path concurrency stopped scaling with blob size, which is backwards.

Fix

Defer the buffer allocation to the first capture:

  • HashOutboard::new is now O(1) — empty vecs, no zeroing.
  • write_pair sizes bytes/captured on the first real interior node it stores, under the per-hash state lock — never the global registry lock. A real interior node implies outboard_size() >= 64, so an empty buffer is an unambiguous "not yet sized" sentinel.
  • try_load already reads an empty buffer as "absent" via .get(), so the read side is unchanged.

The MB-scale zeroing now lands at first-capture time under a per-hash lock (where an admit already does far heavier store I/O), off the global mutex that every other hash's claim contends. This also drops the wasted allocation a second session for a hash used to make and immediately discard when it adopts the canonical outboard — a lazy buffer never allocates before the adopt.

Why not the issue's literal "allocate before locking"

claim can't know Own/Mix/Attach until it holds the lock (the single-lock decision closes a coalescing TOCTOU), so eager pre-lock allocation would waste a 2.7 MB buffer on the common attach case. Lazy allocation reaches the same goal — "registry critical section becomes O(1); no MB-scale zeroing under the global mutex; no wasted allocations on attach" — without that tension, and in one self-contained file.

Tests

  • outboard_buffer_is_allocated_lazily_on_first_capture (new) — a freshly built session allocates no buffer; the first capture sizes it to the full outboard.
  • Existing capture/load round-trips, siblings_share_one_per_hash_outboard, and the park-then-resume families all stay green.
  • Full cache suite (402) + 88 node serve/fill tests green; clippy/doc/fmt/pre-commit clean.

Remaining #1790 items

Items 2, 4, 8, 9 remain (7 merged; 5 in #1882 merged; 3 in #1883; 6 closed as already-considered — collides with a documented deliberate decision in retry.rs, see the issue comment).

🤖 Generated with Claude Code

…ry lock (#1790 item 1)

`HashOutboard::new` zeroed the whole `tree.outboard_size()` buffer (~2.7 MB for a
700 MB blob) at construction — and `make_session` builds that outboard under the
global `FillRegistry` map lock. So one large-blob miss's claim held the process-
wide registry mutex across an MB-scale `vec![0u8; …]`, stalling every other miss's
claim, parked-reader wakeup, and teardown for the allocation's duration; concurrent
large-blob misses convoyed on the registry, so miss-path concurrency stopped
scaling with blob size — backwards.

Defer the buffer allocation to the first capture. `HashOutboard::new` is now O(1)
(empty vecs); `write_pair` sizes `bytes`/`captured` on the first real interior node
it stores, under the per-hash `state` lock — never the global registry lock. A real
interior node implies `outboard_size() >= 64`, so an empty buffer is an unambiguous
"not yet sized" sentinel. `try_load` already reads an empty buffer as "absent" via
`.get()`, so the read side is unchanged. This also drops the wasted allocation a
second session for a hash used to make and immediately discard when it adopts the
canonical outboard — a lazy buffer never allocates before the adopt.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 31, 2026 15:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a miss/fill hot-path contention issue in the cache by making HashOutboard construction O(1) and deferring the large outboard buffer allocation/zeroing until the first actual capture, so the global FillRegistry lock is no longer held across MB-scale allocations.

Changes:

  • Make HashOutboard::new allocate empty vectors instead of zeroing the full outboard buffer immediately.
  • Lazily allocate and size the outboard bytes/captured buffers on the first interior-node capture inside HashOutboard::write_pair under the per-hash lock.
  • Add a regression test asserting that outboard buffers start empty and are allocated on first capture.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/cache/src/fill_session.rs Resolved
…esentable

Copilot review: the lazy `write_pair` sizing fell back to `usize::MAX` when
`tree.outboard_size()` did not fit `usize`, which would attempt `vec![0u8;
usize::MAX]` and abort the process. Degrade the same way the adjacent `idx` guard
does — return `false` (not capturable) — rather than allocate the maximum.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.32% (was 82.31%, ● 0.00%) — 69502/84431 lines

File Coverage Lines Δ
crates/cache/src/fill_session.rs 93.82% 866/923 ▲ +0.05%
History
Commit Total Δ vs base
221f9e4 (HEAD) 82.32% ● 0.00%

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.

2 participants