Skip to content

Commit

Permalink
Merge pull request #87 from FlickerSoul/__hash__
Browse files Browse the repository at this point in the history
Fix hashing overflow issues (#86)
  • Loading branch information
Julian authored Oct 31, 2024
2 parents 53d1976 + edc02d6 commit 06d98b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 12 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ def tests(session):
"""
Run the test suite with a corresponding Python version.
"""
session.install("-r", REQUIREMENTS["tests"])
# Really we want --profile=test here (for
# https://github.com/crate-py/rpds/pull/87#issuecomment-2291409297)
# but it produces strange symbol errors saying:
# dynamic module does not define module export function (PyInit_rpds)
# so OK, dev it is.
session.install(
"--config-settings",
"build-args=--profile=dev",
"--no-cache",
"-r",
REQUIREMENTS["tests"],
)

if session.posargs and session.posargs[0] == "coverage":
if len(session.posargs) > 1 and session.posargs[1] == "github":
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

fn hash_shuffle_bits(h: usize) -> usize {
((h ^ 89869747) ^ (h << 16)) * 3644798167
((h ^ 89869747) ^ (h << 16)).wrapping_mul(3644798167)
}

#[derive(Debug)]
Expand Down Expand Up @@ -217,11 +217,11 @@ impl HashTrieMapPy {
})?;

// factor in the number of entries in the collection
hash_val ^= (self.inner.size() + 1) * 1927868237;
hash_val ^= self.inner.size().wrapping_add(1).wrapping_mul(1927868237);

// dispense patterns in the hash value
hash_val ^= (hash_val >> 11) ^ (hash_val >> 25);
hash_val = hash_val * 69069 + 907133923;
hash_val = hash_val.wrapping_mul(69069).wrapping_add(907133923);

Ok(hash_val as isize)
}
Expand Down Expand Up @@ -833,11 +833,11 @@ impl HashTrieSetPy {
.fold(0, |acc: usize, x: usize| acc ^ hash_shuffle_bits(x));

// factor in the number of entries in the collection
hash_val ^= (self.inner.size() + 1) * 1927868237;
hash_val ^= self.inner.size().wrapping_add(1).wrapping_mul(1927868237);

// dispense patterns in the hash value
hash_val ^= (hash_val >> 11) ^ (hash_val >> 25);
hash_val = hash_val * 69069 + 907133923;
hash_val = hash_val.wrapping_mul(69069).wrapping_add(907133923);

Ok(hash_val as isize)
}
Expand Down

0 comments on commit 06d98b9

Please sign in to comment.