From ee129b12ede79fd8cf7e398cf955941b62e58974 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 24 Sep 2024 13:32:29 -0700 Subject: [PATCH 1/4] Add `File::open_buffered` and `create_buffered` --- library/std/src/fs.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 55f3b628ce8e9..6d1005272c603 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -375,6 +375,41 @@ impl File { OpenOptions::new().read(true).open(path.as_ref()) } + /// Attempts to open a file in read-only mode with buffering. + /// + /// See the [`OpenOptions::open`] method, the [`BufReader`][io::BufReader] type, + /// and the [`BufRead`][io::BufRead] trait for more details. + /// + /// If you only need to read the entire file contents, + /// consider [`std::fs::read()`][self::read] or + /// [`std::fs::read_to_string()`][self::read_to_string] instead. + /// + /// # Errors + /// + /// This function will return an error if `path` does not already exist. + /// Other errors may also be returned according to [`OpenOptions::open`]. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(file_buffered)] + /// use std::fs::File; + /// use std::io::BufRead; + /// + /// fn main() -> std::io::Result<()> { + /// let mut f = File::open_buffered("foo.txt")?; + /// assert!(f.capacity() > 0); + /// for (line, i) in f.lines().zip(1..) { + /// println!("{i:6}: {}", line?); + /// } + /// Ok(()) + /// } + /// ``` + #[unstable(feature = "file_buffered", issue = "none")] + pub fn open_buffered>(path: P) -> io::Result> { + File::open(path).map(io::BufReader::new) + } + /// Opens a file in write-only mode. /// /// This function will create a file if it does not exist, @@ -404,6 +439,42 @@ impl File { OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref()) } + /// Opens a file in write-only mode with buffering. + /// + /// This function will create a file if it does not exist, + /// and will truncate it if it does. + /// + /// Depending on the platform, this function may fail if the + /// full directory path does not exist. + /// + /// See the [`OpenOptions::open`] method and the + /// [`BufWriter`][io::BufWriter] type for more details. + /// + /// See also [`std::fs::write()`][self::write] for a simple function to + /// create a file with some given data. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(file_buffered)] + /// use std::fs::File; + /// use std::io::Write; + /// + /// fn main() -> std::io::Result<()> { + /// let mut f = File::create_buffered("foo.txt")?; + /// assert!(f.capacity() > 0); + /// for i in 0..100 { + /// writeln!(&mut f, "{i}")?; + /// } + /// f.flush()?; + /// Ok(()) + /// } + /// ``` + #[unstable(feature = "file_buffered", issue = "none")] + pub fn create_buffered>(path: P) -> io::Result> { + File::create(path).map(io::BufWriter::new) + } + /// Creates a new file in read-write mode; error if the file exists. /// /// This function will create a file if it does not exist, or return an error if it does. This From 1e9a50dde8fe417348a9e4c74787975527502ec3 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 24 Sep 2024 13:33:31 -0700 Subject: [PATCH 2/4] Pre-allocate buffers in `File::open_buffered` and `create_buffered` --- library/std/src/fs.rs | 10 ++++++++-- library/std/src/io/buffered/bufreader.rs | 8 ++++++++ library/std/src/io/buffered/bufreader/buffer.rs | 12 +++++++++++- library/std/src/io/buffered/bufwriter.rs | 10 ++++++++++ library/std/src/lib.rs | 1 + 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 6d1005272c603..b8e3f316bebd9 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -407,7 +407,10 @@ impl File { /// ``` #[unstable(feature = "file_buffered", issue = "none")] pub fn open_buffered>(path: P) -> io::Result> { - File::open(path).map(io::BufReader::new) + // Allocate the buffer *first* so we don't affect the filesystem otherwise. + let buffer = io::BufReader::::try_new_buffer()?; + let file = File::open(path)?; + Ok(io::BufReader::with_buffer(file, buffer)) } /// Opens a file in write-only mode. @@ -472,7 +475,10 @@ impl File { /// ``` #[unstable(feature = "file_buffered", issue = "none")] pub fn create_buffered>(path: P) -> io::Result> { - File::create(path).map(io::BufWriter::new) + // Allocate the buffer *first* so we don't affect the filesystem otherwise. + let buffer = io::BufWriter::::try_new_buffer()?; + let file = File::create(path)?; + Ok(io::BufWriter::with_buffer(file, buffer)) } /// Creates a new file in read-write mode; error if the file exists. diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index e51dde994de45..fcb3e36027bab 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -74,6 +74,14 @@ impl BufReader { BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) } + pub(crate) fn try_new_buffer() -> io::Result { + Buffer::try_with_capacity(DEFAULT_BUF_SIZE) + } + + pub(crate) fn with_buffer(inner: R, buf: Buffer) -> Self { + Self { inner, buf } + } + /// Creates a new `BufReader` with the specified buffer capacity. /// /// # Examples diff --git a/library/std/src/io/buffered/bufreader/buffer.rs b/library/std/src/io/buffered/bufreader/buffer.rs index 1bf84d8bef312..3df7e3971dac4 100644 --- a/library/std/src/io/buffered/bufreader/buffer.rs +++ b/library/std/src/io/buffered/bufreader/buffer.rs @@ -10,7 +10,7 @@ //! without encountering any runtime bounds checks. use crate::cmp; -use crate::io::{self, BorrowedBuf, Read}; +use crate::io::{self, BorrowedBuf, ErrorKind, Read}; use crate::mem::MaybeUninit; pub struct Buffer { @@ -36,6 +36,16 @@ impl Buffer { Self { buf, pos: 0, filled: 0, initialized: 0 } } + #[inline] + pub fn try_with_capacity(capacity: usize) -> io::Result { + match Box::try_new_uninit_slice(capacity) { + Ok(buf) => Ok(Self { buf, pos: 0, filled: 0, initialized: 0 }), + Err(_) => { + Err(io::const_io_error!(ErrorKind::OutOfMemory, "failed to allocate read buffer")) + } + } + } + #[inline] pub fn buffer(&self) -> &[u8] { // SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and diff --git a/library/std/src/io/buffered/bufwriter.rs b/library/std/src/io/buffered/bufwriter.rs index 13516d3b961f1..c41bae2aa4e81 100644 --- a/library/std/src/io/buffered/bufwriter.rs +++ b/library/std/src/io/buffered/bufwriter.rs @@ -94,6 +94,16 @@ impl BufWriter { BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) } + pub(crate) fn try_new_buffer() -> io::Result> { + Vec::try_with_capacity(DEFAULT_BUF_SIZE).map_err(|_| { + io::const_io_error!(ErrorKind::OutOfMemory, "failed to allocate write buffer") + }) + } + + pub(crate) fn with_buffer(inner: W, buf: Vec) -> Self { + Self { inner, buf, panicked: false } + } + /// Creates a new `BufWriter` with at least the specified buffer capacity. /// /// # Examples diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 4d93af6ea652e..b81e7c18abbb0 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -374,6 +374,7 @@ #![feature(slice_concat_trait)] #![feature(thin_box)] #![feature(try_reserve_kind)] +#![feature(try_with_capacity)] #![feature(vec_into_raw_parts)] // tidy-alphabetical-end // From 0999b019f8e0a96ca7aac9fd5ec095d59b0806ec Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 24 Sep 2024 14:25:16 -0700 Subject: [PATCH 3/4] Dogfood `feature(file_buffered)` --- compiler/rustc_borrowck/src/facts.rs | 4 ++-- compiler/rustc_borrowck/src/lib.rs | 1 + compiler/rustc_codegen_llvm/src/back/lto.rs | 7 +++---- compiler/rustc_codegen_llvm/src/lib.rs | 1 + compiler/rustc_codegen_ssa/src/back/linker.rs | 15 +++++++-------- compiler/rustc_codegen_ssa/src/lib.rs | 1 + compiler/rustc_data_structures/src/lib.rs | 1 + .../src/obligation_forest/graphviz.rs | 3 +-- .../rustc_incremental/src/assert_dep_graph.rs | 4 ++-- compiler/rustc_incremental/src/lib.rs | 1 + compiler/rustc_interface/src/lib.rs | 1 + compiler/rustc_interface/src/passes.rs | 2 +- compiler/rustc_metadata/src/fs.rs | 3 +-- compiler/rustc_metadata/src/lib.rs | 1 + compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/mir/pretty.rs | 4 ++-- .../rustc_mir_dataflow/src/framework/engine.rs | 5 ++--- compiler/rustc_mir_dataflow/src/lib.rs | 1 + compiler/rustc_mir_transform/src/dump_mir.rs | 2 +- compiler/rustc_mir_transform/src/lib.rs | 1 + compiler/rustc_monomorphize/src/lib.rs | 1 + compiler/rustc_monomorphize/src/partitioning.rs | 5 ++--- library/std/src/sys/pal/unix/thread.rs | 4 ++-- library/test/src/lib.rs | 1 + library/test/src/term/terminfo/mod.rs | 6 ++---- src/librustdoc/html/render/write_shared.rs | 5 ++--- src/librustdoc/json/mod.rs | 2 +- src/librustdoc/lib.rs | 1 + 28 files changed, 44 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_borrowck/src/facts.rs b/compiler/rustc_borrowck/src/facts.rs index 713452796c602..ef8e757a54780 100644 --- a/compiler/rustc_borrowck/src/facts.rs +++ b/compiler/rustc_borrowck/src/facts.rs @@ -1,7 +1,7 @@ use std::error::Error; use std::fmt::Debug; use std::fs::{self, File}; -use std::io::{BufWriter, Write}; +use std::io::Write; use std::path::Path; use polonius_engine::{AllFacts as PoloniusFacts, Atom}; @@ -127,7 +127,7 @@ impl<'w> FactWriter<'w> { T: FactRow, { let file = &self.dir.join(file_name); - let mut file = BufWriter::new(File::create(file)?); + let mut file = File::create_buffered(file)?; for row in rows { row.write(&mut file, self.location_table)?; } diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 733fabe557e6b..a11eca0b9c746 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -6,6 +6,7 @@ #![feature(assert_matches)] #![feature(box_patterns)] #![feature(control_flow_enum)] +#![feature(file_buffered)] #![feature(let_chains)] #![feature(never_type)] #![feature(rustc_attrs)] diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 5e510177818b4..1f7a923dd2c68 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -808,8 +808,7 @@ struct ThinLTOKeysMap { impl ThinLTOKeysMap { fn save_to_file(&self, path: &Path) -> io::Result<()> { use std::io::Write; - let file = File::create(path)?; - let mut writer = io::BufWriter::new(file); + let mut writer = File::create_buffered(path)?; // The entries are loaded back into a hash map in `load_from_file()`, so // the order in which we write them to file here does not matter. for (module, key) in &self.keys { @@ -821,8 +820,8 @@ impl ThinLTOKeysMap { fn load_from_file(path: &Path) -> io::Result { use std::io::BufRead; let mut keys = BTreeMap::default(); - let file = File::open(path)?; - for line in io::BufReader::new(file).lines() { + let file = File::open_buffered(path)?; + for line in file.lines() { let line = line?; let mut split = line.split(' '); let module = split.next().unwrap(); diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index d69112612ba6b..bdfc0f626f801 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -11,6 +11,7 @@ #![feature(assert_matches)] #![feature(exact_size_is_empty)] #![feature(extern_types)] +#![feature(file_buffered)] #![feature(hash_raw_entry)] #![feature(impl_trait_in_assoc_type)] #![feature(iter_intersperse)] diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 84817e1984458..a73ec83ee62cc 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -1,9 +1,8 @@ use std::ffi::{OsStr, OsString}; use std::fs::{self, File}; use std::io::prelude::*; -use std::io::{self, BufWriter}; use std::path::{Path, PathBuf}; -use std::{env, iter, mem, str}; +use std::{env, io, iter, mem, str}; use cc::windows_registry; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; @@ -754,7 +753,7 @@ impl<'a> Linker for GccLinker<'a> { if self.sess.target.is_like_osx { // Write a plain, newline-separated list of symbols let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; for sym in symbols { debug!(" _{sym}"); writeln!(f, "_{sym}")?; @@ -765,7 +764,7 @@ impl<'a> Linker for GccLinker<'a> { } } else if is_windows { let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; // .def file similar to MSVC one but without LIBRARY section // because LD doesn't like when it's empty @@ -781,7 +780,7 @@ impl<'a> Linker for GccLinker<'a> { } else { // Write an LD version script let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; writeln!(f, "{{")?; if !symbols.is_empty() { writeln!(f, " global:")?; @@ -1059,7 +1058,7 @@ impl<'a> Linker for MsvcLinker<'a> { let path = tmpdir.join("lib.def"); let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; // Start off with the standard module name header and then go // straight to exports. @@ -1648,7 +1647,7 @@ impl<'a> Linker for AixLinker<'a> { fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { let path = tmpdir.join("list.exp"); let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; // FIXME: use llvm-nm to generate export list. for symbol in symbols { debug!(" _{symbol}"); @@ -1961,7 +1960,7 @@ impl<'a> Linker for BpfLinker<'a> { fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { let path = tmpdir.join("symbols"); let res: io::Result<()> = try { - let mut f = BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; for sym in symbols { writeln!(f, "{sym}")?; } diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index ec0520fbb09d3..162d14272a555 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -6,6 +6,7 @@ #![doc(rust_logo)] #![feature(assert_matches)] #![feature(box_patterns)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(let_chains)] #![feature(negative_impls)] diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index a35f5b1f17db4..f225684d99ff4 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -23,6 +23,7 @@ #![feature(cfg_match)] #![feature(core_intrinsics)] #![feature(extend_one)] +#![feature(file_buffered)] #![feature(hash_raw_entry)] #![feature(macro_metavar_expr)] #![feature(map_try_insert)] diff --git a/compiler/rustc_data_structures/src/obligation_forest/graphviz.rs b/compiler/rustc_data_structures/src/obligation_forest/graphviz.rs index 60cde9a52b411..65a24366db837 100644 --- a/compiler/rustc_data_structures/src/obligation_forest/graphviz.rs +++ b/compiler/rustc_data_structures/src/obligation_forest/graphviz.rs @@ -1,6 +1,5 @@ use std::env::var_os; use std::fs::File; -use std::io::BufWriter; use std::path::Path; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -33,7 +32,7 @@ impl ObligationForest { let file_path = dir.as_ref().join(format!("{counter:010}_{description}.gv")); - let mut gv_file = BufWriter::new(File::create(file_path).unwrap()); + let mut gv_file = File::create_buffered(file_path).unwrap(); dot::render(&self, &mut gv_file).unwrap(); } diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index 646b9dbe1331b..a006786aa75b9 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -35,7 +35,7 @@ use std::env; use std::fs::{self, File}; -use std::io::{BufWriter, Write}; +use std::io::Write; use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::graph::implementation::{Direction, INCOMING, NodeIndex, OUTGOING}; @@ -245,7 +245,7 @@ fn dump_graph(query: &DepGraphQuery) { { // dump a .txt file with just the edges: let txt_path = format!("{path}.txt"); - let mut file = BufWriter::new(File::create(&txt_path).unwrap()); + let mut file = File::create_buffered(&txt_path).unwrap(); for (source, target) in &edges { write!(file, "{source:?} -> {target:?}\n").unwrap(); } diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index dda1232b80bf5..e8735cba9bd23 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -5,6 +5,7 @@ #![deny(missing_docs)] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(rust_logo)] +#![feature(file_buffered)] #![feature(rustdoc_internals)] #![warn(unreachable_pub)] // tidy-alphabetical-end diff --git a/compiler/rustc_interface/src/lib.rs b/compiler/rustc_interface/src/lib.rs index 94ebe51f213f6..b81a740270163 100644 --- a/compiler/rustc_interface/src/lib.rs +++ b/compiler/rustc_interface/src/lib.rs @@ -1,5 +1,6 @@ // tidy-alphabetical-start #![feature(decl_macro)] +#![feature(file_buffered)] #![feature(let_chains)] #![feature(try_blocks)] #![warn(unreachable_pub)] diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 617581cf667f0..bba517915a2df 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -519,7 +519,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P write_deps_to_file(&mut file)?; } OutFileName::Real(ref path) => { - let mut file = BufWriter::new(fs::File::create(path)?); + let mut file = fs::File::create_buffered(path)?; write_deps_to_file(&mut file)?; } } diff --git a/compiler/rustc_metadata/src/fs.rs b/compiler/rustc_metadata/src/fs.rs index a58f3c6b5caad..4450d050c8e1f 100644 --- a/compiler/rustc_metadata/src/fs.rs +++ b/compiler/rustc_metadata/src/fs.rs @@ -128,8 +128,7 @@ pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> { } pub fn copy_to_stdout(from: &Path) -> io::Result<()> { - let file = fs::File::open(from)?; - let mut reader = io::BufReader::new(file); + let mut reader = fs::File::open_buffered(from)?; let mut stdout = io::stdout(); io::copy(&mut reader, &mut stdout)?; Ok(()) diff --git a/compiler/rustc_metadata/src/lib.rs b/compiler/rustc_metadata/src/lib.rs index 1759ea7d44154..10f2087d1e6f1 100644 --- a/compiler/rustc_metadata/src/lib.rs +++ b/compiler/rustc_metadata/src/lib.rs @@ -8,6 +8,7 @@ #![feature(decl_macro)] #![feature(error_iter)] #![feature(extract_if)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(iter_from_coroutine)] #![feature(let_chains)] diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 23cd247e88418..a32b19b067a55 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -45,6 +45,7 @@ #![feature(discriminant_kind)] #![feature(extern_types)] #![feature(extract_if)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(intra_doc_pointers)] #![feature(iter_from_coroutine)] diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 119aed1106dde..4878956521831 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -277,9 +277,9 @@ pub fn create_dump_file<'tcx>( ) })?; } - Ok(io::BufWriter::new(fs::File::create(&file_path).map_err(|e| { + Ok(fs::File::create_buffered(&file_path).map_err(|e| { io::Error::new(e.kind(), format!("IO error creating MIR dump file: {file_path:?}; {e}")) - })?)) + })?) } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_mir_dataflow/src/framework/engine.rs b/compiler/rustc_mir_dataflow/src/framework/engine.rs index 947bdf860b58d..da01a9740943d 100644 --- a/compiler/rustc_mir_dataflow/src/framework/engine.rs +++ b/compiler/rustc_mir_dataflow/src/framework/engine.rs @@ -266,7 +266,7 @@ where A::Domain: DebugWithContext, { use std::fs; - use std::io::{self, Write}; + use std::io::Write; let def_id = body.source.def_id(); let Ok(attrs) = RustcMirAttrs::parse(tcx, def_id) else { @@ -281,8 +281,7 @@ where if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; } - let f = fs::File::create(&path)?; - io::BufWriter::new(f) + fs::File::create_buffered(&path)? } None if dump_enabled(tcx, A::NAME, def_id) => { diff --git a/compiler/rustc_mir_dataflow/src/lib.rs b/compiler/rustc_mir_dataflow/src/lib.rs index cb7c7edc413a6..cd926c0764129 100644 --- a/compiler/rustc_mir_dataflow/src/lib.rs +++ b/compiler/rustc_mir_dataflow/src/lib.rs @@ -3,6 +3,7 @@ #![feature(associated_type_defaults)] #![feature(box_patterns)] #![feature(exact_size_is_empty)] +#![feature(file_buffered)] #![feature(let_chains)] #![feature(try_blocks)] #![warn(unreachable_pub)] diff --git a/compiler/rustc_mir_transform/src/dump_mir.rs b/compiler/rustc_mir_transform/src/dump_mir.rs index d7c473604196c..5dd84975b88ed 100644 --- a/compiler/rustc_mir_transform/src/dump_mir.rs +++ b/compiler/rustc_mir_transform/src/dump_mir.rs @@ -24,7 +24,7 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> { write_mir_pretty(tcx, None, &mut f)?; } OutFileName::Real(path) => { - let mut f = io::BufWriter::new(File::create(&path)?); + let mut f = File::create_buffered(&path)?; write_mir_pretty(tcx, None, &mut f)?; if tcx.sess.opts.json_artifact_notifications { tcx.dcx().emit_artifact_notification(&path, "mir"); diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 56268c1d201d6..4c090665992bd 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -3,6 +3,7 @@ #![feature(box_patterns)] #![feature(const_type_name)] #![feature(cow_is_borrowed)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(impl_trait_in_assoc_type)] #![feature(let_chains)] diff --git a/compiler/rustc_monomorphize/src/lib.rs b/compiler/rustc_monomorphize/src/lib.rs index 91101ddd59022..e92e6978d0f49 100644 --- a/compiler/rustc_monomorphize/src/lib.rs +++ b/compiler/rustc_monomorphize/src/lib.rs @@ -1,5 +1,6 @@ // tidy-alphabetical-start #![feature(array_windows)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(let_chains)] #![warn(unreachable_pub)] diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 1c1e6164f2eb0..ad05cca66cab3 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -95,7 +95,7 @@ use std::cmp; use std::collections::hash_map::Entry; use std::fs::{self, File}; -use std::io::{BufWriter, Write}; +use std::io::Write; use std::path::{Path, PathBuf}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; @@ -1243,8 +1243,7 @@ fn dump_mono_items_stats<'tcx>( let ext = format.extension(); let filename = format!("{crate_name}.mono_items.{ext}"); let output_path = output_directory.join(&filename); - let file = File::create(&output_path)?; - let mut file = BufWriter::new(file); + let mut file = File::create_buffered(&output_path)?; // Gather instantiated mono items grouped by def_id let mut items_per_def_id: FxIndexMap<_, Vec<_>> = Default::default(); diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 7fe9b6c3e52f4..ce5e8ea5866d5 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -517,7 +517,7 @@ mod cgroups { use crate::borrow::Cow; use crate::ffi::OsString; use crate::fs::{File, exists}; - use crate::io::{BufRead, BufReader, Read}; + use crate::io::{BufRead, Read}; use crate::os::unix::ffi::OsStringExt; use crate::path::{Path, PathBuf}; use crate::str::from_utf8; @@ -690,7 +690,7 @@ mod cgroups { /// If the cgroupfs is a bind mount then `group_path` is adjusted to skip /// over the already-included prefix fn find_mountpoint(group_path: &Path) -> Option<(Cow<'static, str>, &Path)> { - let mut reader = BufReader::new(File::open("/proc/self/mountinfo").ok()?); + let mut reader = File::open_buffered("/proc/self/mountinfo").ok()?; let mut line = String::with_capacity(256); loop { line.clear(); diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index ebbe50cc65154..4b2c65cfdf548 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -18,6 +18,7 @@ #![doc(test(attr(deny(warnings))))] #![doc(rust_logo)] #![feature(rustdoc_internals)] +#![feature(file_buffered)] #![feature(internal_output_capture)] #![feature(staged_api)] #![feature(process_exitcode_internals)] diff --git a/library/test/src/term/terminfo/mod.rs b/library/test/src/term/terminfo/mod.rs index ac10ec2b850ec..974b8afd598dd 100644 --- a/library/test/src/term/terminfo/mod.rs +++ b/library/test/src/term/terminfo/mod.rs @@ -3,9 +3,8 @@ use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; -use std::io::{self, BufReader}; use std::path::Path; -use std::{env, error, fmt}; +use std::{env, error, fmt, io}; use parm::{Param, Variables, expand}; use parser::compiled::{msys_terminfo, parse}; @@ -102,8 +101,7 @@ impl TermInfo { } // Keep the metadata small fn _from_path(path: &Path) -> Result { - let file = File::open(path).map_err(Error::IoError)?; - let mut reader = BufReader::new(file); + let mut reader = File::open_buffered(path).map_err(Error::IoError)?; parse(&mut reader, false).map_err(Error::MalformedTerminfo) } } diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 1625379921577..12246b0d416a8 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -16,7 +16,7 @@ use std::cell::RefCell; use std::ffi::OsString; use std::fs::File; -use std::io::{self, BufWriter, Write as _}; +use std::io::{self, Write as _}; use std::iter::once; use std::marker::PhantomData; use std::path::{Component, Path, PathBuf}; @@ -1020,8 +1020,7 @@ where for part in parts { template.append(part); } - let file = try_err!(File::create(&path), &path); - let mut file = BufWriter::new(file); + let mut file = try_err!(File::create_buffered(&path), &path); try_err!(write!(file, "{template}"), &path); try_err!(file.flush(), &path); } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 7f82fb5c68658..b7a683eed1c2f 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -286,7 +286,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { self.serialize_and_write( output_crate, - BufWriter::new(try_err!(File::create(&p), p)), + try_err!(File::create_buffered(&p), p), &p.display().to_string(), ) } else { diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index dd75f4d7928ae..2be415e2e0ef3 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -5,6 +5,7 @@ #![feature(rustc_private)] #![feature(assert_matches)] #![feature(box_patterns)] +#![feature(file_buffered)] #![feature(if_let_guard)] #![feature(impl_trait_in_assoc_type)] #![feature(iter_intersperse)] From 458537ebc0e7d893103e03450f8830061bab1b2d Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 24 Sep 2024 15:06:55 -0700 Subject: [PATCH 4/4] Add a tracking issue for `file_buffered` --- library/std/src/fs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index b8e3f316bebd9..db7867337dd59 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -405,7 +405,7 @@ impl File { /// Ok(()) /// } /// ``` - #[unstable(feature = "file_buffered", issue = "none")] + #[unstable(feature = "file_buffered", issue = "130804")] pub fn open_buffered>(path: P) -> io::Result> { // Allocate the buffer *first* so we don't affect the filesystem otherwise. let buffer = io::BufReader::::try_new_buffer()?; @@ -473,7 +473,7 @@ impl File { /// Ok(()) /// } /// ``` - #[unstable(feature = "file_buffered", issue = "none")] + #[unstable(feature = "file_buffered", issue = "130804")] pub fn create_buffered>(path: P) -> io::Result> { // Allocate the buffer *first* so we don't affect the filesystem otherwise. let buffer = io::BufWriter::::try_new_buffer()?;