Skip to content

perf(read): eliminate redundant clone and heap allocations in safe_prepare_path - #1224

Merged
Pr0methean merged 2 commits into
zip-rs:masterfrom
ByGh00st:perf/extract-zero-alloc-safe-prepare-path
Oct 30, 2026
Merged

Pr0methean merged 2 commits into
zip-rs:masterfrom
ByGh00st:perf/extract-zero-alloc-safe-prepare-path

Conversation

@ByGh00st

@ByGh00st ByGh00st commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

This PR eliminates redundant heap allocations and unnecessary PathBuf cloning on the hot extraction path across ZipArchive::extract_with_options, ZipArchive::extract_parallel, and ZipStreamReader.

1. Eliminated Redundant directory.clone() and Resolved TODO in extract.rs

Previously, on every file entry iteration, the caller cloned directory into a mutable outpath, only for safe_prepare_path to immediately drop and overwrite *outpath with the result of resolve_enclosed:

// Old in extract.rs:
let mut outpath = directory.clone();
/* TODO: the control flow of this method call and subsequent expectations about the
 *       values in this loop is extremely difficult to follow. It also appears to
 *       perform a nested loop upon extracting every single file entry? Why does it
 *       accept two arguments that point to the same directory path, one mutable? */
file.safe_prepare_path(directory.as_ref(), &mut outpath, root_dir.as_ref())?;

This resulted in $O(N)$ unnecessary allocations for an archive with $N$ entries.

We resolved this by:

  • Updating safe_prepare_path to return ZipResult<PathBuf> directly by value, matching standard ownership conventions.
  • Eliminating the redundant directory.clone() in both sequential and parallel extraction loops:
// New:
let outpath = file.safe_prepare_path(directory.as_ref(), root_dir.as_ref())?;

2. Zero-Allocation Component Resolution in resolve_enclosed

  • Relaxed components in resolve_enclosed from impl IntoIterator<Item = OsString> to impl IntoIterator<Item = impl Into<Cow<'a, OsStr>>>.
  • Used VecDeque<Cow<'a, OsStr>> inside resolve_enclosed.
  • Standard components borrowed from &[&OsStr] are now queued as Cow::Borrowed with zero OsString heap allocations.
  • Symlink targets continue to be queued safely (Cow::Borrowed for "..", Cow::Owned for normal symlink components).
  • Callers in stream.rs and extract.rs now pass borrowed component iterators directly without calling .to_os_string().

Verification and Test Evidence

  • Lint / Static Hygiene:
    cargo clippy --all-targets passes with 0 warnings and 0 errors.
  • Unit and Integration Tests:
    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)
  • Security Invariants:
    Path containment checks, symlink traversal boundaries (MAX_SYMLINK_HOPS), Windows DOS device name sanitization, and NTFS ADS colon checks remain completely intact and unaffected.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

…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
Pr0methean enabled auto-merge October 30, 2026 22:05
@Pr0methean
Pr0methean added this pull request to the merge queue Oct 30, 2026
Merged via the queue into zip-rs:master with commit ae0eabc Oct 30, 2026
135 checks passed
@ByGh00st
ByGh00st deleted the perf/extract-zero-alloc-safe-prepare-path branch October 30, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants