Repository navigation
perf(read): eliminate redundant clone and heap allocations in safe_prepare_path - #1224
Merged
Pr0methean merged 2 commits intoOct 30, 2026
Merged
Pr0methean merged 2 commits into
Pr0methean merged 2 commits into
Conversation
…epare_path - Eliminate redundant `directory.clone()` on every iteration in `extract_with_options` and `extract_parallel`, removing the TODO in `extract.rs`. - Refactor `safe_prepare_path` to return `ZipResult<PathBuf>` directly by value. - Generalize `resolve_enclosed` to accept `impl IntoIterator<Item = impl Into<Cow<'a, OsStr>>>`, avoiding `OsString` allocations for borrowed path components. - Update callers in `stream.rs` and `extract.rs` to pass borrowed components directly.
Pr0methean
approved these changes
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
This PR eliminates redundant heap allocations and unnecessary
PathBufcloning on the hot extraction path acrossZipArchive::extract_with_options,ZipArchive::extract_parallel, andZipStreamReader.1. Eliminated Redundant
directory.clone()and Resolved TODO inextract.rsPreviously, on every file entry iteration, the caller cloned
directoryinto a mutableoutpath, only forsafe_prepare_pathto immediately drop and overwrite*outpathwith the result ofresolve_enclosed:This resulted in$O(N)$ unnecessary allocations for an archive with $N$ entries.
We resolved this by:
safe_prepare_pathto returnZipResult<PathBuf>directly by value, matching standard ownership conventions.directory.clone()in both sequential and parallel extraction loops:2. Zero-Allocation Component Resolution in
resolve_enclosedcomponentsinresolve_enclosedfromimpl IntoIterator<Item = OsString>toimpl IntoIterator<Item = impl Into<Cow<'a, OsStr>>>.VecDeque<Cow<'a, OsStr>>insideresolve_enclosed.&[&OsStr]are now queued asCow::Borrowedwith zeroOsStringheap allocations.Cow::Borrowedfor"..",Cow::Ownedfor normal symlink components).stream.rsandextract.rsnow pass borrowed component iterators directly without calling.to_os_string().Verification and Test Evidence
cargo clippy --all-targetspasses with 0 warnings and 0 errors.All relevant test suites pass cleanly:
cargo test --test extraction(4 passed, 0 failed)cargo test --test extract_size_limit(4 passed, 0 failed)cargo test --test invalid_path(5 passed, 0 failed)cargo test --test extract_parallel --features rayon(9 passed, 0 failed)cargo check --benches(0 errors)cargo check --examples(0 errors)Path containment checks, symlink traversal boundaries (
MAX_SYMLINK_HOPS), Windows DOS device name sanitization, and NTFS ADS colon checks remain completely intact and unaffected.Need help on this PR? Tag
@codesmith-botwith what you need.