Subtraction, Simplicity & Conventions — PR #661 @ 3d868f84
Key findings
The diff is leaner than the average change of this size, and the lens should say so plainly: it deletes a duplicated ~60-line prefill branch out of add_candidate into a dedicated add_prefill_candidate (policy.rs:1422-1458), caches the previously recomputed terminal_prefill_window (scheduler.rs:847-858, field :662, init :757, refresh at :808/:843) instead of recomputing it per call, adds no CLI flag, no PolicyConfig field, no unsafe, no ABI change, and keeps the add_candidate/add_prefill_candidate split conventional. There is no reject-level convention violation.
What remains is five concrete simplifications, and four of them share one cause: the age-priced path was added alongside the existing terminal-completion logic instead of being expressed through it, so the same decision is made in two places with two spellings, and the same catalog walk exists three times.
| # | Cut | Where | Kind |
|---|---|---|---|
| 1 | last_interp memo + the 9-arg fn it forces |
policy.rs:517-543, :1092, :1429 |
dead weight |
| 2 | Three near-identical prefill-catalog scans | scheduler.rs:964-994, policy.rs:409-434, policy.rs:451-459 |
duplication |
| 3 | Age-price terminal dispatch spelled twice; has_active_age_priced_stream() called per request |
scheduler.rs:1007-1022 vs service.rs:297-301; policy.rs:750-757 |
duplication + cost |
| 4 | prefill_catalog_for_terminal / is_unmeasured_candidate re-dispatch wrappers |
policy.rs:371-374, :545-548 |
wrappers |
| 5 | Theory-doc restatement of the runtime-design paragraph; 25-line rationale-doc | docs/scheduling-theory.md:562-573, policy.rs:462-478 |
prose duplication |
1. Delete the last_interp memo and the 9-argument function it forces (highest value)
helios/src/scheduler/policy.rs:517-543:
#[allow(clippy::too_many_arguments)]
fn prefill_candidate_ns(
&self, plan: &mut PrefillSlotPlan, request: &LiveRequest, kind: GraphKind, capacity: usize,
work: usize, age_price: f64, predictor: Option<cost::PrefillPredictor>,
last_interp: &mut Option<(usize, u64, Option<u64>)>,
) -> Option<u64> {
...
if age_price > 0.0 && plan.nat_fit.is_some_and(|nat| capacity <= nat) {
let interp = match *last_interp {
Some((w, pos, res)) if w == work && pos == request.scheduled_consumed => res,
_ => {
let res = self.costs.interpolated_prefill_duration(kind, capacity, work, request.scheduled_consumed);
*last_interp = Some((work, request.scheduled_consumed, res));
res
}
};
if let Some(ns) = interp { return Some(ns); }
}
The memo is declared inside the per-capacity loop, not outside it: policy.rs:1429 (let mut last_interp: Option<(usize, u64, Option<u64>)> = None;, used at :1437) and policy.rs:1092 (used at :1118). So it can only hit when two different slots in the same mask happen to report identical (work, scheduled_consumed) — the capacity argument is fixed across the loop body, so repeated capacities never reach it. It buys essentially nothing while costing: an opaque tuple type spelled out longhand three times (no type alias), a #[allow(clippy::too_many_arguments)] that exists only because of the memo parameter, a &mut parameter threaded through both callers, and a match nested inside a if let inside a closure-visited function.
Replace the whole if age_price > 0.0 && ... block with:
if age_price > 0.0 && plan.nat_fit.is_some_and(|nat| capacity <= nat) {
if let Some(ns) = self.costs.interpolated_prefill_duration(
kind, capacity, work, request.scheduled_consumed,
) {
return Some(ns);
}
}
and delete last_interp from prefill_candidate_ns (:521), add_prefill_candidate (:1429, :1437) and continuation_candidate (:1092, :1118). That drops the function to 8 arguments and should make the clippy::too_many_arguments allow unnecessary — remove it if cargo clippy agrees. If the call cost genuinely matters, the memo belongs inside ActionCosts next to the O(entries) scan it guards (cost.rs:716 onward), not as a caller-owned one-slot cache; but there is no timing evidence in the PR that it matters, and interpolated_prefill_duration was verified pure in exactly (kind, capacity, work, scheduled_consumed) (cost.rs:695-776).
Supporting structural note: PrefillSlotPlan (policy.rs:89-96) carries two memo-ready flags plus nat_fit: Option<usize> that is computed once from the first kind seen for a slot (policy.rs:526-530) but then reused for Prefill, TerminalPrefill and ScoredPrefill because candidates() shares one prefill_plans array across kinds (policy.rs:1537, loop :1538-1548, use :1569-1574). Deleting the nat_fit memo — or keying it on kind — would drop two fields and one latent hazard; a value that two iter().find() passes produce does not need a hand-rolled per-slot cache. (Whether the cross-kind reuse is an actual behavioural defect is for the Correctness lens; from this lens it is simply an unnecessary memo.)
2. Collapse three near-identical prefill-catalog scans
The PR adds/keeps three functions that all walk a prefill catalog, compute prefill_work_*, resolve the kind, skip GraphKind::Prefill when terminal_only, and apply one differing acceptance test:
scheduler.rs:964-994 (prefill_must_complete_terminally_from), verbatim core:
for &capacity in catalog {
let work = self.prefill_work_from(request, start, capacity, cut);
if work == 0 {
continue;
}
if self.resolve_prefill_kind_from(request, start, capacity, work, cut)
== GraphKind::Prefill
{
return false;
}
if capacity >= limit {
return true;
}
}
policy.rs:409-434 (natural_fit_prefill_capacity_for_terminal):
for &capacity in catalog {
let work = self.prefill_work_through(request, capacity, cut);
if work == 0 {
continue;
}
let resolved = self.resolve_prefill_kind(request, capacity, work, cut);
if terminal_only && resolved == GraphKind::Prefill {
continue;
}
if self.costs.duration(resolved, 1, capacity).is_none() {
continue;
}
if best.is_none_or(|(_, best_work)| work > best_work) {
best = Some((capacity, work));
}
}
policy.rs:451-459 (fits_single_prefill_from) — same walk as a .any().
Collapse into one helper over (catalog, start, cut, terminal_only, accept: impl Fn(usize, usize, GraphKind) -> bool); the three callers become 3–4 lines each: accept = |_, _, k| k != GraphKind::Prefill with an extra capacity >= limit break for the terminal case, accept = |c, _, k| self.costs.duration(k, 1, c).is_some() with a best fold for natural fit, and accept = |c, w, k| w == remaining && self.costs.prefill_entry(k, c).is_some() for the single-shot fit. This also removes the _from / _through / plain-variant proliferation (prefill_work → prefill_work_through → prefill_work_from, policy.rs:376-403; resolve_prefill_kind vs resolve_prefill_kind_from) which exists only because these three loops disagree about where start comes from.
3. The age-price terminal dispatch is spelled twice, and the stream predicate is re-scanned per request
scheduler.rs:1007-1022:
pub(super) fn prefill_requires_terminal_completion_for_age(
&self,
request: &LiveRequest,
cut: Option<usize>,
age_price: f64,
) -> bool {
if age_price > 0.0 {
self.prefill_must_complete_terminally_from(
request,
Self::prefill_cursor(request),
cut,
)
} else {
self.prefill_requires_terminal_completion(request, cut)
}
}
service.rs:297-301 re-implements the same decision by hand, with a different start (consumed_tokens rather than prefill_cursor):
let terminal_only = if self.has_active_age_priced_stream() {
self.prefill_must_complete_terminally_from(request, start, cut)
} else {
self.prefill_requires_terminal_completion_from(request, start, cut)
};
The start divergence is a correctness question (Correctness lens), but from this lens it is the direct evidence that the helper's signature is wrong: it hard-codes Self::prefill_cursor(request) and keys off age_price > 0.0 instead of the stream predicate that service.rs actually has. Widen the helper to (request, start, cut, age_price) and have both sites call it; the age_price-vs-has_active_age_priced_stream() distinction disappears into one expression. One predicate, one spelling, and the start disagreement becomes impossible to reintroduce.
Related cost, same root cause: has_active_age_priced_stream() (scheduler.rs:996-1005) scans all slots, and service_rates calls it once per request from inside the rates fold at policy.rs:750-757, i.e. O(slots²) per lane_values pass — plus a third call from preserves_first_output_wait (policy.rs:503) and a fourth from prefill_step_requires_feedback (scheduler.rs:1188). The PR just learned this exact lesson for terminal_prefill_window (cached on catalog update, scheduler.rs:847-858) and for PrefillSlotPlan state. Do the same here: compute the bool once per lane_values pass and pass it down, or store it as derived state refreshed wherever slot occupancy changes.
4. Two wrappers worth deleting
policy.rs:371-374 — uses GraphKind as a boolean and adds a second level of indirection over prefill_catalog (:363-369):
fn prefill_catalog_for_terminal(&self, kind: GraphKind, terminal_only: bool) -> &[usize] {
self.prefill_catalog(if kind == GraphKind::ScoredPrefill { kind }
else if terminal_only { GraphKind::TerminalPrefill } else { GraphKind::Prefill })
}
Only two call sites (policy.rs:527, :875, :1840 — three total). Inline the match at each; the wrapper earns nothing and hides that ScoredPrefill silently overrides terminal_only.
policy.rs:545-548 — roundabout tuple destructuring to ask "no measured nanos":
fn is_unmeasured_candidate(&self, candidate: Candidate) -> bool {
candidate.rate.duration_ns == 0
|| self.costs.observation(candidate.cost_key()).and_then(|(_, observed, _)| observed).is_none()
}
Add ActionCosts::is_measured(Key) -> bool next to the existing is_fresh (cost.rs:867-869) and say it once, so the (key, observed, fresh) tuple shape is not re-destructured at every call site.
5. Documentation duplication
docs/scheduling-theory.md:562-573 restates, in 12 lines, the interpolated-discovery rule the same PR added to docs/scheduling-runtime-design.md:494-513 — and the paragraph itself already cross-links there ("match the Helios interpolated prefill-discovery rule in [the scheduling runtime design]"). Keep the algorithm in one place; replace the theory paragraph with two sentences, e.g. "initial_discovery = "interpolated" preserves native discovery whenever age_price == 0 or no decoding stream is resident; otherwise unmeasured pure-prefill capacities at or below natural fit are priced by sibling or peer interpolation, exactly as the scheduling runtime design specifies." This also removes the second copy that future changes would have to keep in sync.
policy.rs:462-478 — the doc comment on preserves_first_output_wait has grown to 25 lines carrying a justification parenthetical that reads as change history:
/// graph (carries across the forced split and boundary snapshot; we intentionally
/// do not require `prefill < step.token_capacity` because an anchor caps larger
/// capacities to the same work, and when `TerminalPrefill` clears `deferred_prefill`
/// at an interior anchor, `fits_single_prefill_from` runs against the pre-completion
/// `deferred_prefill` state so it checks the terminal prefill catalog), and
State the invariant ("an anchor caps larger capacities to the same work") and let the paragraph before it carry the case list; the "we intentionally do not require X because" clause belongs in the commit message, not the item doc. Minor, but it is the kind of prose that makes the next reader think the condition was a mistake to undo.
Verdict
No reject-level convention violation; nothing in this PR is speculative generality added for a flag nobody uses, and the diff actively removes code (the 60-line duplicated prefill branch) as well as adding it. Items 1–3 are the substantive cuts and are all bug-adjacent cleanups the author can land in one follow-up commit; items 4–5 are cleanup. If only one thing is taken: delete last_interp — it is pure dead weight with a clippy-allow cost, and deleting it removes the only reason that function has 9 parameters.
Open questions and what I could not verify
- No planner-timing evidence is given anywhere in the PR body, so I cannot state a measured cost for any of the memo/scan claims in 1–3. The argument for each cut is structural, not profile-driven. If the author has a benchmark showing the
last_interpmemo orhas_active_age_priced_stream()per-request scan matters, that changes the recommendation for item 1 (memo insideActionCostsinstead of deleted) and item 3. - Whether the
nat_fitcross-kind memo (policy.rs:526-530vs the sharedprefill_plansarray at:1537/:1569-1574) is a behavioural defect is a Correctness-lens question; here it is only an unnecessary memo. I did not determine whetherPlanOrigin::Discoveryclassification can differ between kinds for the same slot within onecandidates()pass. - Does any real signed core have a
TERMINAL_PREFILLcatalog that is a strict subset ofPREFILL? If not, the wholeterminal_onlyplumbing added acrossservice.rs/policy.rs/scheduler.rsis exercised only by synthetic tests — which would make item 3 less urgent but also means the duplication is guarding a case not yet observed in production. Not answered in the body. - I did not audit
policy_tests.rs(+1,762) for removable duplication, per the instruction to ignore nitpicking of good unit tests.