Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Firefox, Naga may prefer std's default DOS-resistant hash functions #2499

Closed
jimblandy opened this issue Sep 22, 2023 · 2 comments
Closed

Comments

@jimblandy
Copy link
Member

Naga uses these types for nearly all the hash functions in the crate:

/// Hash map that is faster but not resilient to DoS attacks.
pub type FastHashMap<K, T> = rustc_hash::FxHashMap<K, T>;
/// Hash set that is faster but not resilient to DoS attacks.
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;

/// Insertion-order-preserving hash set (`IndexSet<K>`), but with the same
/// hasher as `FastHashSet<K>` (faster but not resilient to DoS attacks).
pub type FastIndexSet<K> =
    indexmap::IndexSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

These type aliases replace the standard library's hash function, designed to make std::collections::HashMap and HashSet resistant to denial-of-service attacks, with faster, more predictable hash functions.

This may not be the right tradeoff for Firefox. We should benchmark to see how much it would cost to just std::collections' default settings, and if the cost is minor, we should make FxHasher an option. (If the cost is minor enough, we should just drop FxHasher altogether...)

See also: #2498

@jrmuizel
Copy link
Contributor

The algorithm used in FxHasher originally comes from Firefox and is widely used there: https://searchfox.org/mozilla-central/rev/57f6fbd39c0b5957e11b27b4db58b821d8e1607d/mfbt/HashFunctions.h#111

I think it's probably the right default and wouldn't worry about benchmarking.

@jimblandy
Copy link
Member Author

Sounds good.

@teoxoy teoxoy closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants