From d0a49a6a240c5cc142128565feb0917c26c83362 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:08:38 +0000 Subject: [PATCH] perf: look up parent package initializers by prefix in dead-code reachability Linking each module to the initializers of its parent packages compared every module with every other one and formatted a prefix string per pair, which grows quadratically with the module count. Each module now looks up its own dotted prefixes in a set of initializer names. --- crates/fensu-facts/src/dead_code/_helpers/graph.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/fensu-facts/src/dead_code/_helpers/graph.rs b/crates/fensu-facts/src/dead_code/_helpers/graph.rs index 5d8b13b1..b5be25c2 100644 --- a/crates/fensu-facts/src/dead_code/_helpers/graph.rs +++ b/crates/fensu-facts/src/dead_code/_helpers/graph.rs @@ -68,12 +68,18 @@ pub(crate) fn extract(modules: &[Module], entries: &[ProjectEntryPoint]) -> Grap .visit_body(&module.program.module().body); graph.bindings.push(bindings); } + let initializers: HashSet<&str> = modules + .iter() + .filter(|module| module.initializer) + .map(|module| module.name.as_str()) + .collect(); for (index, module) in modules.iter().enumerate() { let owner = graph.nodes[&(index, String::new())]; - for parent in modules.iter().filter(|parent| { - parent.initializer && module.name.starts_with(&format!("{}.", parent.name)) - }) { - graph.edge(owner, &Binding::Module(parent.name.clone())); + for (end, _) in module.name.match_indices('.') { + let parent: &str = &module.name[..end]; + if initializers.contains(parent) { + graph.edge(owner, &Binding::Module(parent.to_owned())); + } } for hook in ["__getattr__", "__dir__", "__path__"] { graph.edge(