Skip to content

Commit a9cc22f

Browse files
committed
Avoid Command::output if it's just going to be written to a file anyway
1 parent 95c68de commit a9cc22f

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)