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
Java: where the app can't be asked to close, wait for the flight recording before stopping it
Mohit Mishrac4d5e7104355
diff --git a/src/java.rs b/src/java.rsindex 0ac512f..514a55b 100644--- a/src/java.rs+++ b/src/java.rs@@ -255,6 +255,29 @@ pub fn profile(ctx: &Ctx, program: &str, args: &[String], duration: Option<f64>)profile_with(ctx, program, args, duration, |java| describe(ctx, java, &sources(&ctx.root, program, args)?))}+/// Every flight recording in `dir` is written out: there is one, none is empty, and none is still open for+/// writing (Windows only lets a file be opened exclusively once its writer has closed it).+fn recordings_written(dir: &Path) -> bool {+ let files: Vec<PathBuf> = match fs::read_dir(dir) {+ Ok(entries) => {+ entries.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "jfr")).collect()+ }+ Err(_) => return false,+ };+ !files.is_empty() && files.iter().all(|f| fs::metadata(f).is_ok_and(|m| m.len() > 0) && closed(f))+}++#[cfg(windows)]+fn closed(f: &Path) -> bool {+ use std::os::windows::fs::OpenOptionsExt;+ fs::OpenOptions::new().read(true).share_mode(0).open(f).is_ok()+}++#[cfg(not(windows))]+fn closed(_: &Path) -> bool {+ true+}+/// Profile the app with Flight Recorder and rank the methods `describe` finds in its sources/// (given the JDK that runs the build). `duration`: stop the app after that many seconds.pub(crate) fn profile_with(@@ -286,11 +309,13 @@ pub(crate) fn profile_with(}let started = Instant::now();// asked to close (SIGTERM) at the end, the JVM runs its shutdown hooks and writes the recording- let (run, stopped) = util::output_for(+ let (run, stopped) = util::output_stopping(cmd.current_dir(&ctx.root).env(DISABLE_ENV, "1").stdin(Stdio::null()),program,duration.map(|d| Duration::from_secs_f64(d.ceil() + 2.0)),Duration::from_secs(15),+ "TERM",+ || recordings_written(&jfr_dir),)?;let wall = started.elapsed().as_secs_f64();ensure!(stopped || run.status.success(), "the app failed ({}):\n{}", run.status, tail(&combined(&run), 30));diff --git a/src/util.rs b/src/util.rsindex 329b6d7..af2c925 100644--- a/src/util.rs+++ b/src/util.rs@@ -58,6 +58,19 @@ pub fn output_signalling(limit: Option<Duration>,grace: Duration,signal: &str,+) -> Result<(Output, bool)> {+ output_stopping(cmd, what, limit, grace, signal, || true)+}++/// output_signalling, for an app that saves its own results on time: where it can't be asked to close (a+/// console app on Windows), it's left to run, at most `grace` longer, until `saved` says they're written.+pub fn output_stopping(+ cmd: &mut Command,+ what: &str,+ limit: Option<Duration>,+ grace: Duration,+ signal: &str,+ saved: impl Fn() -> bool,) -> Result<(Output, bool)> {let Some(limit) = limit else { return Ok((output(cmd, what)?, false)) };let mut child = cmd@@ -76,16 +89,14 @@ pub fn output_signalling(}let (stdout, stderr) = (drain(child.stdout.take()), drain(child.stderr.take()));let started = Instant::now();- let mut asked: Option<Instant> = None;+ let mut asked: Option<(Instant, bool)> = None;let status = loop {if let Some(status) = child.try_wait()? {break status;}match asked {- None if started.elapsed() >= limit => {- asked = Some(if ask_to_close(child.id(), signal) { Instant::now() } else { Instant::now() - grace });- }- Some(at) if at.elapsed() >= grace => {+ None if started.elapsed() >= limit => asked = Some((Instant::now(), ask_to_close(child.id(), signal))),+ Some((at, closing)) if at.elapsed() >= grace || (!closing && saved()) => {let _ = child.kill();break child.wait()?;}