Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(primitives): add and use foldhash as default hasher #763

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/map/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -206,6 +207,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore = "foldhash queries time (orlp/foldhash#4)")]
fn map() {
let mut map = AddressHashMap::<bool>::default();
map.insert(Address::ZERO, true);
Expand Down
11 changes: 9 additions & 2 deletions crates/primitives/src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +57,7 @@ pub type HashMap<K, V, S = DefaultHashBuilder> = imp::HashMap<K, V, S>;
/// See [`HashSet`](imp::HashSet) for more information.
pub type HashSet<V, S = DefaultHashBuilder> = imp::HashSet<V, S>;

// Faster hasher.
// Faster hashers.
cfg_if! {
if #[cfg(feature = "map-fxhash")] {
#[doc(no_inline)]
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = <DefaultHashBuilder as Default>::default();
let _hash_builder2 = <DefaultHashBuilder as Clone>::clone(&hash_builder);
Expand Down
1 change: 1 addition & 0 deletions tests/core-sol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MyStruct>::default();
set.insert(Default::default());
Expand Down