3

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?

Martin
  • 381
  • 4
  • 17
  • 3
    I don't think there's any provided way of doing so, but if you're insistent you could compile a modified libstd and link against that? – eggyal Mar 02 '22 at 17:36

0 Answers0