Skip to content

Commit 9eeb3bb

Browse files
authored
Revert "perf: use memmap to speed up file reading" (#701)
Reverts #696 Performance regression shown in * rolldown/rolldown#6224
1 parent ae725f4 commit 9eeb3bb

File tree

3 files changed

+6
-67
lines changed

3 files changed

+6
-67
lines changed

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ pnp = { version = "0.12.2", optional = true }
9494

9595
document-features = { version = "0.2.11", optional = true }
9696

97-
[target.'cfg(not(any(target_family = "wasm", target_os = "wasi")))'.dependencies]
98-
memmap2 = "0.9"
99-
10097
[target.'cfg(target_os = "windows")'.dependencies]
10198
windows = { version = "0.62.0", features = ["Win32_Storage_FileSystem"] }
10299

src/file_system.rs

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::{
44
};
55

66
use cfg_if::cfg_if;
7-
#[cfg(not(any(target_family = "wasm", target_os = "wasi")))]
8-
use memmap2::Mmap;
97
#[cfg(feature = "yarn_pnp")]
108
use pnp::fs::{LruZipCache, VPath, VPathInfo, ZipCache};
119

@@ -126,67 +124,21 @@ pub struct FileSystemOs {
126124
}
127125

128126
impl FileSystemOs {
129-
/// Memory-mapped file reading threshold in bytes
130-
#[cfg(not(any(target_family = "wasm", target_os = "wasi")))]
131-
const MMAP_THRESHOLD: u64 = 4096;
132-
133-
/// Validates UTF-8 encoding and converts bytes to String
134-
///
135127
/// # Errors
136128
///
137-
/// Returns an error if the bytes are not valid UTF-8
138-
fn validate_and_convert_utf8(bytes: &[u8]) -> io::Result<String> {
139-
if simdutf8::basic::from_utf8(bytes).is_err() {
129+
/// See [std::fs::read_to_string]
130+
pub fn read_to_string(path: &Path) -> io::Result<String> {
131+
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
132+
let bytes = std::fs::read(path)?;
133+
if simdutf8::basic::from_utf8(&bytes).is_err() {
140134
// Same error as `fs::read_to_string` produces (`io::Error::INVALID_UTF8`)
141135
return Err(io::Error::new(
142136
io::ErrorKind::InvalidData,
143137
"stream did not contain valid UTF-8",
144138
));
145139
}
146140
// SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
147-
Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_string())
148-
}
149-
150-
/// # Errors
151-
///
152-
/// See [std::fs::read_to_string]
153-
pub fn read_to_string(path: &Path) -> io::Result<String> {
154-
#[cfg(not(any(target_family = "wasm", target_os = "wasi")))]
155-
{
156-
let file = std::fs::File::open(path)?;
157-
let metadata = file.metadata()?;
158-
159-
// Use memory mapping for files >= 4KB, standard read for smaller files
160-
if metadata.len() >= Self::MMAP_THRESHOLD {
161-
return Self::read_to_string_mmap(&file);
162-
}
163-
}
164-
Self::read_to_string_standard(path)
165-
}
166-
167-
/// Standard file reading implementation using std::fs::read
168-
///
169-
/// # Errors
170-
///
171-
/// See [std::fs::read_to_string]
172-
pub fn read_to_string_standard(path: &Path) -> io::Result<String> {
173-
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
174-
let bytes = std::fs::read(path)?;
175-
Self::validate_and_convert_utf8(&bytes)
176-
}
177-
178-
/// Memory-mapped file reading implementation
179-
///
180-
/// # Errors
181-
///
182-
/// See [std::fs::read_to_string] and [memmap2::Mmap::map]
183-
#[cfg(not(any(target_family = "wasm", target_os = "wasi")))]
184-
fn read_to_string_mmap(file: &std::fs::File) -> io::Result<String> {
185-
// SAFETY: memmap2::Mmap::map requires that the file remains valid and unmutated
186-
// for the lifetime of the mmap. Since we're doing read-only access and the file
187-
// won't be modified during this function's execution, this is safe.
188-
let mmap = unsafe { Mmap::map(file)? };
189-
Self::validate_and_convert_utf8(&mmap[..])
141+
Ok(unsafe { String::from_utf8_unchecked(bytes) })
190142
}
191143

192144
/// # Errors

0 commit comments

Comments
 (0)