Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(bench): suggest enable --only and pass crate path to diff hints (#1431)

m3lonc5a7990f58c7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 688aa82b..c68f9c31 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- `bench` mixed verdicts now recommend `rust-anything enable <crate> --only <faster>` for languages that `enable` switches (instead of `bridge --keep`, which refuses an existing crate) and pass the crate folder to `rust-anything diff <crate>` hints (#1356 by @m3lonxd).
- PowerShell keeps functions with `[char]`, `[System.Char]`, or character array parameters in
PowerShell. Bridging those parameters as strings produced inputs that failed to bind (#1342).
- Recorded SMTP attachments preserve literal percent escapes in plain pieces of a split filename
@@ -20,6 +21,10 @@
- CONTRIBUTING's "Set up" table has a Kotlin row (`kotlinc` and a JDK 17+), and its C row says
the C tests also need Python 3.9+, which calls the library through ctypes. A test checks that
the table has a row for every language (#1352, #1395 by @iyazici00).
+- `bench` mixed verdicts now recommend `rust-anything enable <crate> --only <faster>` for TS/JS and
+ Python units, the ones `enable` can switch on in part, and tell other languages to keep calling
+ the original for the slower functions, instead of `bridge --keep`, which refuses an existing
+ crate. Its `diff` hints name the crate: `rust-anything diff <crate>` (#1356 by @m3lonxd).
- `obligations --evidence` ends with `1 of 3 obligations has no recorded case` for a single
one. With no obligations at all, it and `app record` say that none were listed and point to
`rust-anything obligations` for the families it could not enumerate, instead of `0 of 0`, which
diff --git a/src/card.rs b/src/card.rs
index f1480bb7..cf5ab92b 100644
--- a/src/card.rs
+++ b/src/card.rs
@@ -144,7 +144,7 @@ pub fn report(opts: &Options) -> Result<Card> {
resolve_from: opts.resolve_from,
node_args: opts.node_args,
})?;
- let verdict = harness::verdict(&bench, manifest.language);
+ let verdict = harness::verdict(&bench, manifest.language, &crate_dir);
let per_function = cases_per_function(&crate_dir);
let comparison = crate::recording::Loosening::read(&crate_dir);
let mut notes = verified.notes.clone();
diff --git a/src/go.rs b/src/go.rs
index c6818b30..0cfd4432 100644
--- a/src/go.rs
+++ b/src/go.rs
@@ -654,7 +654,7 @@ pub fn go(opts: &Options) -> Result<Outcome> {
resolve_from: None,
node_args: &[],
})?;
- let speedup = crate::harness::verdict(&rows, manifest.language).speedup;
+ let speedup = crate::harness::verdict(&rows, manifest.language, &crate_dir).speedup;
say(
&mut steps,
format!(
diff --git a/src/harness.rs b/src/harness.rs
index 25a5a659..38371888 100644
--- a/src/harness.rs
+++ b/src/harness.rs
@@ -1386,7 +1386,7 @@ pub struct Verdict {
/// Faster means at least 1.1x, slower below 0.9x; between is a tie (timing noise). Functions
/// whose cases do not all match are left out: their time is the cost of failing (a panic caught
/// at the bridge), not of the work.
-pub fn verdict(rows: &[BenchRow], lang: crate::lang::Lang) -> Verdict {
+pub fn verdict(rows: &[BenchRow], lang: crate::lang::Lang, crate_dir: &std::path::Path) -> Verdict {
let left_out: Vec<String> = rows
.iter()
.filter(|r| r.failing)
@@ -1408,9 +1408,17 @@ pub fn verdict(rows: &[BenchRow], lang: crate::lang::Lang) -> Verdict {
} else {
(rows.iter().map(|r| r.speedup.max(1e-9).ln()).sum::<f64>() / rows.len() as f64).exp()
};
+ let q = crate::app::cli_path(crate_dir);
+ let can_enable = matches!(lang, crate::lang::Lang::Ts | crate::lang::Lang::Python);
+ let only_flags = faster
+ .iter()
+ .map(|name| format!("--only {name}"))
+ .collect::<Vec<_>>()
+ .join(" ");
let advice = if rows.is_empty() && !left_out.is_empty() {
- "Nothing to compare: no function matches all its recorded cases yet. Run `diff`."
- .to_string()
+ format!(
+ "Nothing to compare: no function matches all its recorded cases yet. Run `rust-anything diff {q}`."
+ )
} else if rows.is_empty() {
"Nothing to compare: record cases that do not throw first.".to_string()
} else if slower.is_empty() && faster.len() == rows.len() {
@@ -1431,15 +1439,29 @@ pub fn verdict(rows: &[BenchRow], lang: crate::lang::Lang) -> Verdict {
slower.join(", ")
)
} else if speedup >= 1.1 && slower.len() == 1 {
- format!(
- "Ship the faster functions. Keep the slower one in the original (`bridge --keep`) or move its callers to Rust too: {}.",
- slower.join(", ")
- )
+ if can_enable {
+ format!(
+ "Ship the faster functions. Keep the slower one in the original (`rust-anything enable {q} {only_flags}`) or move its callers to Rust too: {}.",
+ slower.join(", ")
+ )
+ } else {
+ format!(
+ "Ship the faster functions. Keep calling the original for the slower one or move its callers to Rust too: {}.",
+ slower.join(", ")
+ )
+ }
} else if speedup >= 1.1 {
- format!(
- "Ship the faster functions. Keep the slower ones in the original (`bridge --keep`) or move their callers to Rust too: {}.",
- slower.join(", ")
- )
+ if can_enable {
+ format!(
+ "Ship the faster functions. Keep the slower ones in the original (`rust-anything enable {q} {only_flags}`) or move their callers to Rust too: {}.",
+ slower.join(", ")
+ )
+ } else {
+ format!(
+ "Ship the faster functions. Keep calling the original for the slower ones or move their callers to Rust too: {}.",
+ slower.join(", ")
+ )
+ }
} else if rows.len() == 1 {
"Do not ship this unit for speed: calling across costs more than the work this function does. \
Port a bigger unit (this function together with its callers), so one call does more work."
@@ -5128,6 +5150,7 @@ mod verdict_tests {
failing: true,
..row(name, speedup)
};
+ let cd = std::path::Path::new("rust/unit-rs");
let v = verdict(
&[
failing("rangeOf", 0.0012),
@@ -5135,15 +5158,18 @@ mod verdict_tests {
row("count", 0.11),
],
crate::lang::Lang::Ts,
+ cd,
);
assert_eq!(v.left_out, ["rangeOf", "words"]);
assert!((v.speedup - 0.11).abs() < 1e-9, "{v:?}");
assert_eq!(v.slower, ["count"]);
- let none = verdict(&[failing("words", 0.0058)], crate::lang::Lang::Ts);
+ let none = verdict(&[failing("words", 0.0058)], crate::lang::Lang::Ts, cd);
assert!(
none.advice
- .starts_with("Nothing to compare: no function matches")
+ .contains("Run `rust-anything diff rust/unit-rs`"),
+ "{}",
+ none.advice
);
assert!((none.speedup - 1.0).abs() < 1e-9);
}
@@ -5151,29 +5177,65 @@ mod verdict_tests {
#[test]
fn advice_follows_the_numbers() {
let ts = crate::lang::Lang::Ts;
+ let cd = std::path::Path::new("rust/unit-rs");
assert!(
- verdict(&[row("a", 3.0), row("b", 2.0)], ts)
+ verdict(&[row("a", 3.0), row("b", 2.0)], ts, cd)
.advice
.starts_with("Ship it: every")
);
assert!(
- verdict(&[row("a", 3.0), row("b", 1.0)], ts)
+ verdict(&[row("a", 3.0), row("b", 1.0)], ts, cd)
.advice
.starts_with("Ship it: no function")
);
- let mixed = verdict(&[row("a", 4.0), row("b", 0.5)], ts);
- assert!(mixed.advice.contains("Keep the slower one") && mixed.advice.ends_with("b."));
- let mixed_many = verdict(&[row("a", 10.0), row("b", 0.5), row("c", 0.5)], ts);
+ let mixed = verdict(&[row("a", 4.0), row("b", 0.5)], ts, cd);
+ assert!(
+ mixed.advice.contains("Keep the slower one")
+ && mixed
+ .advice
+ .contains("`rust-anything enable rust/unit-rs --only a`")
+ && !mixed.advice.contains("bridge --keep")
+ && mixed.advice.ends_with("b."),
+ "{}",
+ mixed.advice
+ );
+ let mixed_many = verdict(&[row("a", 10.0), row("b", 0.5), row("c", 0.5)], ts, cd);
assert!(
- mixed_many.advice.contains("Keep the slower ones") && mixed_many.advice.ends_with("c.")
+ mixed_many.advice.contains("Keep the slower ones")
+ && mixed_many
+ .advice
+ .contains("`rust-anything enable rust/unit-rs --only a`")
+ && mixed_many.advice.ends_with("c."),
+ "{}",
+ mixed_many.advice
+ );
+ let ps_mixed = verdict(
+ &[row("a", 4.0), row("b", 0.5)],
+ crate::lang::Lang::Powershell,
+ cd,
);
assert!(
- verdict(&[row("a", 0.2), row("b", 0.3)], ts)
+ ps_mixed.advice.contains("Keep calling the original")
+ && !ps_mixed.advice.contains("enable"),
+ "{}",
+ ps_mixed.advice
+ );
+ let go_mixed = verdict(&[row("a", 4.0), row("b", 0.5)], crate::lang::Lang::Go, cd);
+ assert!(
+ go_mixed.advice.contains("Keep calling the original")
+ && !go_mixed.advice.contains("enable")
+ && !go_mixed.advice.contains("bridge --keep")
+ && go_mixed.advice.ends_with("b."),
+ "{}",
+ go_mixed.advice
+ );
+ assert!(
+ verdict(&[row("a", 0.2), row("b", 0.3)], ts, cd)
.advice
.starts_with("Do not ship")
);
assert!(
- verdict(&[row("a", 0.5)], crate::lang::Lang::C)
+ verdict(&[row("a", 0.5)], crate::lang::Lang::C, cd)
.advice
.starts_with("No call overhead")
);
@@ -5182,13 +5244,18 @@ mod verdict_tests {
#[test]
fn advice_singular_for_one_slower_or_tied_function() {
let ts = crate::lang::Lang::Ts;
+ let cd = std::path::Path::new("rust/unit-rs");
// One slower function in C: names it, not "these"/"them".
- let c_one = verdict(&[row("add", 0.5)], crate::lang::Lang::C);
+ let c_one = verdict(&[row("add", 0.5)], crate::lang::Lang::C, cd);
assert_eq!(
c_one.advice,
"No call overhead in C: add is slower as Rust code. Profile and optimize it, or keep it in C."
);
- let c_many = verdict(&[row("add", 0.5), row("sub", 0.5)], crate::lang::Lang::C);
+ let c_many = verdict(
+ &[row("add", 0.5), row("sub", 0.5)],
+ crate::lang::Lang::C,
+ cd,
+ );
assert!(
c_many.advice.starts_with(
"No call overhead in C: these are slower as Rust code. Profile and optimize them"
@@ -5198,7 +5265,7 @@ mod verdict_tests {
);
// One slower function mixed with a faster one: "the slower one" and "its callers".
- let mixed_one = verdict(&[row("a", 4.0), row("b", 0.5)], ts);
+ let mixed_one = verdict(&[row("a", 4.0), row("b", 0.5)], ts, cd);
assert!(
mixed_one.advice.contains("the slower one") && mixed_one.advice.contains("its callers"),
"{}",
@@ -5206,12 +5273,12 @@ mod verdict_tests {
);
// A single function that only ties (no slower, not all faster) does not say "some".
- let tie_one = verdict(&[row("twice", 1.0)], ts);
+ let tie_one = verdict(&[row("twice", 1.0)], ts, cd);
assert_eq!(
tie_one.advice,
"Ship it: the function is not slower in Rust; it ties."
);
- let tie_many = verdict(&[row("a", 3.0), row("b", 1.0)], ts);
+ let tie_many = verdict(&[row("a", 3.0), row("b", 1.0)], ts, cd);
assert_eq!(
tie_many.advice,
"Ship it: no function is slower in Rust; some only tie."
@@ -5220,7 +5287,11 @@ mod verdict_tests {
#[test]
fn advice_for_one_function_says_this_function() {
- let v = verdict(&[row("twice", 0.38)], crate::lang::Lang::Ts);
+ let v = verdict(
+ &[row("twice", 0.38)],
+ crate::lang::Lang::Ts,
+ std::path::Path::new("rust/unit-rs"),
+ );
assert!(
v.advice.contains("this function together with its callers"),
"{}",
diff --git a/src/main.rs b/src/main.rs
index 92d9fca8..9432136e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1124,7 +1124,11 @@ fn say_replay(replay: &app::ReplayOutcome) {
}
/// Summary lines printed below the `bench` table.
-fn bench_summary_lines(rows: &[harness::BenchRow], v: &harness::Verdict) -> Vec<String> {
+fn bench_summary_lines(
+ rows: &[harness::BenchRow],
+ v: &harness::Verdict,
+ crate_dir: &std::path::Path,
+) -> Vec<String> {
let compared = rows.len().saturating_sub(v.left_out.len());
let mut lines = vec![String::new()];
if compared > 0 {
@@ -1138,8 +1142,9 @@ fn bench_summary_lines(rows: &[harness::BenchRow], v: &harness::Verdict) -> Vec<
}
if !v.left_out.is_empty() {
lines.push(format!(
- "Left out of the overall: {} (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `diff`).",
- v.left_out.join(", ")
+ "Left out of the overall: {} (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `rust-anything diff {}`).",
+ v.left_out.join(", "),
+ rust_anything::app::cli_path(crate_dir)
));
}
lines.push(v.advice.clone());
@@ -1958,8 +1963,8 @@ fn run() -> Result<ExitCode> {
let lang = bridge::Manifest::load(&crate_dir)
.map(|m| m.language)
.unwrap_or_default();
- let v = harness::verdict(&rows, lang);
- for line in bench_summary_lines(&rows, &v) {
+ let v = harness::verdict(&rows, lang, &crate_dir);
+ for line in bench_summary_lines(&rows, &v, &crate_dir) {
say!("{line}");
}
}
@@ -3203,8 +3208,9 @@ mod tests {
bench_row("words", 0.02, true),
bench_row("count", 0.03, true),
];
- let v_all = verdict(&all_failing, Lang::Ts);
- let lines_all = bench_summary_lines(&all_failing, &v_all);
+ let cd = Path::new("rust/text-rs");
+ let v_all = verdict(&all_failing, Lang::Ts, cd);
+ let lines_all = bench_summary_lines(&all_failing, &v_all, cd);
let joined_all = lines_all.join("\n");
assert!(!joined_all.contains("Overall"), "{joined_all}");
assert!(!joined_all.contains("0 of 0"), "{joined_all}");
@@ -3212,14 +3218,14 @@ mod tests {
lines_all,
vec![
String::new(),
- "Left out of the overall: rangeOf, words, count (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `diff`).".to_string(),
- "Nothing to compare: no function matches all its recorded cases yet. Run `diff`.".to_string(),
+ "Left out of the overall: rangeOf, words, count (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `rust-anything diff rust/text-rs`).".to_string(),
+ "Nothing to compare: no function matches all its recorded cases yet. Run `rust-anything diff rust/text-rs`.".to_string(),
]
);
let empty: Vec<BenchRow> = Vec::new();
- let v_empty = verdict(&empty, Lang::Ts);
- let lines_empty = bench_summary_lines(&empty, &v_empty);
+ let v_empty = verdict(&empty, Lang::Ts, cd);
+ let lines_empty = bench_summary_lines(&empty, &v_empty, cd);
assert_eq!(
lines_empty,
vec![
@@ -3229,9 +3235,9 @@ mod tests {
);
let one = vec![bench_row("double", 2.0, false)];
- let v_one = verdict(&one, Lang::Ts);
+ let v_one = verdict(&one, Lang::Ts, cd);
assert_eq!(
- bench_summary_lines(&one, &v_one),
+ bench_summary_lines(&one, &v_one, cd),
vec![
String::new(),
"Timed as callers call it, crossing included. Overall 2.0x faster (geometric mean); 1 of 1 function at least 1.1x faster.".to_string(),
@@ -3244,16 +3250,20 @@ mod tests {
bench_row("triple", 0.5, false),
bench_row("broken", 0.01, true),
];
- let v_mixed = verdict(&mixed, Lang::Ts);
+ let v_mixed = verdict(&mixed, Lang::Python, cd);
assert_eq!(
- bench_summary_lines(&mixed, &v_mixed),
+ bench_summary_lines(&mixed, &v_mixed, cd),
vec![
String::new(),
"Timed as callers call it, crossing included. Overall 1.4x faster (geometric mean); 1 of 2 functions at least 1.1x faster.".to_string(),
- "Left out of the overall: broken (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `diff`).".to_string(),
- "Ship the faster functions. Keep the slower one in the original (`bridge --keep`) or move its callers to Rust too: triple.".to_string(),
+ "Left out of the overall: broken (their recorded cases do not all match, so their time is the cost of failing, not of the work; run `rust-anything diff rust/text-rs`).".to_string(),
+ "Ship the faster functions. Keep the slower one in the original (`rust-anything enable rust/text-rs --only double`) or move its callers to Rust too: triple.".to_string(),
]
);
+
+ let v_go = verdict(&mixed, Lang::Go, cd);
+ assert!(!v_go.advice.contains("bridge --keep"));
+ assert!(!v_go.advice.contains("enable"));
}
#[test]
diff --git a/src/mcp.rs b/src/mcp.rs
index a7e1af9f..7281263d 100644
--- a/src/mcp.rs
+++ b/src/mcp.rs
@@ -1652,7 +1652,7 @@ fn call(name: &str, args: &Value) -> Result<Value> {
let lang = bridge::Manifest::load(&crate_dir)
.map(|m| m.language)
.unwrap_or_default();
- let verdict = harness::verdict(&rows, lang);
+ let verdict = harness::verdict(&rows, lang, &crate_dir);
serde_json::json!({ "rows": rows, "verdict": verdict })
}
"fuzz" => {
diff --git a/tests/loop.rs b/tests/loop.rs
index 02cb220c..98c15246 100644
--- a/tests/loop.rs
+++ b/tests/loop.rs
@@ -945,7 +945,7 @@ fn benchmark_marks_functions_whose_cases_do_not_match() {
let rows = result.unwrap();
let failing: Vec<(&str, bool)> = rows.iter().map(|r| (r.name.as_str(), r.failing)).collect();
assert_eq!(failing, [("double", false), ("triple", true)], "{rows:?}");
- let verdict = harness::verdict(&rows, rust_anything::lang::Lang::Ts);
+ let verdict = harness::verdict(&rows, rust_anything::lang::Lang::Ts, &dir);
assert_eq!(verdict.left_out, ["triple"]);
assert!(
(verdict.speedup - rows[0].speedup).abs() < 1e-9,