diff --git a/Cargo.toml b/Cargo.toml index 4caca8f8d..3b7332188 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,6 +77,7 @@ sha3 = { version = "0.10.8", default-features = false } # maps hashbrown = { version = "0.15", default-features = false } indexmap = { version = "2.5", default-features = false } +foldhash = { version = "0.1", default-features = false } rustc-hash = { version = "2.0", default-features = false } # misc diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index bfb9a2a21..1a5be450d 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -73,6 +73,7 @@ hashbrown = { workspace = true, optional = true, features = [ "inline-more", ] } indexmap = { workspace = true, optional = true } +foldhash = { workspace = true, optional = true } rustc-hash = { workspace = true, optional = true } # arbitrary @@ -94,12 +95,13 @@ criterion.workspace = true serde_json.workspace = true [features] -default = ["std", "map-fxhash"] +default = ["std", "map-foldhash"] std = [ "bytes/std", "hex/std", "ruint/std", "alloy-rlp?/std", + "foldhash?/std", "indexmap?/std", "k256?/std", "keccak-asm?/std", @@ -124,6 +126,7 @@ tiny-keccak = [] map = ["dep:hashbrown"] map-hashbrown = ["map"] map-indexmap = ["map", "dep:indexmap"] +map-foldhash = ["map", "dep:foldhash"] map-fxhash = ["map", "dep:rustc-hash"] getrandom = ["dep:getrandom"] diff --git a/crates/primitives/src/map/fixed.rs b/crates/primitives/src/map/fixed.rs index 52a62f56b..6e1af0a10 100644 --- a/crates/primitives/src/map/fixed.rs +++ b/crates/primitives/src/map/fixed.rs @@ -196,6 +196,7 @@ mod tests { } #[test] + #[cfg_attr(miri, ignore = "foldhash queries time (orlp/foldhash#4)")] fn fb_hasher() { // Just by running it once we test that it compiles and that debug assertions are correct. ruint::const_for!(N in [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, @@ -206,6 +207,7 @@ mod tests { } #[test] + #[cfg_attr(miri, ignore = "foldhash queries time (orlp/foldhash#4)")] fn map() { let mut map = AddressHashMap::::default(); map.insert(Address::ZERO, true); diff --git a/crates/primitives/src/map/mod.rs b/crates/primitives/src/map/mod.rs index 7908db690..951fc12aa 100644 --- a/crates/primitives/src/map/mod.rs +++ b/crates/primitives/src/map/mod.rs @@ -20,6 +20,10 @@ //! resistance should enable the "rand" feature so that the hasher is initialized using a random //! seed. //! +//! Note that using the types provided in this module may require using different APIs than the +//! standard library as they might not be generic over the hasher state, such as using +//! `HashMap::default()` instead of `HashMap::new()`. +//! //! [fb]: crate::FixedBytes //! [`Selector`]: crate::Selector //! [`Address`]: crate::Address @@ -53,7 +57,7 @@ pub type HashMap = imp::HashMap; /// See [`HashSet`](imp::HashSet) for more information. pub type HashSet = imp::HashSet; -// Faster hasher. +// Faster hashers. cfg_if! { if #[cfg(feature = "map-fxhash")] { #[doc(no_inline)] @@ -111,7 +115,9 @@ cfg_if! { // Default hasher. cfg_if! { - if #[cfg(feature = "map-fxhash")] { + if #[cfg(feature = "map-foldhash")] { + type DefaultHashBuilderInner = foldhash::fast::RandomState; + } else if #[cfg(feature = "map-fxhash")] { type DefaultHashBuilderInner = FxBuildHasher; } else if #[cfg(any(feature = "map-hashbrown", not(feature = "std")))] { type DefaultHashBuilderInner = hashbrown::DefaultHashBuilder; @@ -150,6 +156,7 @@ mod tests { use super::*; #[test] + #[cfg_attr(miri, ignore = "foldhash queries time (orlp/foldhash#4)")] fn default_hasher_builder_traits() { let hash_builder = ::default(); let _hash_builder2 = ::clone(&hash_builder); diff --git a/tests/core-sol/src/lib.rs b/tests/core-sol/src/lib.rs index a5029c1d1..72a4cafdb 100644 --- a/tests/core-sol/src/lib.rs +++ b/tests/core-sol/src/lib.rs @@ -65,6 +65,7 @@ sol! { } #[test] +#[cfg_attr(miri, ignore = "foldhash queries time (orlp/foldhash#4)")] fn do_stuff() { let mut set = alloy_core::primitives::map::HashSet::::default(); set.insert(Default::default());