Skip to content

ltm-finding: discovery mode (discover_loops) is intractable on World3 (390s / 22GB, no result) #540

Closed

Description

@bpowers

Summary

LTM has two loop-finding modes. Exhaustive mode (Johnson elementary-circuit enumeration) is gated by MAX_LTM_SCC_NODES = 50; a model whose variable-level causal-graph SCC exceeds that auto-flips to discovery mode -- the strongest-path heuristic from Eberlein & Schoenberg, "Finding the Loops That Matter" (2020), Appendix I, in src/simlin-engine/src/ltm_finding.rs. World3's variable-level SCC is 166, so it auto-flips. Discovery mode is the heuristic shortcut meant to make large models tractable -- but on World3 it does not terminate within any practical budget.

This was identified as Finding 3 of the LTM deep review, surfaced during the LTM hardening effort (epic #488) while adding test/tracking coverage.

Re-measured numbers (release build, current HEAD dbc0844e)

The phases before discovery are all fast:

  • parse: ~4 ms
  • compile (LTM discovery mode): ~136 ms
  • simulate (401 saved timesteps): ~178 ms cumulative
  • Project::from: ~184 ms cumulative

discover_loops itself:

  • Full discovery (all 401 timesteps): ran 390+ seconds and allocated 22+ GB RAM before a hard kill, no result produced.
  • Discovery over a single discoverable timestep (results truncated to step_count = 2): did not finish within 90 seconds, allocated 8.5+ GB RAM, killed.

IMPORTANT: this is post the Finding 1 fix (commit 297c5ee, which removed a 1/dt inflation of flow-to-stock link scores -- World3's dt is 0.5, so pre-fix those scores were ~2x too large). The Finding 1 fix did not make World3 discovery tractable.

Root cause

The single-timestep measurement is the decisive signal: the per-timestep strongest-path DFS itself is the blow-up, not merely the 401x per-timestep multiplier. A single timestep alone is intractable.

SearchGraph::check_outbound_uses (ltm_finding.rs, ~line 184) is a recursive DFS with best_score-based pruning: if score < current_best { return; }, where score is the running product of |link_score| along the path. This pruning only bounds work when path-score products shrink monotonically along paths. On World3's dense 166-node SCC, with link scores straddling 1.0, products do not shrink: a node's best_score is raised repeatedly (once per successively-stronger path that reaches it), and each raise re-explores that node's entire subtree. There is no polynomial-delay guarantee (unlike Johnson's enumerator). The result is super-polynomial subtree re-exploration that effectively enumerates a large fraction of the graph's elementary circuits -- for World3's element graph that population is ~1.86M circuits (see tests/wrld3_ltm_panic.rs).

Compounding the algorithmic problem, results accumulate without bound: found_loops per timestep + all_paths across timesteps + the seen_sets HashSet<Vec<String>> dedup keys. The MAX_LOOPS = 200 truncation happens only at the very end in rank_and_filter, after all paths across all timesteps have been collected and converted to FoundLoop with full score series -- so it is never reached. This is the source of the 22 GB.

Contributing factors / likely fix targets

  • ltm-finding: SearchGraph clones Ident<Canonical> in per-timestep DFS hot path #481: Ident<Canonical> (heap-allocated String) cloning in the hot DFS path -- visiting.insert(variable.clone()), stack.push(variable.clone()), best_score.insert(variable.clone(), ...), plus seen_sets: HashSet<Vec<String>>. The exhaustive path (src/simlin-engine/src/ltm/indexed.rs) was specifically optimized to swap Ident<Canonical> for compact u32 indices (visited: Vec<bool>, path: Vec<u32>, u32-keyed dedup) -- see the doc comment at the top of indexed.rs. The discovery path never received that treatment, and discovery is the path that runs on the big models. (Note: ltm-finding: SearchGraph clones Ident<Canonical> in per-timestep DFS hot path #481 addresses the constant-factor cost only; it would not by itself make a 390s/22GB run tractable -- the algorithmic blow-up below is the dominant issue.)
  • Per-timestep rebuild: SearchGraph::from_results (ltm_finding.rs, ~line 112) rebuilds the adjacency list (cloning every from/to Ident) and re-sorts every adjacency list, once per timestep -- 401 times for World3 -- even though the graph structure is static across timesteps (only the edge scores change).
  • Per-stock best_score re-init: find_strongest_loops (ltm_finding.rs, ~line 136) allocates a fresh HashMap and inserts an entry -- with a .clone() of the Ident -- for every node in the graph, for every (stock x timestep) pair.
  • add_loop_if_unique (ltm_finding.rs, ~line 247): builds a Vec<String> from the stack, runs the O(n^2) canonical_rotation, and does a HashSet lookup -- for every loop found.
  • The 401x per-timestep multiplier in discover_loops_with_graph (ltm_finding.rs, ~line 742, for step in 1..step_count) -- secondary, since a single timestep is already intractable, but it multiplies everything.

Why this matters

Discovery is meant to be the tractable path for large models, but it degenerates to worse than the exhaustive enumeration it replaces: Johnson on World3's element graph finds ~1.86M circuits in ~30 s (debug), is polynomial-delay, and is u32-indexed. For external comparison, Eberlein & Schoenberg (2020) report 10-20 s for Urban Dynamics -- a model larger than World3 -- so the gap is algorithmic, not fundamental.

Production impact: src/simlin-engine/src/analysis.rs::analyze_model calls discover_loops_with_graph as its production path. Any model that auto-flips to discovery (variable-level SCC > 50) will hang/OOM in analyze_model. (analyze_model degrades gracefully on simulation failure, but discovery is post-simulation -- it would hang, not degrade.)

Existing / new test coverage

  • Coverage gap: tests/wrld3_ltm_panic.rs only asserts that LTM compilation finishes; it never runs discover_loops.
  • New (added by the LTM hardening effort, Piece 4): src/simlin-engine/tests/ltm_discovery_large_models.rs -- world3_discovery_single_timestep is an #[ignore]d, bounded (single-timestep), runnable repro with a FIXME(Finding 3) doc comment that encodes the contract discovery should satisfy (terminates, non-empty loop set, finite scores) and currently fails at the timeout. It will go green when this issue is fixed. clearn_ltm_discovery_blocked_by_macro_expansion documents that C-LEARN is not yet a viable second fixture (blocked at compile by engine: C-LEARN cannot simulate because macros are not expanded #349/engine: incremental compiler panics on C-LEARN #363).

Key file:line references

  • src/simlin-engine/src/ltm_finding.rs: check_outbound_uses (~184), find_strongest_loops (~136), discover_loops_with_graph per-timestep loop (~742), SearchGraph::from_results (~112), add_loop_if_unique (~247), rank_and_filter / MAX_LOOPS (~890).
  • src/simlin-engine/src/ltm/indexed.rs: the u32-indexed optimization the discovery path lacks (top-of-file doc comment).
  • src/simlin-engine/src/analysis.rs: analyze_model / run_ltm_pipeline -- the production caller.

Scope

Making discovery tractable is a large algorithmic undertaking, explicitly out of scope for the test/tracking work that surfaced it. This issue captures the finding, the measurements, and the root-cause analysis for a future dedicated effort.

Tracked under epic #488. Related (contributing sub-factor, not a duplicate): #481.

Activity

  1. added
    ltm
    on May 14, 2026
  2. bpowers commented on Jun 2, 2026

    @bpowers
    OwnerAuthor

    Fixed by the discovery rewrite 081e984 (plus 94f9c07 for the table-mediated link scores that previously zeroed most of World3's feedback structure).

    Verified empirically on main (521fc37), release build, full 401 timesteps on World3-03: the strongest-path discovery DFS completes in 0.21s and finds 200 loops (truncated: false), peak process RSS ~103MB — was 390+s / 22+GB / no result. The bounded #[ignore]'d repro world3_discovery_single_timestep now passes (33 loops in 4.1ms). Follow-up tracked on the ltm-gaps branch: un-ignore that test and refresh the now-stale FIXME(Finding 3) module docs in tests/ltm_discovery_large_models.rs.

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

    ltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions