Skip to content

Commit cebe9fc

Browse files
authored
perf: mark error path functions as #[cold] for better optimization (#729)
1 parent 449e419 commit cebe9fc

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl ResolveError {
125125
matches!(self, Self::Ignored(_))
126126
}
127127

128+
#[cold]
128129
#[must_use]
129130
pub fn from_serde_json_error(path: PathBuf, error: &serde_json::Error) -> Self {
130131
Self::Json(JSONError {
@@ -163,13 +164,15 @@ impl PartialEq for IOError {
163164
}
164165

165166
impl From<IOError> for io::Error {
167+
#[cold]
166168
fn from(error: IOError) -> Self {
167169
let io_error = error.0.as_ref();
168170
Self::new(io_error.kind(), io_error.to_string())
169171
}
170172
}
171173

172174
impl From<io::Error> for ResolveError {
175+
#[cold]
173176
fn from(err: io::Error) -> Self {
174177
Self::IOError(IOError(Arc::new(err)))
175178
}
@@ -191,6 +194,7 @@ impl Display for CircularPathBufs {
191194
}
192195

193196
impl From<Vec<PathBuf>> for CircularPathBufs {
197+
#[cold]
194198
fn from(value: Vec<PathBuf>) -> Self {
195199
Self(value)
196200
}

src/file_system.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ impl FileSystemOs {
144144
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
145145
if simdutf8::basic::from_utf8(&bytes).is_err() {
146146
// Same error as `fs::read_to_string` produces (`io::Error::INVALID_UTF8`)
147-
return Err(io::Error::new(
148-
io::ErrorKind::InvalidData,
149-
"stream did not contain valid UTF-8",
150-
));
147+
#[cold]
148+
fn invalid_utf8_error() -> io::Error {
149+
io::Error::new(io::ErrorKind::InvalidData, "stream did not contain valid UTF-8")
150+
}
151+
return Err(invalid_utf8_error());
151152
}
152153
// SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
153154
Ok(unsafe { String::from_utf8_unchecked(bytes) })

src/specifier.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ impl<'a> Specifier<'a> {
1616

1717
pub fn parse(specifier: &'a str) -> Result<Self, SpecifierError> {
1818
if specifier.is_empty() {
19-
return Err(SpecifierError::Empty(specifier.to_string()));
19+
#[cold]
20+
fn empty_specifier_error(specifier: &str) -> SpecifierError {
21+
SpecifierError::Empty(specifier.to_string())
22+
}
23+
return Err(empty_specifier_error(specifier));
2024
}
2125
let offset = match specifier.as_bytes()[0] {
2226
b'/' | b'.' | b'#' => 1,
2327
_ => 0,
2428
};
2529
let (path, query, fragment) = Self::parse_query_fragment(specifier, offset);
2630
if path.is_empty() {
27-
return Err(SpecifierError::Empty(specifier.to_string()));
31+
#[cold]
32+
fn empty_path_error(specifier: &str) -> SpecifierError {
33+
SpecifierError::Empty(specifier.to_string())
34+
}
35+
return Err(empty_path_error(specifier));
2836
}
2937
Ok(Self { path, query, fragment })
3038
}

src/windows/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ pub fn strip_windows_prefix(path: PathBuf) -> Result<PathBuf, ResolveError> {
3333
// \\?\BootPartition\
3434
// It seems nodejs does not support DOS device paths with Volume GUIDs.
3535
// This can happen if the path points to a Mounted Volume without a drive letter.
36-
return Err(ResolveError::PathNotSupported(path));
36+
#[cold]
37+
fn unsupported_path_error(path: PathBuf) -> ResolveError {
38+
ResolveError::PathNotSupported(path)
39+
}
40+
return Err(unsupported_path_error(path));
3741
}
3842
// SAFETY: `as_encoded_bytes` ensures `p` is valid path bytes
3943
unsafe { PathBuf::from(std::ffi::OsStr::from_encoded_bytes_unchecked(p)) }

0 commit comments

Comments
 (0)