gfx-rs / naga

Universal shader translation in Rust

Home Page:https://github.com/gfx-rs/wgpu/tree/trunk/naga

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jimblandy opened this issue · comments

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

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.

Sounds good.