Subtractive report · NVIDIA/hyperflare-inference #496

Raw Markdown for agents: https://autobot.eidonix.com/p/68b54199-fd72-4608-a483-39965be9a81d.md

Key findings

The change is large (+3358/−933 in 3 files) but mostly load-bearing: the shared segment/window state machine, the reference counts that keep SlabPool slots pinned until every follower's GPU upload finishes, the private-vs-shared failure isolation, and the TransportEvent::InboundWindowSample plumbing to DiskStore::inbound_rate are each required by a stated goal in the description and cannot be dropped without losing the measured 3.5–4.2x physical read reduction.

Nothing is left dead behind it: the old Destination, InPhase, InboundRecord::apply_transfer, InboundRecord::submit_upload and the device branch of the old inbound record are all deleted inside the same diff, and the only pre-existing helper whose call sites are rewritten, plan_windows, has both call sites and both plan-shape tests updated.

What can still go is roughly 215–230 lines (~6% of the change, ~9% of the non-test production lines), in 8 places. Ranked by lines saved:

# Where Lines Kind
1 Demand-index scheduling ~80 Should fix
2 Duplicate close-drain test ~70 Should fix
3 HeaderIdentity + manual Hash ~50 added, net ~22 Should fix
4 Pass 1 / Pass 5b double traversal ~12 Nit
5 poll_all_fetches vs poll_until ~10–16 Nit
6 HostInbound one-element Vec<Segment> ~8 Nit
7 Duplicated docs paragraph ~5 Nit
8 Dead term in Staged::close max ~2–4 Nit

Of disk_store.rs's +3313, roughly +900 is the new test module (six coalescing tests at :8234-9357 plus the GatedCopier / write_shared_ancestor_tree / poll_all_fetches fixtures), so the production change is about +2400/−930.


Findings, largest saving first

1. Should fix — the per-pump demand table and demand-aware picker (~80 lines) only reorder which new window starts first

selene/src/disk_store.rs:1732-1755 (build_pending_demand), :1757-1793 (pick_next_pending_plan), the demand: Option<&mut HashMap<WindowKey, usize>> argument on attach_pending (:1711-1730), and the two decrement sites at :1939 and :1991-1995.

These exist for one purpose: when several restores are in flight, prefer to start the new window that a sibling restore also needs, so concurrent branches stay synchronized on a shared ancestor. Coalescing does not depend on it. attach_pending → find_best_existing_window_for (:1665-1681) already scans every unstarted plan for a reusable in-flight window regardless of order, and plan_windows(.., true) guarantees aligned (first, count) equality so a later leader matches an earlier one's window anyway.

The machinery is provably idle for the common case: build_pending_demand returns an empty map when device_inbound.len() <= 1 (:1736) — every single-restore caller — and pick_next_pending_plan then degenerates to "first unstarted plan that fits".

Smaller version: delete build_pending_demand, the demand parameter, and both decrement sites; pick_next_pending_plan becomes just the free-slot filter over record.plans:

fn pick_next_pending_plan(&self, id: JobId) -> Option<usize> {
    let record = self.device_inbound.get(&id)?;
    let free = self.resources.free_slots();
    let row_slots = lock(&self.resources.transfers).layout().row_slots();
    record.plans.iter().enumerate().position(|(i, plan)| {
        !record.started[i] && plan.count + if plan.row { row_slots } else { 0 } <= free
    })
}

~65–80 lines removed; attach_pending loses its second argument and both call sites (:1917, :1933) get shorter. The only thing lost is start-order relaxation across concurrent restores. That shows up in the staggered-arrival row of the PR table (3.76x read reduction vs 4.22x simultaneous), so if the staggered number regresses, the preference can be kept as a single peer_demand > 0 boolean lookup in window_index rather than a per-pump HashMap of every pending key.

2. Should fix — the close-drain test duplicates the burst test (~70 lines)

selene/src/disk_store.rs:8831-8906 (staged_close_drains_active_coalesced_fetches_cleanly) and :8294-8384 (concurrent_burst_coalesces_shared_ancestor_opens_and_window_reads) build the same fixture (write_shared_ancestor_tree, scanned records, intake-sized SlabPool, SimulatedGpu::shared_mapped, DiskStore::new, per-branch InboundTicket, poll_all_fetches), assert the same counters (disk_opens() == 1 + BRANCHES, coalesced_windows(), pool free, no pins) and run the same per-page and row equality loops. They differ only in BRANCHES/PARENT_PAGES constants and in when store.close() runs.

Smaller version: one test with for close_early in [false, true], BRANCHES = 4, PARENT_PAGES = 3 * WINDOW_PAGES, and the in_flight().1 == BRANCHES / store.close() / stats().fetched lines inside if close_early. That shape already gives the exact counts the close test asserts (disk_opens() == 1 + BRANCHES, coalesced_windows() == 3 * (BRANCHES - 1)), and the burst test's 4-aligned-parent-window shape remains covered by different_skip_pages_coalesce_aligned_windows_to_exact_gpu_pages (:8510, 3*WINDOW_PAGES+7). ~70 lines, no behaviour lost.

3. Should fix — HeaderIdentity re-implements Header's identity rules (50 lines added, ~22 net)

