Skip to content

ltm: enumerate variable-level loops first, expand only cross-element subgraph #482

Closed

Description

@bpowers

Summary

model_element_loop_circuits in src/simlin-engine/src/db_analysis.rs enumerates circuits directly on the element graph. For a pure-A2A model with V variables over an N-element dimension, every variable-level cycle inflates to N element-level cycles that build_element_level_loops then collapses back to one A2A loop. Cost is multiplicative when it should be additive in N.

The 2026-04-25 per-reference graph refactor removed the spurious N x N edges that made FixedIndex models worst-case, but the structural multiplication for pure-A2A loops remains. Dense-feedback A2A subgraphs still trip the MAX_LTM_SCC_NODES = 50 auto-flip gate sooner than variable-level enumeration would.

Locations

  • src/simlin-engine/src/db_analysis.rs:1497-1508 -- model_element_loop_circuits calls find_indexed_circuits_with_limit(usize::MAX) on the element graph directly
  • src/simlin-engine/src/db_ltm.rs::build_element_level_loops -- the collapsing step

Suggested fix

  1. Enumerate cycles at the variable level first.
  2. Tag each cycle's edges by RefShape: same-element (A2A pass-through), cross-element, or scalar.
  3. For cycles that are pure same-element, emit one Loop per cycle with dimensions set; do not enter the element-level enumerator.
  4. Only run element-level enumeration on the cross-element subgraph (or cycles whose tag set is mixed/cross-element).

Cost becomes additive in N for pure-A2A loops instead of multiplicative; SCC width stays below the auto-flip threshold for many real models.

Severity

medium

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions