Martin
Martin

Reputation: 906

How to force a whole crate to use a deterministic hasher (even dependencies)?

I have a fairly large Rust crate with some dependencies. Now, I want to implement some benchmarks. To get the most stable measurements possible, I'm using cachegrind with bheisler's iai.

Even after disabling ASLR, there remains some jitter in the measurements. This most likely stems from the usage of HashMaps whose hashers are randomly seeded. I know that I can initialize the HashMap with my own hasher and seed that with a pre-set value, but some of my dependencies, like serde, contain HashMaps on their own, so that is not a comprehensive solution. In addition, writing something like the below code to initialize hash maps is a bit ... much:

use highway::{HighwayBuildHasher, Key};
use std::collections::HashMap;

let high = HighwayBuildHasher::new(Key([0123, 4567, 8901, 2345]));
let mut map: HashMap<u8, u8, HighwayBuildHasher> = HashMap::with_hasher(high);

How do I eliminate any inherent non-deterministic behavior from the standard library? Is there any way to specify a default hasher, or at least default random seed, for the Rust standard library that affects a whole crate, including its dependencies?

Upvotes: 3

Views: 457

Answers (0)

Related Questions