Brotli compression ratio depends on endianness of CPU #188

Closed
opened 2026-01-29 20:39:31 +00:00 by claunia · 0 comments
Owner

Originally created by @eustas on GitHub (Aug 2, 2017).

TL;DR: Just realized that "unaligned load + mask" technique used by some hashers will give endianness-inconsistent results.

This problem does not affect correctness, only what backward references selection and, consequently, compression ratio.

Lets say, hasher depends on 5 bytes of input. Then, it does the following: hash = magic * (load64(src) & 0xFFFFFFFFFF). On little-endian CPU the first 5 bytes of src will affect the resulting hash. On big-endian CPU, the first 3 bytes will be ignored, and next 5 bytes will be taken into account. This way, on big-endian CPU, matches of length [5..7] are likely to be missed by hasher. On the other side there will be more 8+ matches.

Originally created by @eustas on GitHub (Aug 2, 2017). TL;DR: Just realized that "unaligned load + mask" technique used by some hashers will give endianness-inconsistent results. This problem does not affect correctness, only what backward references selection and, consequently, compression ratio. Lets say, hasher depends on `5` bytes of input. Then, it does the following: `hash = magic * (load64(src) & 0xFFFFFFFFFF)`. On little-endian CPU the first `5` bytes of `src` will affect the resulting hash. On big-endian CPU, the first `3` bytes will be ignored, and next `5` bytes will be taken into account. This way, on big-endian CPU, matches of length `[5..7]` are likely to be missed by hasher. On the other side there will be more `8+` matches.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#188