Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4c62024

Browse files
committed
Auto merge of rust-lang#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: rust-lang#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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 8 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

0 commit comments

Comments
 (0)