From 563920ce1410dd27104dcfe861398a43da0e319c Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Sun, 29 Dec 2024 01:17:18 +0100 Subject: [PATCH 1/2] 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. --- compiler/rustc_codegen_ssa/src/back/archive.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index d9eece1d8dc9b..dc8774ff477d5 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -2,7 +2,7 @@ use std::env; use std::error::Error; use std::ffi::OsString; use std::fs::{self, File}; -use std::io::{self, Write}; +use std::io::{self, BufWriter, Write}; use std::path::{Path, PathBuf}; use ar_archive_writer::{ @@ -493,7 +493,6 @@ impl<'a> ArArchiveBuilder<'a> { perms: 0o644, }) } - // Write to a temporary file first before atomically renaming to the final name. // This prevents programs (including rustc) from attempting to read a partial archive. // It also enables writing an archive with the same filename as a dependency on Windows as @@ -509,9 +508,9 @@ impl<'a> ArArchiveBuilder<'a> { io_error_context("couldn't create a directory for the temp file", err) })?; let archive_tmpfile_path = archive_tmpdir.path().join("tmp.a"); - let mut archive_tmpfile = File::create_new(&archive_tmpfile_path) + let archive_tmpfile = File::create_new(&archive_tmpfile_path) .map_err(|err| io_error_context("couldn't create the temp file", err))?; - + let mut archive_tmpfile = BufWriter::new(archive_tmpfile); write_archive_to_stream( &mut archive_tmpfile, &entries, @@ -519,7 +518,8 @@ impl<'a> ArArchiveBuilder<'a> { false, /* is_ec = */ self.sess.target.arch == "arm64ec", )?; - + archive_tmpfile.flush()?; + drop(archive_tmpfile); let any_entries = !entries.is_empty(); drop(entries); // Drop src_archives to unmap all input archives, which is necessary if we want to write the From 586a805d84630e068b77774f296f14cb29f69aff Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Sun, 29 Dec 2024 21:27:19 +0100 Subject: [PATCH 2/2] review fixes: Adjust whitespace --- compiler/rustc_codegen_ssa/src/back/archive.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index dc8774ff477d5..58eb137c06879 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -493,6 +493,7 @@ impl<'a> ArArchiveBuilder<'a> { perms: 0o644, }) } + // Write to a temporary file first before atomically renaming to the final name. // This prevents programs (including rustc) from attempting to read a partial archive. // It also enables writing an archive with the same filename as a dependency on Windows as @@ -510,6 +511,7 @@ impl<'a> ArArchiveBuilder<'a> { let archive_tmpfile_path = archive_tmpdir.path().join("tmp.a"); let archive_tmpfile = File::create_new(&archive_tmpfile_path) .map_err(|err| io_error_context("couldn't create the temp file", err))?; + let mut archive_tmpfile = BufWriter::new(archive_tmpfile); write_archive_to_stream( &mut archive_tmpfile, @@ -520,6 +522,7 @@ impl<'a> ArArchiveBuilder<'a> { )?; archive_tmpfile.flush()?; drop(archive_tmpfile); + let any_entries = !entries.is_empty(); drop(entries); // Drop src_archives to unmap all input archives, which is necessary if we want to write the