Skip to content

Commit 2bbcafc

Browse files
committed
Mmap::map is always used on a freshly opened file, merge those two operations
1 parent e738847 commit 2bbcafc

File tree

10 files changed

+40
-69
lines changed

10 files changed

+40
-69
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// /usr/bin/ld: warning: type of symbol `_RNvNvNvNvNtNtNtCsAj5i4SGTR7_3std4sync4mpmc5waker17current_thread_id5DUMMY7___getit5___KEY' changed from 1 to 6 in /tmp/ccKeUSiR.ltrans0.ltrans.o
1818
// /usr/bin/ld: warning: incremental linking of LTO and non-LTO objects; using -flinker-output=nolto-rel which will bypass whole program optimization
1919
use std::ffi::{CStr, CString};
20-
use std::fs::{self, File};
20+
use std::fs;
2121
use std::path::{Path, PathBuf};
2222
use std::sync::Arc;
2323

@@ -126,9 +126,7 @@ fn prepare_lto(
126126
.extend(exported_symbols[&cnum].iter().filter_map(symbol_filter));
127127
}
128128

129-
let archive_data = unsafe {
130-
Mmap::map(File::open(path).expect("couldn't open rlib")).expect("couldn't map rlib")
131-
};
129+
let archive_data = unsafe { Mmap::map(path).expect("couldn't map rlib") };
132130
let archive = ArchiveFile::parse(&*archive_data).expect("wanted an rlib");
133131
let obj_files = archive
134132
.members()

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ fn prepare_lto(
112112
.extend(exported_symbols[&cnum].iter().filter_map(symbol_filter));
113113
}
114114

115-
let archive_data = unsafe {
116-
Mmap::map(std::fs::File::open(&path).expect("couldn't open rlib"))
117-
.expect("couldn't map rlib")
118-
};
115+
let archive_data = unsafe { Mmap::map(&path).expect("couldn't map rlib") };
119116
let archive = ArchiveFile::parse(&*archive_data).expect("wanted an rlib");
120117
let obj_files = archive
121118
.members()

