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

Provide public API for initial seed of FxHasher #15

Closed
hniksic opened this issue Nov 29, 2020 · 1 comment · Fixed by #22
Closed

Provide public API for initial seed of FxHasher #15

hniksic opened this issue Nov 29, 2020 · 1 comment · Fixed by #22

Comments

@hniksic
Copy link

hniksic commented Nov 29, 2020

FxHasher can currently only be created with the 0 value for hash. It would be useful to have a constructor that constructs it with the explicitly provided seed.

As described in #14, certain operations on FxHashMap could be made faster by making the map unstable, i.e. changing the seed of the hash function for each new map. However, rustc_hash::FxHasher doesn't offer a way to create the hasher with the specified initial hash value. As a workaround, we can use write_u32() to modify the value:

// BuildHasherRandom is created with random state, like RandomState in the
// stlib - https://bit.ly/39pEvnN
#[derive(Debug, Clone)]
struct BuildHasherRandom(u32);

impl BuildHasher for BuildHasherRandom {
    type Hasher = FxHasher;

    fn build_hasher(&self) -> FxHasher {
        // workaround: create `FxHasher` with the random seed from self.0
        let mut hasher = FxHasher::default();
        hasher.write_u32(self.0);
        hasher
    }
}

It's not obvious that the compiler is able to optimize write_u32(n) with a known starting value of hasher.hash. Ideally build_hasher() should be able to explicitly provide the seed to the FxHasher constructor, such as:

    fn build_hasher(&self) -> FxHasher {
        FxHasher::from_seed(self.0)
    }

Note that we need from_seed only to implement randomization to fix #14. If an equivalent of RandomState were built into rustc_hash, we would no longer need the from_seed functionality.

@revillo
Copy link

revillo commented Apr 5, 2021

With the following code you can create FxHashMapRnd and FxHashSetRnd.

use rand::Rng;

pub struct BuildFxHasherRandom(usize);

impl BuildHasher for BuildFxHasherRandom
{
    type Hasher = FxHasher;

    fn build_hasher(&self) -> Self::Hasher {
        FxHasher::from_seed(self.0)
    }
}

impl Default for BuildFxHasherRandom
{
    fn default() -> Self
    {
        let mut thread_rng = rand::thread_rng();
        Self(thread_rng.gen())
    }
}

pub type FxHashMapRnd<K, V> = HashMap<K, V, BuildFxHasherRandom>;
pub type FxHashSetRnd<V> = HashSet<V, BuildFxHasherRandom>;

And Then:

impl FxHasher {
    #[inline]
    fn add_to_hash(&mut self, i: usize) {
        self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
    }

    fn from_seed(seed : usize) -> Self {
        Self {hash: seed}
    }
}

After benchmarking your set test case
The performance of FxHashSetRnd is 0.6s compared with 5.4s for FxHashSet!

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 a pull request may close this issue.

2 participants