Skip to content

Commit

Permalink
Optimize io-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 23, 2025
1 parent b29dcba commit 23b545c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ fn clean_trybuild_artifacts(ws: &Workspace, pkg_ids: &[PackageId], verbose: bool

fn rm_rf(path: impl AsRef<Path>, verbose: bool) -> Result<()> {
let path = path.as_ref();
let m = fs::symlink_metadata(path);
#[allow(clippy::disallowed_methods)] // std::fs is okay here since we ignore error contents
let m = std::fs::symlink_metadata(path);
if m.as_ref().map(fs::Metadata::is_dir).unwrap_or(false) {
if verbose {
status!("Removing", "{}", path.display());
Expand Down
2 changes: 1 addition & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub(crate) use std::fs::Metadata;
use std::{ffi::OsStr, io, path::Path};

pub(crate) use fs_err::{File, create_dir_all, read_dir, symlink_metadata, write};
pub(crate) use fs_err::{File, create_dir_all, read_dir, write};

/// Removes a file from the filesystem **if exists**. (Similar to `rm -f`)
pub(crate) fn remove_file(path: impl AsRef<Path>) -> io::Result<()> {
Expand Down
25 changes: 15 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
collections::{BTreeSet, HashMap},
ffi::{OsStr, OsString},
fmt::Write as _,
io::{self, BufRead as _, Read as _, Write as _},
io::{self, BufRead as _, BufWriter, Read as _, Write as _},
path::Path,
process::ExitCode,
time::SystemTime,
Expand Down Expand Up @@ -69,16 +69,20 @@ fn try_main() -> Result<()> {

match args.subcommand {
Subcommand::Demangle => {
rustc_demangle::demangle_stream(&mut io::stdin().lock(), &mut io::stdout(), false)?;
let mut stdout = BufWriter::new(io::stdout().lock()); // Buffered because it is written many times.
rustc_demangle::demangle_stream(&mut io::stdin().lock(), &mut stdout, false)?;
stdout.flush()?;
}
Subcommand::Clean => clean::run(&mut args)?,
Subcommand::ShowEnv => {
let cx = &Context::new(args)?;
let stdout = io::stdout();
let writer =
&mut ShowEnvWriter { writer: stdout.lock(), options: cx.args.show_env.clone() };
let writer = &mut ShowEnvWriter {
writer: BufWriter::new(io::stdout().lock()), // Buffered because it is written with newline many times.
options: cx.args.show_env.clone(),
};
set_env(cx, writer, IsNextest(true))?; // Include envs for nextest.
writer.set("CARGO_LLVM_COV_TARGET_DIR", cx.ws.metadata.target_directory.as_str())?;
writer.writer.flush()?;
}
Subcommand::Report { .. } => {
let cx = &Context::new(args)?;
Expand Down Expand Up @@ -613,8 +617,7 @@ fn generate_report(cx: &Context) -> Result<()> {
// Handle --show-missing-lines.
let uncovered_files = json.get_uncovered_lines(ignore_filename_regex.as_deref());
if !uncovered_files.is_empty() {
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut stdout = BufWriter::new(io::stdout().lock()); // Buffered because it is written with newline many times.
writeln!(stdout, "Uncovered Lines:")?;
for (file, lines) in &uncovered_files {
let lines: Vec<_> = lines.iter().map(ToString::to_string).collect();
Expand Down Expand Up @@ -800,7 +803,8 @@ fn object_files(cx: &Context) -> Result<Vec<OsString>> {
}
target_dir.push("target");
let archive_file = cx.args.nextest_archive_file.as_ref().unwrap();
let decoder = ruzstd::decoding::StreamingDecoder::new(fs::File::open(archive_file)?)?;
let file = fs::File::open(archive_file)?; // TODO: Buffering?
let decoder = ruzstd::decoding::StreamingDecoder::new(file)?;
let mut archive = Archive::new(decoder);
let mut binaries_metadata = vec![];
for entry in archive.entries()? {
Expand Down Expand Up @@ -1174,8 +1178,9 @@ impl Format {
let mut cov = serde_json::from_str::<LlvmCovJsonExport>(&out)?;
cov.inject(cx.ws.current_manifest.clone());

let stdout = std::io::stdout().lock();
serde_json::to_writer(stdout, &cov)?;
let mut stdout = BufWriter::new(io::stdout().lock()); // Buffered because it is written many times.
serde_json::to_writer(&mut stdout, &cov)?;
stdout.flush()?;
} else {
cmd.run()?;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const INSTR_PROF_RAW_MAGIC_64: u64 = (255_u64 << 56)
| 129_u64;

fn perturb_header(path: &Path) {
let mut file = fs::OpenOptions::new().read(true).write(true).open(path).unwrap();
let mut file = fs::OpenOptions::new().read(true).write(true).open(path).unwrap(); // Not buffered because it is read and written only once each.
let mut magic = {
let mut buf = [0_u8; mem::size_of::<u64>()];
file.read_exact(&mut buf).unwrap();
Expand Down

0 comments on commit 23b545c

Please sign in to comment.