Skip to content

Commit

Permalink
optimization: Cache fxhash instance after seeding instead of reseedin…
Browse files Browse the repository at this point in the history
…g every time
  • Loading branch information
koraa committed Jan 19, 2020
1 parent c2658a9 commit fd6323c
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashSet, HashMap, hash_map};
use std::hash::{Hash, Hasher, BuildHasher};
use std::io::{stdin, Read, BufRead, BufReader, stdout, Write, BufWriter, ErrorKind};
use std::{slice, default::Default, marker::PhantomData};
use std::{slice, default::Default, marker::PhantomData, clone::Clone};
use sysconf::page::pagesize;
use anyhow::Result;
use clap::{Arg, App};
Expand Down Expand Up @@ -46,12 +46,11 @@ impl<H: Hasher + Default> BuildHasher for BuildDefaultHasher<H> {
/// cryptographically secure RNG at creation
/// and supplying that as the first value to hash when
/// creating Hashers
struct BuildRandomStateHasher<H: Hasher + Default> {
seed: u64,
_phantom: PhantomData<H>
struct BuildRandomStateHasher<H: Hasher + Default + Clone> {
hasher: H
}

impl<H: Hasher + Default> BuildRandomStateHasher<H> {
impl<H: Hasher + Default + Clone> BuildRandomStateHasher<H> {
fn new() -> Self {
let mut buf = [0u8; 8];

Expand All @@ -69,20 +68,18 @@ impl<H: Hasher + Default> BuildRandomStateHasher<H> {
cnt += 1;
}

BuildRandomStateHasher {
seed: u64::from_ne_bytes(buf),
_phantom: PhantomData
}
let mut hasher = H::default();
hasher.write(&buf);

BuildRandomStateHasher { hasher }
}
}

impl<H: Hasher + Default> BuildHasher for BuildRandomStateHasher<H> {
impl<H: Hasher + Default + Clone> BuildHasher for BuildRandomStateHasher<H> {
type Hasher = H;

fn build_hasher(&self) -> Self::Hasher {
let mut r = H::default();
self.seed.hash(&mut r);
r
self.hasher.clone()
}
}

Expand Down

0 comments on commit fd6323c

Please sign in to comment.