#1 [high] Subtraction/Simplicity review of #661 — findings with citations
Head reviewed: c588d610 on 1fbcc3c9. Lens: subtraction/simplicity/conventions.
F1 (HIGH) Verbatim-duplicated 33-line interpolated-exploration block, twice in policy.rs.
Copy A: policy.rs:1110-1142 (inside continuation_candidate's consider closure):
let ns = match predictor {
Some(p) => p.predict(work, request.scheduled_consumed),
None => {
let nat_fit =
self.prefill_slot_nat_fit(prefill_plans, slot, request, following);
if age_price > 0.0
&& nat_fit.is_some_and(|nat| width <= nat)
&& let Some(interp) = match last_interp {
Some((w, pos, res))
if w == work && pos == request.scheduled_consumed =>
{
res
}
_ => {
let res = self.costs.interpolated_prefill_duration(
following, width, work, request.scheduled_consumed,
);
last_interp = Some((work, request.scheduled_consumed, res));
res
}
}
{ interp } else if nat_fit == Some(width) { 0 } else { continue; }
}
};
Copy B: policy.rs:1462-1494 (inside add_candidate), identical modulo kind/capacity/r.
Fix: extract one method, e.g.
fn explore_prefill_ns(&self, request:&LiveRequest, kind:GraphKind, capacity:usize, work:usize, age_price:f64) -> Option<u64> returning None = exclude candidate (the continue), Some(0) = natural-fit discovery, Some(ns) = interpolated; keep the prefill_slot_nat_fit call inside it. Both sites become let ns = match self.explore_prefill_ns(...) { Some(ns) => ns, None => continue }; for B and match ... {Some(ns)=>ns, None=>return} for A. ~66 lines -> ~16. Also deletes the second let mut last_interp declaration (policy.rs:1081, :1443) and the (usize,u64,Option<u64>) triple.
F1b last_interp memo (policy.rs:1081, 1117-1132, 1443, 1469-1484) is speculative micro-optimisation: it only hits when two different slots in the same capacity candidate have identical (work, scheduled_consumed). interpolated_prefill_duration is an O(#graphs) integer scan. Dropping the memo removes ~20 lines of match/match/_ => plumbing at no algorithmic loss; the PR's own 4.7x-8.65x plan() figure predates it.
F2 (HIGH) Duplicated "unmeasured ⇒ Discovery" expression, verbatim twice: policy.rs:1784-1789 (select_plan):
let ordinary_unmeasured = ordinary.rate.duration_ns == 0
|| self
.costs
.observation(ordinary.cost_key())
.and_then(|(_, observed, _)| observed)
.is_none();
policy.rs:2071-2077 (lifecycle phase_plan path) same expression inlined into let origin = if ....
Fix: one helper, e.g. on Candidate or RequestScheduler:
fn is_unmeasured(&self, c: Candidate) -> bool { c.rate.duration_ns == 0 || self.costs.duration_key(c.cost_key()).is_none_or(|ns| ns == 0) }
— observation(k).and_then(|(_,o,_)| o).is_none() is exactly "nanos == 0 or entry absent" (cost.rs:904-908 returns (nanos, (nanos != 0).then_some(observed_at_ns), fresh)), so duration_key(...).is_none_or(|ns| ns == 0) is equivalent and avoids the tuple dance. 14 lines -> 4 (helper + two call sites).
F3 (MED) Dead re-filter inside interpolated_prefill_duration, cost.rs:734-744:
if key.tokens > capacity {
if hi.is_some_and(|(c, cur_same, _)| {
key.tokens > c || (key.tokens == c && cur_same && !same_kind)
}) {
continue;
}
} else if lo.is_some_and(|(c, cur_same, _)| {
key.tokens < c || (key.tokens == c && cur_same && !same_kind)
}) {
continue;
}
This is a strict superset of the assignment filter at cost.rs:762-772 (hi.is_none_or(...) / lo.is_none_or(...) with the same comparison), so deleting 734-744 changes no result — the update below simply doesn't fire. Remove it (11 lines) and keep only the 762-772 update; optionally fold the same_kind/better_at_same_cap tie-break into a small fn better(candidate, current) -> bool used once.
F4 (MED) Test-fixture triplication in the same file, introduced by this diff: cost.rs:1284-1325, :1359-1400, :1478-1519 each define byte-identical make, step_for, timing closures (let make = |name: &str, kind: GraphKind, batch: usize, cap: usize| GraphDescriptor {...}, the ScheduledStep builder and the gpu_ns[0] = Some(ns) ExecutionTiming builder) — ~92 lines that one module-level fn prefill_graph(name:&str, kind:GraphKind, cap:usize) -> GraphDescriptor, fn prefill_step(kind, cap, tokens) -> ScheduledStep, fn timing(ns:u64) -> ExecutionTiming would carry (the existing make_prefill at :1092, :1197 shows the file already has this shape). Saves ~60 lines; no behavioural change.
F5 (MED) service.rs double catalog scan (scan_prefill(terminal_only) then scan_prefill(false)), service.rs:298-328:
let scan_prefill = |skip_nonterminal: bool| -> f64 { ... };
token_rate = token_rate.max(scan_prefill(terminal_only));
if token_rate == 0.0 && terminal_only && self.has_active_age_priced_stream()
&& !self.prefill_must_complete_terminally_from(request, start, cut) {
token_rate = token_rate.max(scan_prefill(false));
}
The second closure call re-walks the whole catalog doing the same prefill_work_from/resolve_prefill_kind_from/prefill_duration work. Track both maxima in one pass (best_all, best_terminal) and select afterwards — this is also the extra per-call catalog scan the reach finding (A2) mentions. Same loop, half the scans, no skip_nonterminal parameter.
F6 (LOW) Two near-identical catalog selectors: policy.rs:363-369 prefill_catalog(kind) and policy.rs:371-379 prefill_catalog_for_terminal(kind, terminal_only) differ only in that the second takes the bool instead of kind == TerminalPrefill. Collapse to fn prefill_catalog_for(&self, kind, terminal_only) and define/replace the old one as self.prefill_catalog_for(kind, kind == GraphKind::TerminalPrefill) (or drop the bool and have callers pass the catalog slice they already compute). 8 lines -> ~3, and removes the chance the two drift (their ScoredPrefill/TerminalPrefill precedence already differs in appearance).
F7 (LOW, note not demand) PrefillSlotPlan (policy.rs:89-96) + prefill_slot_state (:501-516) + prefill_slot_nat_fit (:518-537) thread a &mut [PrefillSlotPlan; REQUEST_LANES] through add_candidate/continuation_candidate and all 8 call sites plus two fresh arrays (:1599, :2052). The cut half of that memo is redundant: estimated_checkpoint_cut already goes through the per-planning-scope checkpoint_selections RefCell cache (checkpoints.rs:410-428, called at :745-753). Only nat_fit needs memoising; if prefill_slot_state is deleted the struct shrinks to nat_fit: Option<Option<usize>> and one helper, and ready/nat_fit_ready/cut/terminal_only go away. Say this as "worth a follow-up", not a blocker.
Docs duplication: docs/scheduling-runtime-design.md states the interpolation/exploration rule twice — :484-505 (exploration section, ~14 lines) and :662-671 (dispatch pricing section) — same content ("capacities up to the natural-fit capacity (the natural fit alone when no active age-priced streams exist)"). One should point at the other.
Verified NOT problems (state plainly): no new flags/knobs/env vars, no unsafe, no per-model branches, no ABI change; terminal_prefill_window (scheduler.rs:662, 847-866) is a genuine simplification replacing a recomputed binary-search-and-max at a call site; prefill_must_complete_terminally_from/has_active_age_priced_stream (scheduler.rs:964-1005) are tight and well-scoped; py check.py/workloads.py changes are additive fixtures, no dead code. The +397/-78 prod diff is otherwise lean and idiomatic for this module.
#2 [high] Research summary
Subtraction/simplicity lens on #661 (head c588d610). The production diff is mostly lean and idiomatic — no new flags, knobs, env vars, unsafe, per-model branches, or ABI change, and terminal_prefill_window (scheduler.rs:662, 847-866) plus the two guard helpers (scheduler.rs:964-1005) are genuine simplifications. The subtraction surface is concentrated in five places, all recorded in scratchpad entry 1 with verbatim quotes and line ranges:
High value. (1) A 33-line interpolated-exploration block is copy-pasted verbatim twice — policy.rs:1110-1142 (continuation_candidate) and policy.rs:1462-1494 (add_candidate) — differing only in kind/capacity/r; extract one explore_prefill_ns(...) -> Option<u64> (None = exclude) and both sites collapse to one line. (2) The last_interp (usize, u64, Option<u64>) memo declared and matched twice (:1081/:1117-1132, :1443/:1469-1484) is speculative micro-optimisation over an O(#graphs) integer scan and falls out of that extraction. (3) The "unmeasured ⇒ Discovery" expression is duplicated at policy.rs:1784-1789 and :2071-2077 and can be one helper using duration_key(..).is_none_or(|ns| ns == 0) instead of the observation(...).and_then(...) tuple dance (observation at cost.rs:904-908 makes these equivalent).
Medium. (4) interpolated_prefill_duration carries an inverted duplicate of its own peer-selection comparison at cost.rs:734-744 plus the real one at :762-772 — collapse into a single better(candidate, current) predicate. (5) service.rs:298-328 scans the prefill catalog twice via scan_prefill(terminal_only) then scan_prefill(false); track both maxima in one pass, which also halves the extra per-call scan flagged as an undeclared reach item. (6) ~92 lines of byte-identical make/step_for/timing test fixtures are triplicated in cost.rs tests (:1284, :1359, :1478) where the file already has make_prefill helpers.
Low / follow-up. prefill_catalog and prefill_catalog_for_terminal (policy.rs:363-379) are near-identical selectors that can collapse; PrefillSlotPlan (:89-96) + prefill_slot_state (:501-516) memoise a cut that checkpoint_selections already caches (checkpoints.rs:410-428, :745-753), so the struct could shrink to the single nat_fit memo. The runtime-design doc states the interpolation/exploration rule twice (:484-505 and :662-671) and one should point at the other.
#3 [medium] #661 subtraction review — supplementary citation detail (quotes for F6/F7, doc duplication, thread-through call sites)
Companion to entry 1. Head c588d610 on 1fbcc3c9.
F6 verbatim — the two near-identical selectors, policy.rs:363-379 (unchanged/new pair):
fn prefill_catalog(&self, kind: GraphKind) -> &[usize] {
match kind {
GraphKind::ScoredPrefill => &self.scored_prefill_catalog,
GraphKind::TerminalPrefill => &self.terminal_prefill_catalog,
_ => &self.prefill_catalog,
}
}
fn prefill_catalog_for_terminal(&self, kind: GraphKind, terminal_only: bool) -> &[usize] {
if kind == GraphKind::ScoredPrefill {
&self.scored_prefill_catalog
} else if terminal_only {
&self.terminal_prefill_catalog
} else {
&self.prefill_catalog
}
}
Same three-way selection, expressed twice; collapse to the bool form and let the kind-only callers pass kind == GraphKind::TerminalPrefill. Callers of prefill_catalog_for_terminal: policy.rs:527, :864, :1908 (plus :877 via nat_fit helper). Callers of prefill_catalog: policy.rs:449, :1632, :2054.
F7 verbatim — the memo struct and its two accessors, policy.rs:89-96, :501-537:
#[derive(Clone, Copy, Default)]
struct PrefillSlotPlan {
ready: bool,
cut: Option<usize>,
terminal_only: bool,
nat_fit_ready: bool,
nat_fit: Option<usize>,
}
fn prefill_slot_state(
&self,
slot_plans: &mut [PrefillSlotPlan; REQUEST_LANES],
slot: usize,
request: &LiveRequest,
age_price: f64,
) -> (Option<usize>, bool) {
let plan = &mut slot_plans[slot];
if !plan.ready {
plan.cut = self.estimated_checkpoint_cut(request);
plan.terminal_only =
self.prefill_requires_terminal_completion_for_age(request, plan.cut, age_price);
plan.ready = true;
}
(plan.cut, plan.terminal_only)
}
plus prefill_slot_nat_fit at :518-537. The cut memo is redundant with the existing cache: estimated_checkpoint_cut (checkpoints.rs:745-753) goes through checkpoint_selection_cached (checkpoints.rs:410-428), which stores results in the checkpoint_selections: RefCell<Option<HashMap<..>>> per planning scope. So only nat_fit genuinely needs memoising; ready/cut/terminal_only/nat_fit_ready could be replaced by a single [Option<Option<usize>>; REQUEST_LANES].
Thread-through cost of the &mut [PrefillSlotPlan; REQUEST_LANES] parameter (why it is worth a follow-up rather than free): it is added to two signatures (continuation_candidate policy.rs:1048, add_candidate :1436) and passed at policy.rs:1571, :1633, :1637, :1643, :1649, :2055, :2059, with two fresh arrays allocated at :1599 and :2052 — all on the plan() hot path (the PR advertises plan() got 4.70x-8.65x faster, so the arrays are cheap, but the plumbing is 9 sites).
Docs duplication detail — docs/scheduling-runtime-design.md:484-505 (exploration section) already says "When active age-priced streams ... exist and a measured sibling (PREFILL or TERMINAL_PREFILL) or strictly larger measured peer plus a positive lower floor ... is available, unmeasured pure-prefill capacities up to the natural fit compete by ValueRate", and docs/scheduling-runtime-design.md:662-671 (dispatch pricing) restates the same rule as "Pure prefill dispatch explores only capacities up to the natural-fit capacity (the natural fit alone when no active age-priced streams exist)". Second should cross-reference the first.
Already-lean items to state plainly in the report (so it reads as a verdict, not just a defect list): no new flags/knobs/env vars/unsafe/per-model branches/ABI change; terminal_prefill_window (scheduler.rs:662, :847-866) replaces a recompute-per-call binary-search-and-max at :864-866 while keeping the accessor signature; prefill_must_complete_terminally_from + has_active_age_priced_stream + prefill_requires_terminal_completion_for_age (scheduler.rs:964-1022) are short and single-purpose; scripts/scheduling_sim/workloads.py and verification/helios-lifecycle/check.py changes are additive fixtures with no dead code.