Skip to content

Commit 8be7e34

Browse files
committed
Avoid Command::output if it's just going to be written to a file anyway
1 parent 97aa63a commit 8be7e34

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

collector/src/compile/execute/bencher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ impl<'a> BenchProcessor<'a> {
6262
assert!(has_perf);
6363
} else {
6464
let has_xperf = Command::new(env::var("XPERF").unwrap_or("xperf.exe".to_string()))
65-
.output()
65+
.status()
6666
.is_ok();
6767
assert!(has_xperf);
6868

6969
let has_tracelog =
7070
Command::new(env::var("TRACELOG").unwrap_or("tracelog.exe".to_string()))
71-
.output()
71+
.status()
7272
.is_ok();
7373
assert!(has_tracelog);
7474
}

collector/src/compile/execute/profiler.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::utils;
33
use crate::utils::cachegrind::cachegrind_annotate;
44
use anyhow::Context;
55
use std::collections::HashMap;
6+
use std::fs::File;
67
use std::future::Future;
78
use std::io::BufRead;
89
use std::io::Write;
@@ -143,10 +144,8 @@ impl<'a> Processor for ProfileProcessor<'a> {
143144
// Run `summarize`.
144145
let mut summarize_cmd = Command::new("summarize");
145146
summarize_cmd.arg("summarize").arg(&zsp_files_prefix);
146-
fs::write(
147-
summarize_file,
148-
summarize_cmd.output().context("summarize")?.stdout,
149-
)?;
147+
summarize_cmd.stdout(File::create(summarize_file)?);
148+
summarize_cmd.status().context("summarize")?;
150149

151150
// Run `flamegraph`.
152151
let mut flamegraph_cmd = Command::new("flamegraph");
@@ -198,17 +197,19 @@ impl<'a> Processor for ProfileProcessor<'a> {
198197
.arg("--debug-info")
199198
.arg("--threshold")
200199
.arg("0.5")
201-
.arg(&session_dir_arg);
202-
fs::write(oprep_file, op_report_cmd.output()?.stdout)?;
200+
.arg(&session_dir_arg)
201+
.stdout(File::create(oprep_file)?);
202+
op_report_cmd.status()?;
203203

204204
let mut op_annotate_cmd = Command::new("opannotate");
205205
// Other possibly useful args: --assembly
206206
op_annotate_cmd
207207
.arg("--source")
208208
.arg("--threshold")
209209
.arg("0.5")
210-
.arg(&session_dir_arg);
211-
fs::write(opann_file, op_annotate_cmd.output()?.stdout)?;
210+
.arg(&session_dir_arg)
211+
.stdout(File::create(opann_file)?);
212+
op_annotate_cmd.status()?;
212213
}
213214

214215
// Samply produces (via rustc-fake) a data file called
@@ -248,8 +249,9 @@ impl<'a> Processor for ProfileProcessor<'a> {
248249
clg_annotate_cmd
249250
.arg("--auto=yes")
250251
.arg("--show-percs=yes")
251-
.arg(&clgout_file);
252-
fs::write(clgann_file, clg_annotate_cmd.output()?.stdout)?;
252+
.arg(&clgout_file)
253+
.stdout(File::create(clgann_file)?);
254+
clg_annotate_cmd.status()?;
253255
}
254256

255257
// DHAT produces (via rustc-fake) a data file called `dhout`. We

collector/src/toolchain.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ pub fn get_local_toolchain(
372372
// specified. This follows the similar pattern used by rustup's binaries
373373
// (e.g., `rustc +stage1`).
374374
let (rustc, id) = if let Some(toolchain) = rustc.strip_prefix('+') {
375-
let output = Command::new("rustup")
375+
let status = Command::new("rustup")
376376
.args(["which", "rustc", "--toolchain", toolchain])
377-
.output()
377+
.status()
378378
.context("failed to run `rustup which rustc`")?;
379379

380380
// Looks like a commit hash? Try to install it...
381-
if !output.status.success() && toolchain.len() == 40 {
381+
if !status.success() && toolchain.len() == 40 {
382382
// No such toolchain exists, so let's try to install it with
383383
// rustup-toolchain-install-master.
384384

@@ -418,9 +418,10 @@ pub fn get_local_toolchain(
418418
.output()
419419
.context("failed to run `rustup which rustc`")?;
420420

421-
if !output.status.success() {
422-
anyhow::bail!("did not manage to obtain toolchain {}", toolchain);
423-
}
421+
anyhow::ensure!(
422+
output.status.success(),
423+
"did not manage to obtain toolchain {toolchain}"
424+
);
424425

425426
let s = String::from_utf8(output.stdout)
426427
.context("failed to convert `rustup which rustc` output to utf8")?;

collector/src/utils/cachegrind.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::is_installed;
22
use crate::utils::mangling::demangle_file;
33
use anyhow::Context;
4+
use std::fs::File;
45
use std::io::{BufRead, Write};
56
use std::path::Path;
67
use std::process::{Command, Stdio};
@@ -61,8 +62,9 @@ pub fn cachegrind_annotate(
6162
cg_annotate_cmd
6263
.arg("--auto=yes")
6364
.arg("--show-percs=yes")
64-
.arg(cgout_output);
65-
fs::write(cgann_output, cg_annotate_cmd.output()?.stdout)?;
65+
.arg(cgout_output)
66+
.stdout(File::create(cgann_output)?);
67+
cg_annotate_cmd.status()?;
6668
Ok(())
6769
}
6870

@@ -81,38 +83,32 @@ fn run_cg_diff(cgout1: &Path, cgout2: &Path, path: &Path) -> anyhow::Result<()>
8183
if !is_installed("cg_diff") {
8284
anyhow::bail!("`cg_diff` not installed.");
8385
}
84-
let output = Command::new("cg_diff")
86+
let status = Command::new("cg_diff")
8587
.arg(r"--mod-filename=s/\/rustc\/[^\/]*\///")
8688
.arg("--mod-funcname=s/[.]llvm[.].*//")
8789
.arg(cgout1)
8890
.arg(cgout2)
8991
.stderr(Stdio::inherit())
90-
.output()
92+
.stdout(File::create(path)?)
93+
.status()
9194
.context("failed to run `cg_diff`")?;
9295

93-
if !output.status.success() {
94-
anyhow::bail!("failed to generate cachegrind diff");
95-
}
96-
97-
fs::write(path, output.stdout).context("failed to write `cg_diff` output")?;
96+
anyhow::ensure!(status.success(), "failed to generate cachegrind diff");
9897

9998
Ok(())
10099
}
101100

102101
/// Postprocess Cachegrind output file and writes the result to `path`.
103102
fn annotate_diff(cgout: &Path, path: &Path) -> anyhow::Result<()> {
104-
let output = Command::new("cg_annotate")
103+
let status = Command::new("cg_annotate")
105104
.arg("--show-percs=no")
106105
.arg(cgout)
107106
.stderr(Stdio::inherit())
108-
.output()
107+
.stdout(File::create(path)?)
108+
.status()
109109
.context("failed to run `cg_annotate`")?;
110110

111-
if !output.status.success() {
112-
anyhow::bail!("failed to annotate cachegrind output");
113-
}
114-
115-
fs::write(path, output.stdout).context("failed to write `cg_annotate` output")?;
111+
anyhow::ensure!(status.success(), "failed to annotate cachegrind output");
116112

117113
Ok(())
118114
}

collector/src/utils/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::future::Future;
2-
use std::process::Command;
2+
use std::process::{Command, Stdio};
33

44
pub mod cachegrind;
55
pub mod fs;
@@ -17,5 +17,9 @@ pub fn wait_for_future<F: Future<Output = R>, R>(f: F) -> R {
1717

1818
/// Checks if the given binary can be executed.
1919
pub fn is_installed(name: &str) -> bool {
20-
Command::new(name).output().is_ok()
20+
Command::new(name)
21+
.stdout(Stdio::null())
22+
.stderr(Stdio::null())
23+
.status()
24+
.is_ok()
2125
}

0 commit comments

Comments
 (0)