Skip to content

optimize hash_string character normalization with lookup table - #2

Closed
DealsBeam wants to merge 2 commits into
samwhosung:mainfrom
DealsBeam:main
Closed

optimize hash_string character normalization with lookup table#2
DealsBeam wants to merge 2 commits into
samwhosung:mainfrom
DealsBeam:main

Conversation

@DealsBeam

Copy link
Copy Markdown

What

Replaced runtime branch-based character-by-character checking and other.to_ascii_uppercase() logic in benilla_mpq::crypto::hash_string with a statically generated lookup table (NORMALIZATION_TABLE: [u32; 256]) built via const fn.

This maps every u8 byte to its normalized u32 value (converting / to \ and lowercase ASCII to uppercase, while preserving all other bytes) in $O(1)$ time with zero branching.

Why

The MPQ filesystem reader is on the critical hot path of the WoW client's asset loader, reading meshes, textures, maps, and database files.

For every single file read in a priority patch chain, up to three sequential search and resolve checks are performed per archive (e.g., checking if it contains the file, verifying delete-markers, and finally reading it).
This results in 9-15 separate calls to hash_string per individual file lookup.
In the original implementation, each character in the path underwent dynamic conditional matches (match b { b'/' => ... }) and case-conversion checks, introducing branch-prediction penalties in high-frequency loops.

Impact

Completely eliminates branching, conditional match blocks, and dynamic method calls within the character loop of hash_string.
Rust's compiler optimizes away bounds checks for NORMALIZATION_TABLE[b as usize] because b is a u8 (guaranteed range of 0..=255) and the array size is exactly 256, resulting in highly-efficient, instruction-level lookup.
Significantly reduces CPU cycles spent on file path hashing during runtime streaming of WoW assets.

Measurement / Verification

Run cargo test -p benilla-mpq to verify correctness of the string hashing, case-insensitivity, and encryption/decryption roundtrips.
Run cargo test -p benilla-formats to confirm all high-level asset loaders (DBC, M2, terrain maps, etc.) correctly parse and resolve assets under the optimized hashing function.
Verified that all workspace linters and format checkers pass successfully via cargo clippy -p benilla-mpq and cargo fmt --all -- --check.

google-labs-jules Bot and others added 2 commits August 3, 2026 18:55
Optimize MPQ `hash_string` character normalization with a
pre-computed static 256-element lookup table. This avoids
dynamic ASCII checks and branch mispredictions.

Co-authored-by: DealsBeam <17716841+DealsBeam@users.noreply.github.com>
…4889550630177

⚡ Bolt: optimize hash_string with lookup table
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

Thanks for the interest! benilla is a solo project and isn't taking pull requests for now — please bring bug reports and ideas to the Discord instead: https://discord.gg/wJSJx467G4

@github-actions github-actions Bot closed this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant