Skip to content

ltm: JohnsonState per-SCC allocation sized to whole-graph node count #460

Closed

Description

@bpowers

Summary

In IndexedGraph::enumerate_circuits_in_scc (src/simlin-engine/src/ltm.rs), the JohnsonState fields blocked, b_list, and in_scc are sized to self.nodes.len() (the whole indexed graph) on every per-SCC call. On graphs with many small SCCs in a large node space this is wasteful memory -- each per-SCC call allocates O(|whole graph|) when it only needs O(|SCC|).

Concrete cost

For a 10,000-node graph with 100 SCCs averaging 10 nodes each:

  • blocked: Vec<bool> -- ~10,000 bytes per call
  • b_list: Vec<Vec<u32>> -- ~240,000 bytes per call (24-byte empty Vec headers)
  • in_scc: Vec<bool> -- ~10,000 bytes per call

Total: ~260 KiB allocated transiently per SCC, ~26 MiB across 100 SCCs, of which only ~30 entries (3 * |SCC| = 30) are actually indexed into. The remaining 99.7 percent of each allocation is dead weight.

Note that the runtime cost is bounded: the per-start reset loops inside enumerate_circuits_in_scc correctly only clear the SCC members (O(|SCC|) per start) rather than zeroing the whole graph, so the dominant cost is purely the one-shot allocation size, not the inner loops.

Why it matters

  • Peak RSS during LTM circuit enumeration is the load-bearing constraint tracked in PR engine: reduce LTM circuit-enumeration memory and time on dense graphs #459 (the recent ltm_mem_bench work). On dense single-SCC graphs like wrld3 this sizing is a non-issue (single 166-node SCC -> ~500 bytes of blocked+b_list+in_scc per call, totally negligible). It becomes a memory hog on sparse SCC structures -- large models with many disconnected feedback subsystems.
  • Developer expectation: enumerate_circuits_in_scc takes an &[u32] SCC, so the naive mental model is "memory scales with the SCC". Today it scales with the whole graph.

Fix sketch

Size blocked, b_list, and in_scc to scc.len() with a local->global index remap on emission (circuits must be returned with global indices to preserve the public contract -- emitted paths are used by downstream dedup and by build_element_level_loops).

The DFS hot path would then look up blocked[local_idx] where local_idx = global_to_local[global_idx]. The global_to_local map can be a dense Vec<i32> sized self.nodes.len() (one allocation per entire call to enumerate_all_circuits, not per SCC) with -1 sentinel for non-members; reset by walking the previous SCC's members before the next SCC is processed.

Trade-off: adds one indirection (global_to_local[w]) per DFS visit, which lives in the inner loop. Worth benchmarking on wrld3 (single large SCC, most-visited code path) before landing -- the win is on sparse-SCC models but we should not regress wrld3.

An alternative that avoids the remap: keep the full-size blocked/b_list/in_scc buffers but hoist them into a reusable scratch allocation on IndexedGraph (or threaded through enumerate_all_circuits) so they amortize across SCCs. Cheaper to implement but doesn't reduce total allocation size for single-large-graph callers.

Measurement plan

Extend src/simlin-engine/examples/ltm_mem_bench.rs (or add a sibling) to accept a synthetic-graph mode: N=10000 nodes distributed across 200 SCCs of varying sizes (mix of 5, 10, 50, 100-node SCCs). Compare peak RSS and wall-time for enumerate_all_circuits before and after. Also run the existing wrld3 compile-time benchmark to verify no regression on the single-SCC case.

Why this was deferred

Flagged as [P3] 'no-op on wrld3' by a reviewer during PR #459 iteration 7. wrld3 has a single 166-node SCC, so the current sizing costs only ~313 bytes * 3 = ~1 KiB per call, negligible. The fix pays off for larger models with sparse SCC structure, which aren't yet the primary performance workload.

Component

simlin-engine -- specifically src/simlin-engine/src/ltm.rs, function IndexedGraph::enumerate_circuits_in_scc (and JohnsonState definition / construction at lines ~436-445 and ~708-716).

Discovery context

Identified during PR #459 ('engine: reduce LTM circuit-enumeration memory and time on dense graphs') code review at the commit df2f240 timeframe.

Activity

  1. added
    ltm
    on May 1, 2026
  2. added a commit that references this issue on May 7, 2026
    d136a07
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