Key findings
The diff is honest and mostly lean: +196/−33 across three files, no new flags, knobs, config defaults, env vars, per-model branches or speculative generality, and every new symbol (sentinel_saved_bytes, Layout::sentinel_granules, both new CPU tests, both doc hunks) is actually used. I found no dead code and no AGENTS.md structural violation. Three concrete cuts are available, listed below in value order. I did not spend findings on pure correctness (that is the correctness lens's job); where a workaround could simply be deleted, I say so.
row_backing_bytesre-implementsbytesline for line (helios/src/runtime_core/memory.rs:383-414vs:357-381). Same capacity filter, sameunits > s.maximumerror, samechecked_mul + (align-1) / align * alignpadding, same"cache allocation size overflow"message; only the final- savedis new. 32 new lines collapse to ~10 and keep all five call sites.- One null-row physical decomposition is spelled four times —
handle_charge(cache_memory.rs:81-102, four identicalinvalid("mapping handle count overflow")closures), the mapping block (:1416-1447), and the zeroing block (:1467-1488). A single derivedcount/granule/tailtriple reproduces today's handle counts exactly and deletes bothelsebranches at:1438-1447and:1483-1488, which are determinable code rather than real cases. - The GPU-test edit adds a branch inside the pattern closure (
cache_memory.rs:1212-1225): −11/+2 lines by assigning row 0 after the untouched closure. - Lower value:
sentinel_granulesis a derived field (:63,:259-262,:487), andsentinel_*is a second name for the object the docs already call the "null row" (docs/runtime-core-design.md:772-778).
1. (High) row_backing_bytes is a verbatim copy of bytes — collapse to a subtraction
helios/src/runtime_core/memory.rs:383-414, verbatim (the whole new function):
/// Physical bytes needed to back `units` continuation rows after aliasing null row zero's full
/// granules onto a single shared granule.
fn row_backing_bytes(core: &Core, units: u64) -> Result<u64> {
core.allocations
.iter()
.filter_map(|a| {
a.sizing
.as_ref()
.filter(|s| s.capacity == 0)
.map(|s| (a, s))
})
.try_fold(0u64, |total, (a, s)| {
if units > s.maximum {
return Err(invalid("cache capacity exceeds its virtual extent"));
}
let align = a.alignment as u64;
let span = units
.checked_mul(s.unit_bytes)
.and_then(|n| n.checked_add(align - 1))
.ok_or_else(|| invalid("cache allocation size overflow"))?
/ align
* align;
let saved = if units > 0 {
super::cache_memory::sentinel_saved_bytes(s.unit_bytes, align)
} else {
0
};
total
.checked_add(span - saved)
.ok_or_else(|| invalid("cache allocation size overflow"))
})
}
The function it duplicates, helios/src/runtime_core/memory.rs:357-381, differs only in the .filter(|s| s.capacity == capacity) predicate and the absence of saved:
let size = units
.checked_mul(s.unit_bytes)
.and_then(|n| n.checked_add(a.alignment as u64 - 1))
.ok_or_else(|| invalid("cache allocation size overflow"))?
/ a.alignment as u64
* a.alignment as u64;
total
.checked_add(size)
.ok_or_else(|| invalid("cache allocation size overflow"))
Replace the entire body with:
fn row_backing_bytes(core: &Core, units: u64) -> Result<u64> {
if units == 0 {
return Ok(0);
}
let saved: u64 = core
.allocations
.iter()
.filter_map(|a| a.sizing.as_ref().filter(|s| s.capacity == 0).map(|s| (a, s)))
.map(|(a, s)| super::cache_memory::sentinel_saved_bytes(s.unit_bytes, a.alignment as u64))
.sum();
bytes(core, 0, units).map(|span| span - saved)
}
Equivalence, checked by hand: with row = q*A + r (0 ≤ r < A) and units ≥ 1, span = ceil(units*row/A)*A and saved = (q-1)*A; the physical backing allocate_shared_cache actually creates is A + (span - q*A) = span - saved, so the planner formula and the allocator agree under one expression. span - saved cannot underflow for units ≥ 1 because ceil(units*row/A) ≥ q + 1 when q ≥ 1, and saved = 0 when q ≤ 1. units == 0 short-circuits, matching bytes(...) == 0. Worked example for the fixture at memory.rs:2032-2041 (row = 82_143_488, A = 2 MiB, units = 2): span = 158 MiB, saved = 38*A = 76 MiB, result 82 MiB — the assertion at :2041 still holds. All five call sites (memory.rs:486, :510, :523, :1003, :1249) are unchanged, as are the two test call sites at :2005, :2020.
Bonus: after this, bytes(core, 0, ...) (virtual span, memory.rs:576-579) sitting next to row_backing_bytes (physical) reads as the deliberate pairing it is, instead of two near-identical bodies a reader must diff by eye.
2. (High) The null-row slice decomposition is written four times — describe it once
helios/src/runtime_core/cache_memory.rs:81-102, handle_charge:
fn handle_charge(&self, row_readers: u64, plane_readers: &[u64]) -> Result<u64> {
let fixed_charge = if self.sentinel_granules > 1 {
let sentinel_bytes = self.sentinel_granules * self.alignment;
let sentinel = 1u64
.checked_add(
self.sentinel_granules
.checked_mul(row_readers)
.ok_or_else(|| invalid("mapping handle count overflow"))?,
)
.ok_or_else(|| invalid("mapping handle count overflow"))?;
if self.fixed_bytes > sentinel_bytes {
sentinel
.checked_add(1)
.and_then(|n| n.checked_add(row_readers))
.ok_or_else(|| invalid("mapping handle count overflow"))?
} else {
sentinel
}
} else {
1u64.checked_add(row_readers)
.ok_or_else(|| invalid("mapping handle count overflow"))?
};
cache_memory.rs:1416-1447, the mapping block:
let sentinel_bytes = layout.sentinel_granules * layout.alignment;
if layout.sentinel_granules > 1 {
let mappings: Vec<_> = (0..layout.sentinel_granules)
.map(|i| {
(
geometry.row.name.as_str(),
geometry.row.address + i * layout.alignment,
layout.alignment,
)
})
.collect();
self.map_cache_chunk(layout.alignment, &mappings)?;
if layout.fixed_bytes > sentinel_bytes {
self.map_cache_chunk(
layout.fixed_bytes - sentinel_bytes,
&[(
geometry.row.name.as_str(),
geometry.row.address + sentinel_bytes,
layout.fixed_bytes - sentinel_bytes,
)],
)?;
}
} else {
self.map_cache_chunk(
layout.fixed_bytes,
&[(
geometry.row.name.as_str(),
geometry.row.address,
layout.fixed_bytes,
)],
)?;
}
cache_memory.rs:1467-1488, the zeroing block, repeats exactly that shape:
// Zero each physical byte once, before any graph or transfer can read an alias.
if layout.sentinel_granules > 1 {
self.driver.check(
"cuMemsetD8_v2",
(self.driver.zero)(geometry.row.address, 0, layout.alignment as usize),
)?;
if layout.fixed_bytes > sentinel_bytes {
self.driver.check(
"cuMemsetD8_v2",
(self.driver.zero)(
geometry.row.address + sentinel_bytes,
0,
(layout.fixed_bytes - sentinel_bytes) as usize,
),
)?;
}
} else {
self.driver.check(
"cuMemsetD8_v2",
(self.driver.zero)(geometry.row.address, 0, layout.fixed_bytes as usize),
)?;
}
Introduce one derived description, near cache_memory.rs:1416 (before the mapping block, reused by the zeroing block ~20 lines later in the same function, with no intervening early return other than the map_cache_chunk calls):
let count = layout.sentinel_granules.max(1);
let granule = if count > 1 { layout.alignment } else { layout.fixed_bytes };
let tail = layout.fixed_bytes - granule * count;
Then the mapping block becomes:
let mappings: Vec<_> = (0..count)
.map(|i| (geometry.row.name.as_str(), geometry.row.address + i * granule, granule))
.collect();
self.map_cache_chunk(granule, &mappings)?;
if tail > 0 {
self.map_cache_chunk(
tail,
&[(geometry.row.name.as_str(), geometry.row.address + granule * count, tail)],
)?;
}
and the zeroing block becomes:
self.driver.check(
"cuMemsetD8_v2",
(self.driver.zero)(geometry.row.address, 0, granule as usize),
)?;
if tail > 0 {
self.driver.check(
"cuMemsetD8_v2",
(self.driver.zero)(geometry.row.address + granule * count, 0, tail as usize),
)?;
}
and handle_charge's fixed_charge collapses to the same quantities (computed from self inside handle_charge as let count = self.sentinel_granules.max(1); let granule = if count > 1 { self.alignment } else { self.fixed_bytes }; let tail = self.fixed_bytes - granule * count;):
let fixed_charge = 1
.checked_add(row_readers.checked_mul(count).ok_or_else(|| invalid("mapping handle count overflow"))?)
.and_then(|n| if tail > 0 { n.checked_add(1)?.checked_add(row_readers) } else { Some(n) })
.ok_or_else(|| invalid("mapping handle count overflow"))?;
Equivalence: sentinel_granules ≤ 1 → count = 1, granule = fixed_bytes, tail = 0, i.e. one handle 1 + row_readers and one cuMemMap covering the whole fixed span — exactly today's else paths. sentinel_granules = n ≥ 2 → n mappings of alignment from one handle (1 + n*row_readers), plus, when fixed_bytes > n*A (tail > 0), 1 + row_readers — exactly today's if path. Both the handle formula and the cache_memory.rs:1366-1385 assertion ((1 + 39) + 2 + base_chunks) still hold, and the DCP16 fixture at :818-834 (518,504) is untouched. This is a maintainability proposal, not a correctness claim: today's > 1 spelling and a count-based one agree because n == 1 is already normalized to 0 at cache_memory.rs:259-262.
Net deletion: roughly 70 lines of branching replaced by 3 derived locals plus ~12 lines of straight-line code in two of the three sites. Note this also removes the duplicated sentinel_bytes binding, which is currently computed at :1416 and only used two blocks later.
3. (Medium) The GPU-test edit puts a branch inside the pattern closure
helios/src/runtime_core/cache_memory.rs:1212-1225:
let expected: Vec<Vec<u8>> = (0..7)
.map(|row| {
if row == 0 {
vec![0u8; geometry.row.unit_bytes as usize]
} else {
(0..geometry.row.unit_bytes as usize)
.map(|i| {
((i ^ (i >> 8) ^ (i >> 16) ^ (i >> 24)) as u8)
.wrapping_add(row * 31)
})
.collect()
}
})
.collect();
Replace with the original closure untouched plus one assignment:
let mut expected: Vec<Vec<u8>> = (0..7)
.map(|row| {
(0..geometry.row.unit_bytes as usize)
.map(|i| {
((i ^ (i >> 8) ^ (i >> 16) ^ (i >> 24)) as u8)
.wrapping_add(row * 31)
})
.collect()
})
.collect();
expected[0] = vec![0u8; geometry.row.unit_bytes as usize]; // null row stays zero
−11/+2 lines, the .skip(1) upload at :1226 and the single contents == expected[row] comparison are unchanged, and the row-zero intent moves into the existing comment at :1210-1211 rather than hiding in a nested if. Collapsing this also keeps the test diff honest about what it changes: only row 0's expectation, not the pattern generator.
4. (Low) Notes worth a line each, not blocking
-
Derived field.
cache_memory.rs:63sentinel_granules: u64is fully derivable at its only allocation site —cache_memory.rs:156setsrow_bytes = geometry.row.unit_bytes, sorow_bytes / row_alignmentis available right there, andpack_prefixesmerely copies it (:487). Removing it means threading the value intohandle_charge, which is also called from compact paths without aGeometryChandyin scope; that trade makes deletion a wash, so treat as a nit. If it stays,cache_memory.rs:259-262deserves a one-line shape:let sentinel_granules = match row_bytes / row_alignment { n if n > 1 => n, _ => 0, };is fine as-is; the only real gain would be a comment saying why one granule is not aliased (it shares a granule with row one's start, per
docs/runtime-core-design.md:774-777). -
Naming drift.
sentinel_granules,sentinel_saved_bytes(cache_memory.rs:69),sentinel_row_aliases_full_granules_onto_one_page(:1367) andsentinel_row_savings_increase_kv_page_capacity(memory.rs:2031) introduce "sentinel" as a second name for an object the spec already calls the null row (docs/runtime-core-design.md:772-777: "one null row", "null row zero").null_row_*would match existing prose; cheap to change now, confusing later. -
No AGENTS.md violation found. No flag/env/config default, no per-model or per-recipe branch, no new trait or fallback layer, no format/Protobuf/CLI/metric file touched (diff is exactly
docs/runtime-core-design.md,helios/src/runtime_core/cache_memory.rs,helios/src/runtime_core/memory.rs), and thesentinel_granules ≤ 1behaviour is a real invariant (docs/runtime-core-design.md:774) not a speculative fallback.saturating_sub(1)atcache_memory.rs:70is reachable (sub-granule rows exist in tests at:1372), so it is not dead defensive code.
What is already lean, and should not be "simplified" further
- Mapping and zeroing both go through the existing
map_cache_chunk(bytes, &[(name, addr, len)])(cache_memory.rs:1500-1534), which already supported one handle at many addresses — the PR reuses the mechanism instead of adding one. Good. - Both new tests pin the formula at its two real boundaries (
cache_memory.rs:1370-1372: 38 granules saved, 0 at exactly one granule, 0 below one granule) and the planner delta as one assertion pair (memory.rs:2040-2041), rather than a third copy of the arithmetic. Nothing to delete. - The doc hunk (
docs/runtime-core-design.md:772-780) is 8 lines that state the condition, the mechanism and the exact saving formula — no restatement of the code.
Open questions for the author
- Is
row_backing_bytesintended to stay a parallel implementation ofbytes, or would you accept thebytes(...) - savedform (finding 1) so the capacity filter and padding arithmetic live in exactly one place? - Would you take the
count/granule/tailderivation (finding 2) in place of the foursentinel_granules > 1branches? It makes then ≤ 1fall-through a consequence of one formula rather than a hand-maintainedelse. - Not found: any reason the three new
sentinel_*names were chosen over the repo's existing "null row" vocabulary — if none, renaming now avoids two names for one concept in the same file.