selene/src/disk_store.rs:1001-1050 adds HeaderIdentity(Arc<Header>) whose only purpose is to be a Hash key inside SegmentKey (:1052-1056) and WindowKey (:1058-1064). It re-states Header's own PartialEq (selene/src/shared/record.rs:136-152) as a 47-line hand-written Hash that destructures Header with .. and hashes ranks.len() then each RankSizes, skipping version. That is a second place to edit whenever a Header field is added — and the .. pattern means the compiler will not flag a miss, so WindowKey hashing can silently disagree with the expected comparison it is paired with. The Arc::ptr_eq fast path buys nothing: identical Arcs are equal by value.

Smaller version: put the Hash impl once, next to the PartialEq it must agree with, and drop the wrapper entirely:

// selene/src/shared/record.rs, after `impl Eq for Header {}` (line 154)
impl std::hash::Hash for Header { /* same field list, ~28 lines */ }
// selene/src/disk_store.rs
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct SegmentKey { path: Arc<Path>, expected: Arc<Header> }

plus HeaderIdentity(Arc::new(..)) at :2444 → Arc::new(..) and (*key.segment.expected.0).clone() at :1964 → (*key.segment.expected).clone(). 50 lines gone, ~28 in, net ~22 and one fewer definition of "which Header fields are identity".

4. Nit — pump_device_inbound walks every record's active-window list twice (~12 lines)

selene/src/disk_store.rs:1897-1909 (Pass 1) removes entries with upload_done; :2144-2196 (Pass 5b) walks the same list again with if active.upload_submitted { continue; } and submits an upload for each ready window. Nothing between them reads record.done/record.bytes, and the only shared input, has_window_credit → active.len(), is changed by reaping only downwards, which merely lets Pass 4 start fewer windows for one pump and is picked up on the next step. Folding the reap into the head of a single "reap-and-upload" loop at the Pass 5b site deletes one full traversal, its active_len copy and the self.device_inbound[&id].active[index] re-lookup per upload: ~12 lines. This is the lowest-confidence item here; merge only if the pump interleaving is confirmed acceptable (the tests that count polls loop, so an extra pump is tolerated).

5. Nit — poll_all_fetches duplicates crate::testing::poll_until (~10–16 lines)

selene/src/disk_store.rs:8215-8232 and selene/src/testing.rs:25-37 are the same helper: poll into a Vec<StoreEvent> in a loop, assert the deadline, return the events. The file already imports use crate::testing::poll_until; (:2829) and uses it throughout. The one difference is the stop predicate — poll_until takes Fn(&StoreEvent) -> bool (not FnMut), so counting to expected needs a Cell in the closure. Six call sites (:8347, :8476, :8583, :8687, :8803, :8961) would each gain that closure; hence the saving is modest, ~10–16 lines, and keeping a thin local wrapper is defensible. Verified signature; the finding is real but small.

6. Nit — Owner::HostInbound(JobId, usize) and HostInbound::segments carry a segment list that always has one element (~8 lines)

selene/src/disk_store.rs:265-267 and :749-750. The only constructor is read_into at :1351/:1359-1360, which builds Owner::HostInbound(id, 0) and HostInbound::new(vec![segment], ...), so the index is always 0. Making it Owner::HostInbound(JobId) with segment: Segment removes the vec![], the segments.get_mut(segment) at :789, and the .iter()/[plan.segment] indirections at :865, :882, :890, :924, :974, :987 — ~8 lines, with Segment::settled unchanged. (The segments: Vec<Segment> inside InboundRecord for the device path is genuinely multi-element and must stay.)

7. Nit — the docs state the physical-vs-logical counter split twice (~5 lines)

docs/prefix-cache-offload.md:362-366 (in the module-level §2.4 description) and :1528-1531 (in the §4 measurement table's notes) say the same thing: StoreStats.bytes_in counts logical restored bytes while inbound_rate comes from physical staging-window reads. The §4 note belongs where the measurement table is; the §2.4 sentence duplicates both it and the module doc at selene/src/disk_store.rs:9-27, which already covers the shared handle, the slots pinned until every upload, close-before-Fetched, and the extra partial window. Drop the §2.4 sentences: ~5 lines.

8. Nit — one provably dead term in the Staged::close max (~2–4 lines)

selene/src/disk_store.rs:2522-2528 computes internal as owners.len().max(maintenance.len()).max(shared_segments.len()).max(shared_windows.len()). SharedWindow owns no disk job id at all, so .max(self.shared_windows.len()) can never raise the count; every maintenance id is already inserted into owners (:2468, :2485) before entering maintenance; and a SharedSegment id is in owners (:1968) as soon as its Open is submitted, so the segment term only matters while a segment is still Unopened. Safe shorter form: self.owners.len().max(self.maintenance.len()).max(self.shared_segments.len()) — the shared_windows term goes outright (~2 lines), and if the Unopened case is intentionally covered by the store-level store_jobs count, the maintenance term can go too (~4 lines).


Checked and found sound (do not re-raise)

Open questions

Total removable: ~215–230 lines across 8 places, dominated by findings 1–3 (about 200 of it).