Skip to content

Commit fc9bfd1

Browse files
Copilotj178
andcommitted
Additional optimizations: avoid allocations in hot paths
- Optimize Partitions iterator to use as_os_str().len() instead of to_string_lossy().len() - Optimize normalize_path on Windows to avoid intermediate String allocation when possible Co-authored-by: j178 <[email protected]>
1 parent e79a152 commit fc9bfd1

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/fs.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,16 @@ pub(crate) fn normalize_path(path: PathBuf) -> PathBuf {
120120
*c = b'/';
121121
}
122122

123-
let os_str = OsString::from(String::from_utf8_lossy(&path).to_string());
124-
PathBuf::from(os_str)
123+
// Avoid the intermediate String allocation when possible
124+
match String::from_utf8(path) {
125+
Ok(s) => PathBuf::from(s),
126+
Err(e) => {
127+
// Fallback to lossy conversion if not valid UTF-8
128+
let path = e.into_bytes();
129+
let os_str = OsString::from(String::from_utf8_lossy(&path).into_owned());
130+
PathBuf::from(os_str)
131+
}
132+
}
125133
}
126134

127135
/// Compute a path describing `path` relative to `base`.

src/run.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl<'a> Iterator for Partitions<'a> {
107107

108108
while self.current_index < self.filenames.len() {
109109
let filename = self.filenames[self.current_index];
110-
let length = filename.to_string_lossy().len() + 1;
110+
// Avoid allocation by computing length directly
111+
let length = filename.as_os_str().len() + 1;
111112

112113
if current_length + length > self.max_cli_length
113114
|| self.current_index - start_index >= self.max_per_batch

0 commit comments

Comments
 (0)