Repository navigation
Description
Found by branch review of conveyor-engine before merge (the in-flight XMILE conveyor+queue implementation; specs docs/design/conveyors.md + docs/design/queues.md). Filed from a review report that exceeded its 15-finding cap.
Summary
The per-DT conveyor pass has several hot-loop inefficiencies. The dominant one: phase_b's insert loop (src/simlin-engine/src/conveyor.rs:909-923) calls cohort_schedule once per non-zero placement share (:915, own_depth = i+1), and each call re-runs the share-independent zone_start_retained (:403; O(dn) plus 3 allocations) plus the share-independent per-k m_entry (:405). So an Even/Dist/Dest placement on a linear-leak belt does O(d^2 * n) work + O(d) allocations per belt per step, where hoisting zone_start_retained + m_entry out of the loop (and a prefix-count table for the per-slat m_own at :412) reduces it to O(dn).
Failure scenario / cost
A linear-leak belt with an Even or Dist inflow at transit 5 / dt 0.01 (d = 500 non-zero shares) and 2 leaks: the insert loop makes ~500 cohort_schedule calls per step, each recomputing zone_start_retained (~O(5002) inner iterations + 3 allocs) and per-k O(500) m_entry/m_own scans -- on the order of 10^6 inner iterations and ~2500 small allocations per step; a 10^4-step run multiplies that by 10^4. The common Beginning placement (one non-zero share) and the exponential-leak path (cohort_schedule early-returns at conveyor.rs:400) are already O(dn) and unaffected, so the blowup is specific to non-default placement + linear leakage.
Secondary/conditional siblings (all real, each fires only for particular inflow kinds):
- per-slat
shedsalloclet mut sheds = vec![0.0; n];insidefor i in 0..l(conveyor.rs:719), exponential-leak branch only - always-materialized
n x lslat_vols(:705) consumed only by a source placement (conveyor_compile.rs:1800); a documented tradeoff per the struct comment at:156-157 - O(L_up*d)
source_weightsnearest-slat scan with a closed-form index available (conveyor_compile.rs:1752) - all-plans
find_upstream_leakrescan per step (conveyor.rs:1771) - per-step
dist_weightsrebuild (conveyor_compile.rs:1704, each entry doing acrate::vm::lookupat:1722)
Evidence
- insert loop:
for (i, &share) in shares.iter().enumerate() { if share == 0.0 { continue; } let (basis, window) = self.cohort_schedule(share, belt_len, d, i + 1, inp.leak_fractions);(conveyor.rs:909-916) - share-independent recomputation:
let r = self.zone_start_retained(belt_len, entry_depth, fractions);(:403),let m_entry = self.zone_count_from(k, belt_len, entry_depth);(:405),let m_own = self.zone_count_from(k, belt_len, own_depth);(:412) zone_start_retainedallocates three Vecs, loops O(n*d) (:352/:356/:365)- d non-zero shares: Even
for share in shares.iter_mut().take(d)(conveyor_compile.rs:1015), Distfor (i, share) in shares.iter_mut().enumerate().take(d)(:1038) ConveyorStatehas no scratch/cache fields (:95-118)
Suggested direction
Hoist zone_start_retained/m_entry out of the per-share loop and add a prefix-count table for m_own; address the secondary allocations with reused scratch buffers on ConveyorState.
Activity
bpowers commentedon Jul 8, 2026
Additional allocation sites in the same class, noticed while fixing #878 (coupling table now precomputed at plan-attach time via
queue_compile::CouplingTable::buildinVm::set_conveyor_plans/set_queue_plans):The pass driver itself in
src/simlin-engine/src/conveyor_compile.rsheap-allocates several Vecs on every Euler step for any conveyor model, independent of placement kind or leak mode (so unlike the primary finding above, these fire on the common Beginning-placement / exponential-leak path too):run_phase_a(~lines 2805-2825): thearrested: Vec<bool>collect,pa: Vec<PhaseAResult>(with_capacity(plans.len())), and per-planfracs: Vec<f64>/leak_dest_arrested: Vec<bool>collects.conveyor_phase_b_one(~lines 2889-2913):eq_rates: Vec<f64>,placements: Vec<Placement>,conv_inflows: Vec<(f64, Placement)>, and thefracs: Vec<f64>collect (plus whateverConveyorState::phase_ballocates internally, e.g.pb.in_vols).These scale linearly with step count: a 10,000-step run pays them 10,000 times.
Related test gap: the allocation regression test added for #878 (
queue_model_allocations_do_not_scale_with_stepsinsrc/simlin-engine/tests/vm_alloc.rs) deliberately uses a queue-only model, because these conveyor-pass allocations would fail an equivalent assertion for a conveyor model. When the scratch buffers are hoisted (into the VM or a per-pass scratch struct, alongside theConveyorStatescratch suggested above), vm_alloc.rs should gain a conveyor-model non-scaling assertion so the fix stays pinned.