Skip to main content

compact

Function compact 

Source
pub fn compact(graph: &mut Graph, roots: &[NodeId]) -> NodeRemap
Expand description

Required compaction of the append-only arena (PR-B4).

Drops every node not in the live structure relative to roots (the exploration debris that pathfinders leave) and rebuilds the arena with contiguous, renumbered NodeIds, remapping every NodeId reference in the graph. Returns the NodeRemap from old → new ids so callers can fix up their own roots / NodeHandle handles.

The live set follows node inputs from roots, the side_effect_roots, and the full multi-path structure — for every reachable Op::Branch, its reconverge_at and all its arms (so Phase C’s route picker keeps its alternatives). Everything else is dropped.

Not in the per-realize hot path. Compaction renumbers NodeIds, which would invalidate a realize built around the current ids (the dispatch order + the StorageCache key are NodeId). It is a standalone pass — deliberately not registered as an Optimizer in the per-realize driver / optimize_graph loop — run at load-time between optimization rounds (Phase D) and required before finalize-to-disk (Phase E). Neither caller exists yet.

See Graph::verify_no_dangling for the post-condition the debug_assert checks: no NodeId reference (node inputs, the op-carried Op::Branch.reconverge_at, any side-table, or side_effect_roots) points outside the new arena.