Skip to content

Commit 4c62024

Browse files
committed
Auto merge of #130803 - cuviper:file-buffered, r=joshtriplett
Add `File` constructors that return files wrapped with a buffer In addition to the light convenience, these are intended to raise visibility that buffering is something you should consider when opening a file, since unbuffered I/O is a common performance footgun to Rust newcomers. ACP: rust-lang/libs-team#446 Tracking Issue: #130804
2 parents 1b5aa96 + 458537e commit 4c62024

File tree

33 files changed

+151
-41
lines changed

33 files changed

+151
-41
lines changed

compiler/rustc_borrowck/src/facts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::error::Error;
22
use std::fmt::Debug;
33
use std::fs::{self, File};
4-
use std::io::{BufWriter, Write};
4+
use std::io::Write;
55
use std::path::Path;
66

77
use polonius_engine::{AllFacts as PoloniusFacts, Atom};
@@ -127,7 +127,7 @@ impl<'w> FactWriter<'w> {
127127
T: FactRow,
128128
{
129129
let file = &self.dir.join(file_name);
130-
let mut file = BufWriter::new(File::create(file)?);
130+
let mut file = File::create_buffered(file)?;
131131
for row in rows {
132132
row.write(&mut file, self.location_table)?;
133133
}

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(assert_matches)]
77
#![feature(box_patterns)]
88
#![feature(control_flow_enum)]
9+
#![feature(file_buffered)]
910
#![feature(let_chains)]
1011
#![feature(never_type)]
1112
#![feature(rustc_attrs)]

compiler/rustc_codegen_llvm/src/back/lto.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ struct ThinLTOKeysMap {
808808
impl ThinLTOKeysMap {
809809
fn save_to_file(&self, path: &Path) -> io::Result<()> {
810810
use std::io::Write;
811-
let file = File::create(path)?;
812-
let mut writer = io::BufWriter::new(file);
811+
let mut writer = File::create_buffered(path)?;
813812
// The entries are loaded back into a hash map in `load_from_file()`, so
814813
// the order in which we write them to file here does not matter.
815814
for (module, key) in &self.keys {
@@ -821,8 +820,8 @@ impl ThinLTOKeysMap {
821820
fn load_from_file(path: &Path) -> io::Result<Self> {
822821
use std::io::BufRead;
823822
let mut keys = BTreeMap::default();
824-
let file = File::open(path)?;
825-
for line in io::BufReader::new(file).lines() {
823+
let file = File::open_buffered(path)?;
824+
for line in file.lines() {
826825
let line = line?;
827826
let mut split = line.split(' ');
828827
let module = split.next().unwrap();

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(assert_matches)]
1212
#![feature(exact_size_is_empty)]
1313
#![feature(extern_types)]
14+
#![feature(file_buffered)]
1415
#![feature(hash_raw_entry)]
1516
#![feature(impl_trait_in_assoc_type)]
1617
#![feature(iter_intersperse)]

compiler/rustc_codegen_ssa/src/back/linker.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::ffi::{OsStr, OsString};
22
use std::fs::{self, File};
33
use std::io::prelude::*;
4-
use std::io::{self, BufWriter};
54
use std::path::{Path, PathBuf};
6-
use std::{env, iter, mem, str};
5+
use std::{env, io, iter, mem, str};
76

87
use cc::windows_registry;
98
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -754,7 +753,7 @@ impl<'a> Linker for GccLinker<'a> {
754753
if self.sess.target.is_like_osx {
755754
// Write a plain, newline-separated list of symbols
756755
let res: io::Result<()> = try {
757-
let mut f = BufWriter::new(File::create(&path)?);
756+
let mut f = File::create_buffered(&path)?;
758757
for sym in symbols {
759758
debug!(" _{sym}");
760759
writeln!(f, "_{sym}")?;
@@ -765,7 +764,7 @@ impl<'a> Linker for GccLinker<'a> {
765764
}
766765
} else if is_windows {
767766
let res: io::Result<()> = try {
768-
let mut f = BufWriter::new(File::create(&path)?);
767+
let mut f = File::create_buffered(&path)?;
769768

770769
// .def file similar to MSVC one but without LIBRARY section
771770
// because LD doesn't like when it's empty
@@ -781,7 +780,7 @@ impl<'a> Linker for GccLinker<'a> {
781780
} else {
782781
// Write an LD version script
783782
let res: io::Result<()> = try {
784-
let mut f = BufWriter::new(File::create(&path)?);
783+
let mut f = File::create_buffered(&path)?;
785784
writeln!(f, "{{")?;
786785
if !symbols.is_empty() {
787786
writeln!(f, " global:")?;
@@ -1059,7 +1058,7 @@ impl<'a> Linker for MsvcLinker<'a> {
10591058

10601059
let path = tmpdir.join("lib.def");
10611060
let res: io::Result<()> = try {
1062-
let mut f = BufWriter::new(File::create(&path)?);
1061+
let mut f = File::create_buffered(&path)?;
10631062

10641063
// Start off with the standard module name header and then go
10651064
// straight to exports.
@@ -1648,7 +1647,7 @@ impl<'a> Linker for AixLinker<'a> {
16481647
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
16491648
let path = tmpdir.join("list.exp");
16501649
let res: io::Result<()> = try {
1651-
let mut f = BufWriter::new(File::create(&path)?);
1650+
let mut f = File::create_buffered(&path)?;
16521651
// FIXME: use llvm-nm to generate export list.
16531652
for symbol in symbols {
16541653
debug!(" _{symbol}");
@@ -1961,7 +1960,7 @@ impl<'a> Linker for BpfLinker<'a> {
19611960
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
19621961
let path = tmpdir.join("symbols");
19631962
let res: io::Result<()> = try {
1964-
let mut f = BufWriter::new(File::create(&path)?);
1963+
let mut f = File::create_buffered(&path)?;
19651964
for sym in symbols {
19661965
writeln!(f, "{sym}")?;
19671966
}

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![doc(rust_logo)]
77
#![feature(assert_matches)]
88
#![feature(box_patterns)]
9+
#![feature(file_buffered)]
910
#![feature(if_let_guard)]
1011
#![feature(let_chains)]
1112
#![feature(negative_impls)]

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(cfg_match)]
2424
#![feature(core_intrinsics)]
2525
#![feature(extend_one)]
26+
#![feature(file_buffered)]
2627
#![feature(hash_raw_entry)]
2728
#![feature(macro_metavar_expr)]
2829
#![feature(map_try_insert)]

compiler/rustc_data_structures/src/obligation_forest/graphviz.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::env::var_os;
22
use std::fs::File;
3-
use std::io::BufWriter;
43
use std::path::Path;
54
use std::sync::atomic::{AtomicUsize, Ordering};
65

@@ -33,7 +32,7 @@ impl<O: ForestObligation> ObligationForest<O> {
3332

3433
let file_path = dir.as_ref().join(format!("{counter:010}_{description}.gv"));
3534

36-
let mut gv_file = BufWriter::new(File::create(file_path).unwrap());
35+
let mut gv_file = File::create_buffered(file_path).unwrap();
3736

3837
dot::render(&self, &mut gv_file).unwrap();
3938
}

compiler/rustc_incremental/src/assert_dep_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
3636
use std::env;
3737
use std::fs::{self, File};
38-
use std::io::{BufWriter, Write};
38+
use std::io::Write;
3939

4040
use rustc_data_structures::fx::FxIndexSet;
4141
use rustc_data_structures::graph::implementation::{Direction, INCOMING, NodeIndex, OUTGOING};
@@ -245,7 +245,7 @@ fn dump_graph(query: &DepGraphQuery) {
245245
{
246246
// dump a .txt file with just the edges:
247247
let txt_path = format!("{path}.txt");
248-
let mut file = BufWriter::new(File::create(&txt_path).unwrap());
248+
let mut file = File::create_buffered(&txt_path).unwrap();
249249
for (source, target) in &edges {
250250
write!(file, "{source:?} -> {target:?}\n").unwrap();
251251
}

compiler/rustc_incremental/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![deny(missing_docs)]
66
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
77
#![doc(rust_logo)]
8+
#![feature(file_buffered)]
89
#![feature(rustdoc_internals)]
910
#![warn(unreachable_pub)]
1011
// tidy-alphabetical-end

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// tidy-alphabetical-start
22
#![feature(decl_macro)]
3+
#![feature(file_buffered)]
34
#![feature(let_chains)]
45
#![feature(try_blocks)]
56
#![warn(unreachable_pub)]

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
519519
write_deps_to_file(&mut file)?;
520520
}
521521
OutFileName::Real(ref path) => {
522-
let mut file = BufWriter::new(fs::File::create(path)?);
522+
let mut file = fs::File::create_buffered(path)?;
523523
write_deps_to_file(&mut file)?;
524524
}
525525
}

compiler/rustc_metadata/src/fs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
128128
}
129129

130130
pub fn copy_to_stdout(from: &Path) -> io::Result<()> {
131-
let file = fs::File::open(from)?;
132-
let mut reader = io::BufReader::new(file);
131+
let mut reader = fs::File::open_buffered(from)?;
133132
let mut stdout = io::stdout();
134133
io::copy(&mut reader, &mut stdout)?;
135134
Ok(())

compiler/rustc_metadata/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(decl_macro)]
99
#![feature(error_iter)]
1010
#![feature(extract_if)]
11+
#![feature(file_buffered)]
1112
#![feature(if_let_guard)]
1213
#![feature(iter_from_coroutine)]
1314
#![feature(let_chains)]

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#![feature(discriminant_kind)]
4646
#![feature(extern_types)]
4747
#![feature(extract_if)]
48+
#![feature(file_buffered)]
4849
#![feature(if_let_guard)]
4950
#![feature(intra_doc_pointers)]
5051
#![feature(iter_from_coroutine)]

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ pub fn create_dump_file<'tcx>(
277277
)
278278
})?;
279279
}
280-
Ok(io::BufWriter::new(fs::File::create(&file_path).map_err(|e| {
280+
Ok(fs::File::create_buffered(&file_path).map_err(|e| {
281281
io::Error::new(e.kind(), format!("IO error creating MIR dump file: {file_path:?}; {e}"))
282-
})?))
282+
})?)
283283
}
284284

285285
///////////////////////////////////////////////////////////////////////////

compiler/rustc_mir_dataflow/src/framework/engine.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ where
266266
A::Domain: DebugWithContext<A>,
267267
{
268268
use std::fs;
269-
use std::io::{self, Write};
269+
use std::io::Write;
270270

271271
let def_id = body.source.def_id();
272272
let Ok(attrs) = RustcMirAttrs::parse(tcx, def_id) else {
@@ -281,8 +281,7 @@ where
281281
if let Some(parent) = path.parent() {
282282
fs::create_dir_all(parent)?;
283283
}
284-
let f = fs::File::create(&path)?;
285-
io::BufWriter::new(f)
284+
fs::File::create_buffered(&path)?
286285
}
287286

288287
None if dump_enabled(tcx, A::NAME, def_id) => {

compiler/rustc_mir_dataflow/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(associated_type_defaults)]
44
#![feature(box_patterns)]
55
#![feature(exact_size_is_empty)]
6+
#![feature(file_buffered)]
67
#![feature(let_chains)]
78
#![feature(try_blocks)]
89
#![warn(unreachable_pub)]

compiler/rustc_mir_transform/src/dump_mir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
2424
write_mir_pretty(tcx, None, &mut f)?;
2525
}
2626
OutFileName::Real(path) => {
27-
let mut f = io::BufWriter::new(File::create(&path)?);
27+
let mut f = File::create_buffered(&path)?;
2828
write_mir_pretty(tcx, None, &mut f)?;
2929
if tcx.sess.opts.json_artifact_notifications {
3030
tcx.dcx().emit_artifact_notification(&path, "mir");

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(box_patterns)]
44
#![feature(const_type_name)]
55
#![feature(cow_is_borrowed)]
6+
#![feature(file_buffered)]
67
#![feature(if_let_guard)]
78
#![feature(impl_trait_in_assoc_type)]
89
#![feature(let_chains)]

compiler/rustc_monomorphize/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// tidy-alphabetical-start
22
#![feature(array_windows)]
3+
#![feature(file_buffered)]
34
#![feature(if_let_guard)]
45
#![feature(let_chains)]
56
#![warn(unreachable_pub)]

compiler/rustc_monomorphize/src/partitioning.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
use std::cmp;
9696
use std::collections::hash_map::Entry;
9797
use std::fs::{self, File};
98-
use std::io::{BufWriter, Write};
98+
use std::io::Write;
9999
use std::path::{Path, PathBuf};
100100

101101
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
@@ -1243,8 +1243,7 @@ fn dump_mono_items_stats<'tcx>(
12431243
let ext = format.extension();
12441244
let filename = format!("{crate_name}.mono_items.{ext}");
12451245
let output_path = output_directory.join(&filename);
1246-
let file = File::create(&output_path)?;
1247-
let mut file = BufWriter::new(file);
1246+
let mut file = File::create_buffered(&output_path)?;
12481247

12491248
// Gather instantiated mono items grouped by def_id
12501249
let mut items_per_def_id: FxIndexMap<_, Vec<_>> = Default::default();

library/std/src/fs.rs

+77
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,44 @@ impl File {
375375
OpenOptions::new().read(true).open(path.as_ref())
376376
}
377377

378+
/// Attempts to open a file in read-only mode with buffering.
379+
///
380+
/// See the [`OpenOptions::open`] method, the [`BufReader`][io::BufReader] type,
381+
/// and the [`BufRead`][io::BufRead] trait for more details.
382+
///
383+
/// If you only need to read the entire file contents,
384+
/// consider [`std::fs::read()`][self::read] or
385+
/// [`std::fs::read_to_string()`][self::read_to_string] instead.
386+
///
387+
/// # Errors
388+
///
389+
/// This function will return an error if `path` does not already exist.
390+
/// Other errors may also be returned according to [`OpenOptions::open`].
391+
///
392+
/// # Examples
393+
///
394+
/// ```no_run
395+
/// #![feature(file_buffered)]
396+
/// use std::fs::File;
397+
/// use std::io::BufRead;
398+
///
399+
/// fn main() -> std::io::Result<()> {
400+
/// let mut f = File::open_buffered("foo.txt")?;
401+
/// assert!(f.capacity() > 0);
402+
/// for (line, i) in f.lines().zip(1..) {
403+
/// println!("{i:6}: {}", line?);
404+
/// }
405+
/// Ok(())
406+
/// }
407+
/// ```
408+
#[unstable(feature = "file_buffered", issue = "130804")]
409+
pub fn open_buffered<P: AsRef<Path>>(path: P) -> io::Result<io::BufReader<File>> {
410+
// Allocate the buffer *first* so we don't affect the filesystem otherwise.
411+
let buffer = io::BufReader::<Self>::try_new_buffer()?;
412+
let file = File::open(path)?;
413+
Ok(io::BufReader::with_buffer(file, buffer))
414+
}
415+
378416
/// Opens a file in write-only mode.
379417
///
380418
/// This function will create a file if it does not exist,
@@ -404,6 +442,45 @@ impl File {
404442
OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
405443
}
406444

445+
/// Opens a file in write-only mode with buffering.
446+
///
447+
/// This function will create a file if it does not exist,
448+
/// and will truncate it if it does.
449+
///
450+
/// Depending on the platform, this function may fail if the
451+
/// full directory path does not exist.
452+
///
453+
/// See the [`OpenOptions::open`] method and the
454+
/// [`BufWriter`][io::BufWriter] type for more details.
455+
///
456+
/// See also [`std::fs::write()`][self::write] for a simple function to
457+
/// create a file with some given data.
458+
///
459+
/// # Examples
460+
///
461+
/// ```no_run
462+
/// #![feature(file_buffered)]
463+
/// use std::fs::File;
464+
/// use std::io::Write;
465+
///
466+
/// fn main() -> std::io::Result<()> {
467+
/// let mut f = File::create_buffered("foo.txt")?;
468+
/// assert!(f.capacity() > 0);
469+
/// for i in 0..100 {
470+
/// writeln!(&mut f, "{i}")?;
471+
/// }
472+
/// f.flush()?;
473+
/// Ok(())
474+
/// }
475+
/// ```
476+
#[unstable(feature = "file_buffered", issue = "130804")]
477+
pub fn create_buffered<P: AsRef<Path>>(path: P) -> io::Result<io::BufWriter<File>> {
478+
// Allocate the buffer *first* so we don't affect the filesystem otherwise.
479+
let buffer = io::BufWriter::<Self>::try_new_buffer()?;
480+
let file = File::create(path)?;
481+
Ok(io::BufWriter::with_buffer(file, buffer))
482+
}
483+
407484
/// Creates a new file in read-write mode; error if the file exists.
408485
///
409486
/// This function will create a file if it does not exist, or return an error if it does. This

0 commit comments

Comments
 (0)