如何强制整个板条箱使用确定性哈希器(甚至依赖项)?
我有一个相当大的 Rust 箱子,有一些依赖项。现在,我想实施一些基准。 为了获得尽可能稳定的测量结果,我将cachegrind与bheisler结合使用iai
。
即使禁用 ASLR 后,测量中仍然存在一些抖动。这很可能源于 使用 HashMap
哈希器是随机播种的。我知道我可以使用自己的哈希器初始化 HashMap
并使用预设值进行播种,但我的一些依赖项,例如serde
,本身包含 HashMap
,因此这不是一个全面的解决方案。 此外,编写如下内容初始化哈希映射的代码有点......很多:
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);
如何从标准库中消除任何固有的非确定性行为?有没有办法为影响整个板条箱(包括其依赖项)的 Rust 标准库指定默认哈希器,或至少指定默认随机种子?
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 HashMap
s 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 HashMap
s 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论