Skip to content

Expose an exact raw-name lookup API to avoid repeated full-archive scans during candidate lookup #1208

Closed

Description

@megayu

Problem

In zip 9.1.3, index_for_name(), which is also used by by_name(), first looks up the supplied name’s bytes in the internal index. If that lookup misses, it scans every entry and compares the entry’s decoded name against the supplied string.

The fallback is useful for names encoded with CP437 and similar encodings. However, it also changes an unsuccessful lookup from expected O(1) to O(n), even when the caller only needs an exact byte match.

There does not appear to be a public API for querying the existing raw-name index without triggering this fallback scan.

A common EPUB use case

EPUB readers resolve references from an OPF manifest to entries inside the ZIP archive. These references cover chapters, images, stylesheets, and fonts.

For example, an OPF manifest may contain:

<item
    id="chapter1"
    href="Text/%E7%AC%AC%E4%B8%80%E7%AB%A0.xhtml"
    media-type="application/xhtml+xml"
/>

The actual entry name in the archive is:

OPS/Text/第一章.xhtml

The application handles relative URI resolution and percent-decoding. For compatibility with archives whose filenames contain literal percent sequences, it may try these candidates in order:

OPS/Text/%E7%AC%AC%E4%B8%80%E7%AB%A0.xhtml
OPS/Text/第一章.xhtml

With the current API:

  1. The first candidate misses the raw-name index.
  2. The library scans the entire directory. In this example, decoding the entry’s filename does not make it match the percent-encoded query.
  3. Only after that scan finishes can the application try the second candidate, which hits the raw-name index directly.

This is not a request for the ZIP library to perform URI percent-decoding. The issue is that callers cannot quickly check all exact candidates before deciding whether decoded-name fallback is appropriate.

Application impact

An extra directory scan may be inexpensive for one resource. However, EPUB readers also walk the OPF spine to calculate reading progress or build a full-text search cache.

For an archive with n entries and m resource lookups, repeatedly missing on the first candidate can result in approximately O(m × n) name lookup work. When m grows with n, this approaches O(n²).

We observed this in a synthetic EPUB containing 10,000 ZIP entries and 9,995 chapters. We compared the same application commit and Windows release-build configuration using:

  • Baseline: zip Git revision 96407e1332cb9ec8f39af88e6dac866ce83995b8
  • Candidate: crates.io zip = "=9.1.3"
  • Features: default-features = false, with chrono and deflate-flate2-zlib-rs
  • Rust: 1.98.0

Each scenario had eight samples, with the first three excluded. The remaining samples produced these medians:

Scenario Baseline 9.1.3
Plain UTF-8 references: first-open preparation 130 ms 122 ms
Percent-encoded references: first-open preparation 130 ms 2,188 ms
Plain UTF-8 references: first search and cache construction 415 ms 390 ms
Percent-encoded references: first search and cache construction 415 ms 2,385 ms

The EPUBs contained the same chapter content and resources; the relevant difference was whether the OPF references used literal Unicode characters or percent-encoding. First-open preparation included spine-length calculation, rather than complete page rendering.

These are application-level measurements, not a claim that every archive experiences the same slowdown. They illustrate the cost of repeatedly triggering the fallback during multi-candidate lookup.

Proposed API

Please consider retaining the current behavior of by_name() and index_for_name(), while exposing an exact lookup against the existing raw-name index. For example:

pub fn index_for_name_raw(&self, name: &[u8]) -> Option<usize>

Suggested semantics:

  • Query only the existing raw filename byte index.
  • Perform no filename decoding, URI decoding, or fallback scan.
  • Return None immediately on a miss.
  • Return an index usable with the existing by_index() API.
  • Document expected O(1) lookup complexity with respect to archive entry count, while accounting separately for name hashing and comparison costs.

Callers could then try their candidates without repeatedly scanning the directory. The following example uses the proposed API:

let candidates = [
    original_path.as_str(),
    decoded_path.as_str(),
];

let index = candidates
    .iter()
    .find_map(|name| archive.index_for_name_raw(name.as_bytes()));

if let Some(index) = index {
    let entry = archive.by_index(index)?;
    // Read the entry.
}

This performs at most two exact index lookups and stops at the first match. If neither candidate matches, the caller can separately decide whether to use the existing decoded-name fallback.

Exposing this capability would let applications reuse the library’s existing index instead of maintaining a duplicate one, while preserving compatibility for existing callers.

Activity

  1. Its-Just-Nans commented on Oct 28, 2026

    @Its-Just-Nans
    Member

    Related to #1080 and #1085

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions