Skip to content

Commit 563920c

Browse files
committed
rustc_codegen_ssa: Buffer file writes in link_rlib
This makes this step take ~25ms on my machine (M3 Max 64GB) for Zed repo instead of ~150ms. Additionally it takes down the time needed for a clean cargo build of ripgrep from ~6.1s to 5.9s. This change is mostly relevant for crates with multiple large CGUs.
1 parent 3c1e750 commit 563920c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/archive.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::error::Error;
33
use std::ffi::OsString;
44
use std::fs::{self, File};
5-
use std::io::{self, Write};
5+
use std::io::{self, BufWriter, Write};
66
use std::path::{Path, PathBuf};
77

88
use ar_archive_writer::{
@@ -493,7 +493,6 @@ impl<'a> ArArchiveBuilder<'a> {
493493
perms: 0o644,
494494
})
495495
}
496-
497496
// Write to a temporary file first before atomically renaming to the final name.
498497
// This prevents programs (including rustc) from attempting to read a partial archive.
499498
// It also enables writing an archive with the same filename as a dependency on Windows as
@@ -509,17 +508,18 @@ impl<'a> ArArchiveBuilder<'a> {
509508
io_error_context("couldn't create a directory for the temp file", err)
510509
})?;
511510
let archive_tmpfile_path = archive_tmpdir.path().join("tmp.a");
512-
let mut archive_tmpfile = File::create_new(&archive_tmpfile_path)
511+
let archive_tmpfile = File::create_new(&archive_tmpfile_path)
513512
.map_err(|err| io_error_context("couldn't create the temp file", err))?;
514-
513+
let mut archive_tmpfile = BufWriter::new(archive_tmpfile);
515514
write_archive_to_stream(
516515
&mut archive_tmpfile,
517516
&entries,
518517
archive_kind,
519518
false,
520519
/* is_ec = */ self.sess.target.arch == "arm64ec",
521520
)?;
522-
521+
archive_tmpfile.flush()?;
522+
drop(archive_tmpfile);
523523
let any_entries = !entries.is_empty();
524524
drop(entries);
525525
// Drop src_archives to unmap all input archives, which is necessary if we want to write the

0 commit comments

Comments
 (0)