如何强制整个板条箱使用确定性哈希器(甚至依赖项)?

发布于 2025-01-11 04:22:33 字数 1103 浏览 1 评论 0原文

我有一个相当大的 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文