compiler/rustc_codegen_ssa/src/back/archive.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,8 @@ pub trait ArchiveBuilderBuilder {
138138
bundled_lib_file_names: &FxIndexSet<Symbol>,
139139
) -> Result<(), ExtractBundledLibsError<'a>> {
140140
let archive_map = unsafe {
141-
Mmap::map(
142-
File::open(rlib)
143-
.map_err(|e| ExtractBundledLibsError::OpenFile { rlib, error: Box::new(e) })?,
144-
)
145-
.map_err(|e| ExtractBundledLibsError::MmapFile { rlib, error: Box::new(e) })?
141+
Mmap::map(rlib)
142+
.map_err(|e| ExtractBundledLibsError::MmapFile { rlib, error: Box::new(e) })?
146143
};
147144
let archive = ArchiveFile::parse(&*archive_map)
148145
.map_err(|e| ExtractBundledLibsError::ParseArchive { rlib, error: Box::new(e) })?;
@@ -363,7 +360,7 @@ pub fn try_extract_macho_fat_archive(
363360
sess: &Session,
364361
archive_path: &Path,
365362
) -> io::Result<Option<PathBuf>> {
366-
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
363+
let archive_map = unsafe { Mmap::map(&archive_path)? };
367364
let target_arch = match sess.target.arch.as_ref() {
368365
"aarch64" => object::Architecture::Aarch64,
369366
"x86_64" => object::Architecture::X86_64,
@@ -400,7 +397,7 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
400397
return Ok(());
401398
}
402399

403-
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
400+
let archive_map = unsafe { Mmap::map(&archive_path)? };
404401
let archive = ArchiveFile::parse(&*archive_map)
405402
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
406403
let archive_index = self.src_archives.len();
@@ -463,25 +460,22 @@ impl<'a> ArArchiveBuilder<'a> {
463460
let mut entries = Vec::new();
464461

465462
for (entry_name, entry) in self.entries {
466-
let data =
467-
match entry {
468-
ArchiveEntry::FromArchive { archive_index, file_range } => {
469-
let src_archive = &self.src_archives[archive_index];
470-
471-
let data = &src_archive.1
472-
[file_range.0 as usize..file_range.0 as usize + file_range.1 as usize];
473-
474-
Box::new(data) as Box<dyn AsRef<[u8]>>
475-
}
476-
ArchiveEntry::File(file) => unsafe {
477-
Box::new(
478-
Mmap::map(File::open(file).map_err(|err| {
479-
io_error_context("failed to open object file", err)
480-
})?)
463+
let data = match entry {
464+
ArchiveEntry::FromArchive { archive_index, file_range } => {
465+
let src_archive = &self.src_archives[archive_index];
466+
467+
let data = &src_archive.1
468+
[file_range.0 as usize..file_range.0 as usize + file_range.1 as usize];
469+
470+
Box::new(data) as Box<dyn AsRef<[u8]>>
471+
}
472+
ArchiveEntry::File(file) => unsafe {
473+
Box::new(
474+
Mmap::map(file)
481475
.map_err(|err| io_error_context("failed to map object file", err))?,
482-
) as Box<dyn AsRef<[u8]>>
483-
},
484-
};
476+
) as Box<dyn AsRef<[u8]>>
477+
},
478+
};
485479

486480
entries.push(NewArchiveMember {
487481
buf: data,

compiler/rustc_codegen_ssa/src/back/link.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::BTreeSet;
22
use std::ffi::OsString;
3-
use std::fs::{File, OpenOptions, read};
3+
use std::fs::{OpenOptions, read};
44
use std::io::{BufWriter, Write};
55
use std::ops::{ControlFlow, Deref};
66
use std::path::{Path, PathBuf};
@@ -680,8 +680,7 @@ fn link_dwarf_object(sess: &Session, cg_results: &CodegenResults, executable_out
680680
}
681681

682682
fn read_input(&self, path: &Path) -> std::io::Result<&[u8]> {
683-
let file = File::open(&path)?;
684-
let mmap = (unsafe { Mmap::map(file) })?;
683+
let mmap = (unsafe { Mmap::map(&path) })?;
685684
Ok(self.alloc_mmap(mmap))
686685
}
687686
}

compiler/rustc_codegen_ssa/src/back/metadata.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Reading of the rustc metadata for rlibs and dylibs
22
33
use std::borrow::Cow;
4-
use std::fs::File;
54
use std::io::Write;
65
use std::path::Path;
76

@@ -45,10 +44,7 @@ fn load_metadata_with(
4544
path: &Path,
4645
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
4746
) -> Result<OwnedSlice, String> {
48-
let file =
49-
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
50-
51-
unsafe { Mmap::map(file) }
47+
unsafe { Mmap::map(&path) }
5248
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))
5349
.and_then(|mmap| try_slice_owned(mmap, |mmap| f(mmap)))
5450
}

compiler/rustc_codegen_ssa/src/back/write.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2142,11 +2142,9 @@ pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
21422142
) {
21432143
let filename = pre_lto_bitcode_filename(&module.name);
21442144
let bc_path = in_incr_comp_dir_sess(tcx.sess, &filename);
2145-
let file = fs::File::open(&bc_path)
2146-
.unwrap_or_else(|e| panic!("failed to open bitcode file `{}`: {}", bc_path.display(), e));
21472145

21482146
let mmap = unsafe {
2149-
Mmap::map(file).unwrap_or_else(|e| {
2147+
Mmap::map(&bc_path).unwrap_or_else(|e| {
21502148
panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e)
21512149
})
21522150
};

compiler/rustc_data_structures/src/memmap.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fs::File;
22
use std::io;
33
use std::ops::{Deref, DerefMut};
4+
use std::path::Path;
45

56
/// A trivial wrapper for [`memmap2::Mmap`] (or `Vec<u8>` on WASM).
67
#[cfg(not(any(miri, target_arch = "wasm32")))]
@@ -17,27 +18,24 @@ impl Mmap {
1718
///
1819
/// However in practice most callers do not ensure this, so uses of this function are likely unsound.
1920
#[inline]
20-
pub unsafe fn map(file: File) -> io::Result<Self> {
21+
pub unsafe fn map(path: impl AsRef<Path>) -> io::Result<Self> {
22+
let file = File::open(path)?;
2123
// By default, memmap2 creates shared mappings, implying that we could see updates to the
2224
// file through the mapping. That would violate our precondition; so by requesting a
2325
// map_copy_read_only we do not lose anything.
2426
// This mapping mode also improves our support for filesystems such as cacheless virtiofs.
2527
// For more details see https://github.com/rust-lang/rust/issues/122262
2628
//
2729
// SAFETY: The caller must ensure that this is safe.
28-
unsafe { memmap2::MmapOptions::new().map_copy_read_only(&file).map(Mmap) }
30+
unsafe { Ok(Self(memmap2::MmapOptions::new().map_copy_read_only(&file)?)) }
2931
}
3032
}
3133

3234
#[cfg(any(miri, target_arch = "wasm32"))]
3335
impl Mmap {
3436
#[inline]
35-
pub unsafe fn map(mut file: File) -> io::Result<Self> {
36-
use std::io::Read;
37-
38-
let mut data = Vec::new();
39-
file.read_to_end(&mut data)?;
40-
Ok(Mmap(data))
37+
pub unsafe fn map(path: impl AsRef<Path>) -> io::Result<Self> {
38+
Ok(Mmap(std::fs::read(path)?))
4139
}
4240
}
4341

compiler/rustc_incremental/src/persist/file_format.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,17 @@ pub(crate) fn read_file(
9595
is_nightly_build: bool,
9696
cfg_version: &'static str,
9797
) -> io::Result<Option<(Mmap, usize)>> {
98-
let file = match fs::File::open(path) {
99-
Ok(file) => file,
100-
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
101-
Err(err) => return Err(err),
102-
};
10398
// SAFETY: This process must not modify nor remove the backing file while the memory map lives.
10499
// For the dep-graph and the work product index, it is as soon as the decoding is done.
105100
// For the query result cache, the memory map is dropped in save_dep_graph before calling
106101
// save_in and trying to remove the backing file.
107102
//
108103
// There is no way to prevent another process from modifying this file.
109-
let mmap = unsafe { Mmap::map(file) }?;
104+
let mmap = match unsafe { Mmap::map(path) } {
105+
Ok(file) => file,
106+
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
107+
Err(err) => return Err(err),
108+
};
110109

111110
let mut file = io::Cursor::new(&*mmap);
112111

compiler/rustc_metadata/src/locator.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -822,13 +822,7 @@ fn get_metadata_section<'p>(
822822
}
823823
CrateFlavor::Rmeta => {
824824
// mmap the file, because only a small fraction of it is read.
825-
let file = std::fs::File::open(filename).map_err(|_| {
826-
MetadataError::LoadFailure(format!(
827-
"failed to open rmeta metadata: '{}'",
828-
filename.display()
829-
))
830-
})?;
831-
let mmap = unsafe { Mmap::map(file) };
825+
let mmap = unsafe { Mmap::map(filename) };
832826
let mmap = mmap.map_err(|_| {
833827
MetadataError::LoadFailure(format!(
834828
"failed to mmap rmeta metadata: '{}'",

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2245,12 +2245,10 @@ pub struct EncodedMetadata {
22452245
impl EncodedMetadata {
22462246
#[inline]
22472247
pub fn from_path(path: PathBuf, temp_dir: Option<MaybeTempDir>) -> std::io::Result<Self> {
2248-
let file = std::fs::File::open(&path)?;
2249-
let file_metadata = file.metadata()?;
2250-
if file_metadata.len() == 0 {
2248+
if std::fs::metadata(&path)?.len() == 0 {
22512249
return Ok(Self { mmap: None, _temp_dir: None });
22522250
}
2253-
let mmap = unsafe { Some(Mmap::map(file)?) };
2251+
let mmap = unsafe { Some(Mmap::map(path)?) };
22542252
Ok(Self { mmap, _temp_dir: temp_dir })
22552253
}
22562254

0 commit comments

Comments
 (0)