Skip to content

Commit 5bbfa6b

Browse files
authored
Improves performances (#42)
* Improves performances * fmt
1 parent cf92ed4 commit 5bbfa6b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

.github/workflows/bench.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
jobs:
77
bench:
88
runs-on: ubuntu-latest
9+
permissions:
10+
pull-requests: write
911
steps:
1012
- uses: taiki-e/checkout-action@v1
1113

src/fs.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn vpath(p: &Path) -> std::io::Result<VPath> {
210210
segment_it.next();
211211
}
212212

213-
let mut base_items: Vec<&str> = Vec::new();
213+
let mut base_items: Vec<&str> = Vec::with_capacity(10);
214214

215215
let mut virtual_items: Option<Vec<&str>> = None;
216216
let mut internal_items: Option<Vec<&str>> = None;
@@ -248,13 +248,13 @@ fn vpath(p: &Path) -> std::io::Result<VPath> {
248248
}
249249

250250
virtual_items = Some(acc_segments);
251-
internal_items = Some(vec![]);
251+
internal_items = Some(Vec::with_capacity(10));
252252

253253
continue;
254254
}
255255

256256
if segment.len() > 4 && segment.ends_with(".zip") {
257-
zip_items = Some(vec![]);
257+
zip_items = Some(Vec::with_capacity(10));
258258
}
259259

260260
if let Some(virtual_segments) = &mut virtual_items {
@@ -268,14 +268,7 @@ fn vpath(p: &Path) -> std::io::Result<VPath> {
268268
}
269269
}
270270

271-
let mut base_path = base_items.join("/");
272-
273-
// Don't forget to add back the leading slash we removed earlier
274-
if normalized_relative_path != normalized_path {
275-
base_path.insert(0, '/');
276-
}
277-
278-
let virtual_info = match (virtual_items, internal_items) {
271+
let virtual_segments = match (virtual_items, internal_items) {
279272
(Some(virtual_segments), Some(internal_segments)) => {
280273
Some((virtual_segments.join("/"), internal_segments.join("/")))
281274
}
@@ -284,20 +277,34 @@ fn vpath(p: &Path) -> std::io::Result<VPath> {
284277
};
285278

286279
if let Some(zip_segments) = zip_items {
280+
let mut base_path = base_items.join("/");
281+
282+
// Don't forget to add back the leading slash we removed earlier
283+
if normalized_relative_path != normalized_path {
284+
base_path.insert(0, '/');
285+
}
286+
287287
if !zip_segments.is_empty() {
288288
return Ok(VPath::Zip(ZipInfo {
289289
base_path,
290-
virtual_segments: virtual_info,
290+
virtual_segments,
291291
zip_path: zip_segments.join("/"),
292292
}));
293293
}
294294
}
295295

296-
if let Some(virtual_info) = virtual_info {
297-
return Ok(VPath::Virtual(VirtualInfo { base_path, virtual_segments: virtual_info }));
296+
if let Some(virtual_segments) = virtual_segments {
297+
let mut base_path = base_items.join("/");
298+
299+
// Don't forget to add back the leading slash we removed earlier
300+
if normalized_relative_path != normalized_path {
301+
base_path.insert(0, '/');
302+
}
303+
304+
return Ok(VPath::Virtual(VirtualInfo { base_path, virtual_segments }));
298305
}
299306

300-
Ok(VPath::Native(PathBuf::from(base_path)))
307+
Ok(VPath::Native(PathBuf::from(normalized_path)))
301308
}
302309

303310
#[cfg(test)]

0 commit comments

Comments
 (0)