Skip to content

ltm: a repeated dimension makes loop finding about 150x slower #1017

Open

Description

@bpowers

When a variable is subscripted by the same dimension twice (cube[D1,D1,D2]), exhaustive loop finding enumerates tens of thousands of element-level loops on a tiny model, then throws every one of them away because the link they all pass through cannot be scored.
LTM on that model is about 150x slower than on the same model with distinct dimensions.

Example

D1 = r1, r2;  D2 = c1, c2
cube[D1,D1,D2] = 0.0001 * pop[D1,D1]
pop[D1,D1]     = INTEG(x, 100)
x[D1,D1]       = SUM(cube[D1,D1,*])
model LTM variable generation element loops enumerated
as above about 200 ms 34,904, all discarded
cube[D1,D3,D2], pop[D1,D3] (same element counts) 1.4 ms 0
as above, discovery mode 1.1 ms n/a

The cube -> x link is declined with a warning (which D1 the sum keeps is ambiguous), and every enumerated loop passes through it.
Enumeration itself is about 13 ms; roughly 93% of the time is the per-loop processing afterwards (canonical rotation, id sorting, partition assignment), mostly string allocation.

Why it matters

Seen only in our own test fixture (square_owner_whole_rhs_declines_and_skips_loudly in tests/integration/ltm_array_agg.rs, about 2 s in a debug build against 0.3 s for its siblings).
Repeating a dimension is an ordinary shape (origin-destination matrices), and the cost grows much faster than the element count.
The guards do not catch it: MAX_LTM_SCC_NODES limits nodes (this SCC has 16) and MAX_LTM_CIRCUITS is 100,000.

Fix

Recommended: find the links that will be declined before element-level enumeration and leave them out of the graph, so loops through them are never enumerated.
Whether the decline is decidable that early has not been checked.
This changes which loops model_detected_loops reports for such models, so it needs a check that the scored and reported loop lists still agree.

Alternative (helps every model that takes the element-level path): do canonical rotation and id sorting on integer node ids and build the strings only for loops that survive.

Pointers

  • src/simlin-engine/src/db/analysis.rs: model_loop_circuits_tiered
  • src/simlin-engine/src/db/ltm/mod.rs: model_ltm_variables, where loops crossing a declined link are dropped
  • src/simlin-engine/src/db/ltm/loops.rs: build_element_level_loops, build_element_subscripted_links
  • src/simlin-engine/src/ltm/graph.rs: assign_loop_ids, loop_id_sort_key; src/simlin-engine/src/ltm/mod.rs: lex_smallest_rotation_start
  • ltm: element-level loop finding spends most of its time on strings #955 is the same slow path, from a profile of the same test

Activity

  1. added
    ltm
    on Aug 10, 2026
  2. changed the title [-]ltm: a repeated dimension in an owner routes to element-level Johnson and enumerates 34,904 circuits that are all declined (150x on an 8-element fixture)[/-] [+]ltm: a repeated dimension makes loop finding about 150x slower[/+] on Oct 12, 2026
  3. added
    bug
    on Oct 12, 2026
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

    bugltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions