Skip to content

Deduplicator: xxh32 digest can collide on large ZIMs #426

Todo

Description

@i-varunsharma

Follow-up from openzim/ifixit#165, as suggested by @benoit74.

Deduplicator keys items by an xxh32 digest only (zim/dedup.py), which is 32-bit. With n deduplicated items the expected number of collisions is about n²/2³³, so roughly 1 around 100k items and ~30 around 500k. A full iFixit ZIM has a lot of images, so two different images could end up with the same digest, and the second one would silently become an alias to the wrong image.

A few options:

  • also store the content size with the digest and compare both, as benoit74 suggested. Cheap, but it only lowers the odds
  • switch to a 128 bit hash (xxhash has xxh3_128), which is about as fast and makes collisions practically impossible
  • both

My suggestion would be xxh3_128 + size as the key. I can open a PR once we agree on the approach.

Activity

  1. i-varunsharma commented on Dec 4, 2026

    @i-varunsharma
    Author

    To make the approach concrete, this is what I have in mind for Deduplicator.add_item_for:

    • hash with xxhash.xxh3_128() instead of xxh32(), for both content and fpath (still reading the file in chunks)
    • count the size while hashing
    • use size.to_bytes(8, "big") + hasher.digest() as the key

    The key stays bytes, so added_items keeps its dict[bytes, str] type and code reading it (like ifixit's test) doesn't need to change. Memory goes from 4 to 24 bytes per item, which seems fine next to the path we already store.

    For the test, I'd force a collision by monkeypatching the hash to return a constant, and check that two items of different sizes are not aliased.

    @benoit74 Does that sound good? If so I'll open a PR.

  2. benoit74 commented on Dec 4, 2026

    @benoit74
    Collaborator

    I propose that we continue using the 32 bits xxh3 and limit to 4 bytes in size.to_bytes(4, "big"); I don't expect we would hash files bigger than 4GB, and accidental collision of both size + hash looks very unlikely (this is not a security feature).

    WDYT?

  3. i-varunsharma commented on Dec 4, 2026

    @i-varunsharma
    Author

    Makes sense, size + hash should be enough here. I'll keep xxh32 (I don't think xxhash has a 32-bit xxh3 variant) and use size.to_bytes(4, "big") + digest as the key. A file over 4GB would then raise an OverflowError instead of being silently mixed up, which seems fine.

    I have exams coming up, so the PR will most likely be around Dec 20, or earlier if I find some time in between.

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