Skip to main content

compute_dead_code

Function compute_dead_code 

Source
pub fn compute_dead_code<S: BuildHasher>(
    graph: &RepoGraph,
    entry_def_indices: &HashSet<DefIndex, S>,
    include_test_paths: bool,
) -> DeadCodeReport
Expand description

Compute the set of definitions unreachable from any entry point.

Returns dead clusters (connected components in the unreachable subgraph), sorted by size descending.

§Algorithm

  1. Optionally filter test paths from entry_def_indices (when include_test_paths is false). Test-path heuristic: the file path contains test_, _test, tests/, spec/, specs/, or bench.
  2. Build full forward + reverse adjacency from RepoGraph::def_edges (the untruncated edge list) as a Compressed Sparse Row (CSR) pair of (starts, dst) Vec<u32> arrays — bounded O(E) memory rather than the O(n_defs * avg_fanout) of duplicated Vec<Vec<DefIndex>> that crashed at kernel scale (I#61). The BFS does NOT use RepoGraph::def_callees / RepoGraph::def_callers: those are display-oriented neighbor lists capped at [MAX_NEIGHBORS] per node, which dropped hub callees from the live set and produced false-positive dead reports (I#57).
  3. BFS forward over the full forward adjacency from the entry seeds -> reachable set.
  4. Complement (all_defs - reachable) = dead set.
  5. Connected-components on the dead subgraph via the full forward + reverse adjacency treated as undirected (union-find).
  6. For each cluster: pick the highest-rank def as the cluster root; size = member count; total_lines = sum of (end_line - start_line) per member.
  7. Sort clusters by size descending.