Skip to content

Commit 3575732

Browse files
committed
Improves performances
1 parent cf92ed4 commit 3575732

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/fs.rs

Lines changed: 25 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,37 @@ 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 {
305+
base_path,
306+
virtual_segments,
307+
}));
298308
}
299309

300-
Ok(VPath::Native(PathBuf::from(base_path)))
310+
Ok(VPath::Native(PathBuf::from(normalized_path)))
301311
}
302312

303313
#[cfg(test)]

0 commit comments

Comments
 (0)