Skip to content

conveyor-engine: per-step conveyor pass hot-loop recomputes share-independent work and re-allocates scratch #879

Closed

Description

@bpowers

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 sheds alloc let mut sheds = vec![0.0; n]; inside for i in 0..l (conveyor.rs:719), exponential-leak branch only
  • always-materialized n x l slat_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_weights nearest-slat scan with a closed-form index available (conveyor_compile.rs:1752)
  • all-plans find_upstream_leak rescan per step (conveyor.rs:1771)
  • per-step dist_weights rebuild (conveyor_compile.rs:1704, each entry doing a crate::vm::lookup at :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_retained allocates 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), Dist for (i, share) in shares.iter_mut().enumerate().take(d) (:1038)
  • ConveyorState has 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

  1. added
    engine
    on Jul 8, 2026
  2. bpowers commented on Jul 8, 2026

    @bpowers
    OwnerAuthor

    Additional allocation sites in the same class, noticed while fixing #878 (coupling table now precomputed at plan-attach time via queue_compile::CouplingTable::build in Vm::set_conveyor_plans/set_queue_plans):

    The pass driver itself in src/simlin-engine/src/conveyor_compile.rs heap-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): the arrested: Vec<bool> collect, pa: Vec<PhaseAResult> (with_capacity(plans.len())), and per-plan fracs: 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 the fracs: Vec<f64> collect (plus whatever ConveyorState::phase_b allocates 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_steps in src/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 the ConveyorState scratch suggested above), vm_alloc.rs should gain a conveyor-model non-scaling assertion so the fix stays pinned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enginetech debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions