Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
scan: a function whose helper does I/O isn't portable either (#75)
Onur Ugurc094523ac85e
diff --git a/src/hotspot.rs b/src/hotspot.rsindex d23bb81..88e9e37 100644--- a/src/hotspot.rs+++ b/src/hotspot.rs@@ -62,8 +62,97 @@ impl Hotspot {}}+/// Blockers about what a function does, not where it lives or what its signature looks like.+/// A helper with one of these can't be ported inside its caller either: the Rust copy would have to do+/// the same thing, and verify only compares results, so a port that skipped it would still pass.+/// The backends word them differently, so this lists each wording.+const PASSED_ON: [&str; 8] = [+ "does I/O",+ "not deterministic",+ "writes global state",+ "writes static fields",+ "writes package-level variables",+ "writes instance, class or global variables",+ "uses global variables",+ "keeps static variables",+];++/// Whether `called`, as written at a call site in `file`, is the name `name`.+/// PHP function and method names are case-insensitive and may be written `\name`.+fn same_name(file: &str, called: &str, name: &str) -> bool {+ if file.ends_with(".php") { called.trim_start_matches('\\').eq_ignore_ascii_case(name) } else { called == name }+}++/// Python and Node only record calls to module-level functions, so there a call never means a method.+const MODULE_LEVEL_CALLS: [&str; 9] = [".py", ".js", ".mjs", ".cjs", ".jsx", ".ts", ".mts", ".cts", ".tsx"];++/// Indices of the functions a call to `called` from `caller` may mean: same file, same name, never the+/// caller itself (an overload shares its name, not its line). When some of them are also the closer+/// match, only those: in Python and Node a module-level function, elsewhere one with the caller's kind+/// and owner, so a method of the caller's own class over one of another class. Overloads all stay in.+fn helper_indices(list: &[Hotspot], caller: &Hotspot, called: &str) -> Vec<usize> {+ let all: Vec<usize> = (0..list.len())+ .filter(|&j| {+ let g = &list[j];+ g.file == caller.file+ && same_name(&caller.file, called, &g.name)+ && !(g.line == caller.line && g.name == caller.name)+ })+ .collect();+ let module_level = MODULE_LEVEL_CALLS.iter().any(|e| caller.file.ends_with(e));+ let near: Vec<usize> = all+ .iter()+ .copied()+ .filter(|&j| {+ let g = &list[j];+ if module_level {+ g.kind == "function" && g.owner.is_none()+ } else {+ g.kind == caller.kind && g.owner == caller.owner+ }+ })+ .collect();+ if near.is_empty() { all } else { near }+}++/// The functions a call to `called` from `caller` may mean (see [`helper_indices`]).+/// `rank` and `port` both find helpers through this.+pub fn helpers_named<'a>(list: &'a [Hotspot], caller: &Hotspot, called: &str) -> Vec<&'a Hotspot> {+ helper_indices(list, caller, called).into_iter().map(|j| &list[j]).collect()+}++/// A function that calls a helper with a [`PASSED_ON`] blocker gets that blocker too, through any+/// number of helpers, including ones blocked only by where they live (they're ported inside the caller+/// all the same). When a call may mean more than one function, one of them doing I/O is enough.+fn pass_on_helper_blockers(list: &mut [Hotspot]) {+ // what each function passes on to its callers, if anything+ let mut passed: Vec<Option<String>> =+ list.iter().map(|h| h.blockers.iter().find(|b| PASSED_ON.iter().any(|p| b.starts_with(p))).cloned()).collect();+ loop {+ let mut found = Vec::new();+ for (i, h) in list.iter().enumerate().filter(|(i, _)| passed[*i].is_none()) {+ let hit = h.calls.iter().find_map(|called| {+ let b = helper_indices(list, h, called).into_iter().find_map(|j| passed[j].as_ref())?;+ let verb = if b.starts_with("not ") { "is " } else { "" };+ Some(format!("calls {called}, which {verb}{b}"))+ });+ if let Some(blocker) = hit {+ found.push((i, blocker));+ }+ }+ if found.is_empty() {+ return;+ }+ for (i, blocker) in found {+ list[i].blockers.push(blocker.clone());+ passed[i] = Some(blocker);+ }+ }+}+/// Give each function an id and a score (time share x portability), best first.pub fn rank(mut list: Vec<Hotspot>, wall: f64) -> Vec<Hotspot> {+ pass_on_helper_blockers(&mut list);for h in &mut list {h.id = match h.module.as_deref() {Some(m) if m != "__main__" => format!("{m}.{}", h.qualname),@@ -74,20 +163,26 @@ pub fn rank(mut list: Vec<Hotspot>, wall: f64) -> Vec<Hotspot> {h.score = h.share * h.portability;}// Prefer the outermost portable function: one call into Rust instead of one per inner call.- let callers: Vec<(String, String, f64, Vec<String>)> = list- .iter()- .filter(|h| h.portable())- .map(|h| (h.id.clone(), h.file.clone(), h.share, h.calls.clone()))+ let covers: Vec<Option<String>> = (0..list.len())+ .map(|i| {+ let h = &list[i];+ if !h.portable() {+ return None;+ }+ list.iter()+ .filter(|g| {+ g.portable()+ && g.id != h.id+ && g.share >= 0.9 * h.share+ && g.calls.iter().any(|c| helper_indices(&list, g, c).contains(&i))+ })+ .max_by(|a, b| a.share.total_cmp(&b.share))+ .map(|g| g.id.clone())+ }).collect();- for h in list.iter_mut().filter(|h| h.portable()) {- let cover = callers- .iter()- .filter(|(id, file, share, calls)| {- *id != h.id && *file == h.file && calls.contains(&h.name) && *share >= 0.9 * h.share- })- .max_by(|a, b| a.2.total_cmp(&b.2));- if let Some((id, ..)) = cover {- h.covered_by = Some(id.clone());+ for (h, cover) in list.iter_mut().zip(covers) {+ if cover.is_some() {+ h.covered_by = cover;h.score = 0.0;}}@@ -139,4 +234,219 @@ mod tests {assert_eq!(ranked[2].score, 0.0);assert!(!ranked[1].worth_it());}++ fn calling(mut h: Hotspot, calls: &[&str]) -> Hotspot {+ h.calls = calls.iter().map(|c| c.to_string()).collect();+ h+ }++ fn blocked_by(mut h: Hotspot, blocker: &str) -> Hotspot {+ h.blockers = vec![blocker.into()];+ h+ }++ fn by_id<'a>(ranked: &'a [Hotspot], id: &str) -> &'a Hotspot {+ ranked.iter().find(|h| h.id == id).unwrap()+ }++ #[test]+ fn caller_of_a_helper_that_does_io_is_blocked() {+ // build_all spends its time waiting on run_tool's subprocess: porting it would drop the subprocess+ let ranked = rank(+ vec![+ calling(hs("build_all", Some("work"), 9.9, 1.0, false), &["checksum", "run_tool"]),+ blocked_by(hs("run_tool", Some("work"), 9.8, 1.0, false), "does I/O: subprocess.run"),+ hs("checksum", Some("work"), 0.1, 1.0, false),+ ],+ 10.0,+ );+ let outer = by_id(&ranked, "work.build_all");+ assert!(!outer.portable() && !outer.worth_it());+ assert_eq!(outer.blockers, ["calls run_tool, which does I/O: subprocess.run"]);+ assert_eq!(outer.score, 0.0);+ assert!(ranked.iter().all(|h| !h.worth_it()), "nothing here is worth porting");+ }++ #[test]+ fn helper_blockers_pass_up_a_chain_and_through_cycles() {+ let ranked = rank(+ vec![+ calling(hs("top", Some("m"), 10.0, 1.0, false), &["mid"]),+ calling(hs("mid", Some("m"), 9.0, 1.0, false), &["leaf", "top"]),+ blocked_by(hs("leaf", Some("m"), 8.0, 1.0, false), "not deterministic: random.random"),+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.mid").blockers, ["calls leaf, which is not deterministic: random.random"]);+ assert_eq!(+ by_id(&ranked, "m.top").blockers,+ ["calls mid, which calls leaf, which is not deterministic: random.random"]+ );+ }++ #[test]+ fn where_a_helper_lives_does_not_block_its_caller() {+ // a helper is ported inside its caller, so being a method or living in the entry script doesn't matter,+ // and a same-named function in another file isn't the helper+ let ranked = rank(+ vec![+ calling(hs("outer", Some("m"), 10.0, 1.0, false), &["inner", "save"]),+ blocked_by(hs("inner", Some("m"), 9.0, 1.0, false), "nested function"),+ Hotspot {+ file: "other.py".into(),+ ..blocked_by(hs("save", Some("other"), 1.0, 1.0, false), "does I/O: open")+ },+ ],+ 10.0,+ );+ assert!(by_id(&ranked, "m.outer").worth_it());+ }++ #[test]+ fn helper_blockers_pass_through_a_helper_blocked_only_by_where_it_lives() {+ let ranked = rank(+ vec![+ calling(hs("top", Some("m"), 10.0, 1.0, false), &["mid"]),+ calling(blocked_by(hs("mid", Some("m"), 9.0, 1.0, false), "nested function"), &["leaf"]),+ blocked_by(hs("leaf", Some("m"), 8.0, 1.0, false), "does I/O: open"),+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.top").blockers, ["calls mid, which calls leaf, which does I/O: open"]);+ }++ #[test]+ fn a_blocked_caller_no_longer_covers_its_portable_helper() {+ let ranked = rank(+ vec![+ calling(hs("outer", Some("m"), 10.0, 1.0, false), &["crunch", "save"]),+ hs("crunch", Some("m"), 9.5, 1.0, false),+ blocked_by(hs("save", Some("m"), 0.4, 1.0, false), "does I/O: open"),+ ],+ 10.0,+ );+ assert_eq!(ranked[0].id, "m.crunch");+ assert!(ranked[0].worth_it() && ranked[0].covered_by.is_none());+ assert!(!by_id(&ranked, "m.outer").portable());+ }++ #[test]+ fn a_cycle_without_blockers_stays_portable() {+ let ranked = rank(+ vec![+ calling(hs("a", Some("m"), 10.0, 1.0, false), &["b"]),+ calling(hs("b", Some("m"), 9.0, 1.0, false), &["a"]),+ ],+ 10.0,+ );+ assert!(ranked.iter().all(|h| h.portable()));+ }++ #[test]+ fn a_method_of_the_same_name_is_not_the_helper() {+ // `outer` calls the top-level `run`, not `Runner.run`, whichever the profiler listed first+ let function = |h: Hotspot| Hotspot { kind: "function".into(), ..h };+ let method = |h: Hotspot| Hotspot { kind: "method".into(), qualname: "Runner.run".into(), ..h };+ let outer = || function(calling(hs("outer", Some("m"), 10.0, 1.0, false), &["run"]));+ let ranked = rank(+ vec![+ outer(),+ method(blocked_by(hs("run", Some("m"), 5.0, 1.0, false), "does I/O: print")),+ function(hs("run", Some("m"), 0.1, 1.0, false)),+ ],+ 10.0,+ );+ assert!(by_id(&ranked, "m.outer").portable());+ let ranked = rank(+ vec![+ outer(),+ method(hs("run", Some("m"), 5.0, 1.0, false)),+ function(blocked_by(hs("run", Some("m"), 0.1, 1.0, false), "does I/O: subprocess.run")),+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.outer").blockers, ["calls run, which does I/O: subprocess.run"]);+ }++ #[test]+ fn in_python_a_bare_call_from_a_staticmethod_means_the_module_level_function() {+ let static_in_text = |h: Hotspot| Hotspot { kind: "method".into(), owner: Some("Text".into()), ..h };+ let ranked = rank(+ vec![+ static_in_text(calling(hs("clean", Some("m"), 10.0, 1.0, false), &["norm"])),+ static_in_text(hs("norm", Some("m"), 5.0, 1.0, false)),+ Hotspot {+ kind: "function".into(),+ ..blocked_by(hs("norm", Some("m"), 0.1, 1.0, false), "does I/O: open")+ },+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.clean").blockers, ["calls norm, which does I/O: open"]);+ }++ #[test]+ fn php_helpers_are_covered_by_their_caller_whatever_the_case() {+ let php = |h: Hotspot| Hotspot { file: "app.php".into(), ..h };+ let ranked = rank(+ vec![+ php(calling(hs("outer", Some("m"), 10.0, 1.0, false), &["\\CRUNCH"])),+ php(hs("crunch", Some("m"), 9.5, 1.0, false)),+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.crunch").covered_by.as_deref(), Some("m.outer"));+ }++ #[test]+ fn each_backends_wording_passes_on() {+ for (file, blocker) in [+ ("App.java", "writes static fields"),+ ("Program.cs", "writes static fields"),+ ("main.go", "writes package-level variables"),+ ("app.rb", "writes instance, class or global variables"),+ ("app.php", "uses global variables"),+ ("main.c", "keeps static variables"),+ ("app.py", "writes global state"),+ ] {+ let at = |h: Hotspot| Hotspot { file: file.into(), ..h };+ let ranked = rank(+ vec![+ at(calling(hs("outer", Some("m"), 10.0, 1.0, false), &["bump"])),+ at(blocked_by(hs("bump", Some("m"), 9.0, 1.0, false), blocker)),+ ],+ 10.0,+ );+ assert_eq!(by_id(&ranked, "m.outer").blockers, [format!("calls bump, which {blocker}")], "{file}");+ }+ }++ #[test]+ fn signature_and_ruby_super_blockers_do_not_pass_on() {+ for blocker in ["calls super", "returns nothing (void)", "takes vararg"] {+ let ranked = rank(+ vec![+ calling(hs("outer", Some("m"), 10.0, 1.0, false), &["inner"]),+ blocked_by(hs("inner", Some("m"), 9.0, 1.0, false), blocker),+ ],+ 10.0,+ );+ assert!(by_id(&ranked, "m.outer").portable(), "{blocker}");+ }+ }++ #[test]+ fn php_helper_names_ignore_case_and_a_leading_backslash() {+ for called in ["runtool", "\\runTool"] {+ let php = |h: Hotspot| Hotspot { file: "app.php".into(), ..h };+ let ranked = rank(+ vec![+ php(calling(hs("outer", Some("m"), 10.0, 1.0, false), &[called])),+ php(blocked_by(hs("runTool", Some("m"), 9.0, 1.0, false), "does I/O: exec")),+ ],+ 10.0,+ );+ assert!(!by_id(&ranked, "m.outer").portable(), "{called}");+ }+ }}diff --git a/src/pipeline.rs b/src/pipeline.rsindex ceb53ff..76ee0ac 100644--- a/src/pipeline.rs+++ b/src/pipeline.rs@@ -119,7 +119,7 @@ pub fn port(root: &Path, id: Option<&str>, samples: usize) -> Result<PortOut> {let helpers: Vec<Hotspot> = hs.calls.iter()- .filter_map(|name| ctx.state.hotspots.iter().find(|h| h.file == hs.file && &h.name == name))+ .filter_map(|name| hotspot::helpers_named(&ctx.state.hotspots, &hs, name).first().copied()).cloned().collect();let text = fs::read_to_string(ctx.root.join(&hs.file)).unwrap_or_default();