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

Fix hashing overflow issues (#86) #87

Merged
merged 2 commits into from
Oct 31, 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
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
Loading