Skip to content

Commit 0f1c936

Browse files
authored
Move hashing functionality (#10)
1 parent 91b6ab4 commit 0f1c936

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/constraint.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::collections::hash_map::Entry;
44
use std::hash::Hash;
55

66
use parking_lot::RwLock;
7-
use siphasher::sip128::{Hasher128, SipHasher13};
87

98
use crate::accelerate;
109

@@ -34,7 +33,7 @@ impl<T: Call> ImmutableConstraint<T> {
3433
/// Enter a constraint for a call to an immutable function.
3534
#[inline]
3635
pub fn push(&self, call: T, ret_hash: u128) {
37-
let call_hash = hash(&call);
36+
let call_hash = crate::hash::hash(&call);
3837
let entry = ConstraintEntry { call, call_hash, ret_hash };
3938
self.0.write().push_inner(Cow::Owned(entry));
4039
}
@@ -103,7 +102,7 @@ impl<T: Call> MutableConstraint<T> {
103102
/// Enter a constraint for a call to a mutable function.
104103
#[inline]
105104
pub fn push(&self, call: T, ret_hash: u128) {
106-
let call_hash = hash(&call);
105+
let call_hash = crate::hash::hash(&call);
107106
let entry = ConstraintEntry { call, call_hash, ret_hash };
108107
self.0.write().push_inner(Cow::Owned(entry));
109108
}
@@ -271,14 +270,6 @@ impl<T: Call> Join for MutableConstraint<T> {
271270
}
272271
}
273272

274-
/// Produce a 128-bit hash of a value.
275-
#[inline]
276-
pub fn hash<T: Hash>(value: &T) -> u128 {
277-
let mut state = SipHasher13::new();
278-
value.hash(&mut state);
279-
state.finish128().as_u128()
280-
}
281-
282273
/// Check for a constraint violation.
283274
#[inline]
284275
#[track_caller]

src/prehashed.rs renamed to src/hash.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ use std::ops::Deref;
66

77
use siphasher::sip128::{Hasher128, SipHasher13};
88

9+
/// Produce a 128-bit hash of a value.
10+
#[inline]
11+
pub fn hash<T: Hash>(value: &T) -> u128 {
12+
let mut state = SipHasher13::new();
13+
value.hash(&mut state);
14+
state.finish128().as_u128()
15+
}
16+
17+
/// Produce a 128-bit hash of a value's type and the value.
18+
///
19+
/// Prefer this over `hash` if the resulting hash is used for the same purpose
20+
/// as hashes of other types.
21+
#[inline]
22+
pub fn hash_any<T: Hash + 'static>(item: &T) -> u128 {
23+
// Also hash the TypeId because the type might be converted
24+
// through an unsized coercion.
25+
let mut state = SipHasher13::new();
26+
item.type_id().hash(&mut state);
27+
item.hash(&mut state);
28+
state.finish128().as_u128()
29+
}
30+
931
/// A wrapper type with precomputed hash.
1032
///
1133
/// This is useful if you want to pass large values of `T` to memoized
@@ -37,7 +59,7 @@ impl<T: Hash + 'static> Prehashed<T> {
3759
/// Compute an item's hash and wrap it.
3860
#[inline]
3961
pub fn new(item: T) -> Self {
40-
Self { hash: hash(&item), item }
62+
Self { hash: hash_any(&item), item }
4163
}
4264

4365
/// Return the wrapped value.
@@ -53,21 +75,11 @@ impl<T: Hash + 'static> Prehashed<T> {
5375
F: FnOnce(&mut T) -> U,
5476
{
5577
let output = f(&mut self.item);
56-
self.hash = hash(&self.item);
78+
self.hash = hash_any(&self.item);
5779
output
5880
}
5981
}
6082

61-
/// Hash the item.
62-
fn hash<T: Hash + 'static>(item: &T) -> u128 {
63-
// Also hash the TypeId because the type might be converted
64-
// through an unsized coercion.
65-
let mut state = SipHasher13::new();
66-
item.type_id().hash(&mut state);
67-
item.hash(&mut state);
68-
state.finish128().as_u128()
69-
}
70-
7183
impl<T: ?Sized> Deref for Prehashed<T> {
7284
type Target = T;
7385

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ For the full example see [`examples/calc.rs`][calc].
8585
mod accelerate;
8686
mod cache;
8787
mod constraint;
88+
mod hash;
8889
mod input;
89-
mod prehashed;
9090
mod track;
9191

9292
pub use crate::cache::evict;
93-
pub use crate::prehashed::Prehashed;
93+
pub use crate::hash::Prehashed;
9494
pub use crate::track::{Track, Tracked, TrackedMut, Validate};
9595
pub use comemo_macros::{memoize, track};
9696

@@ -100,7 +100,8 @@ pub mod internal {
100100
pub use parking_lot::RwLock;
101101

102102
pub use crate::cache::{Cache, CacheData, memoized, register_evictor};
103-
pub use crate::constraint::{Call, ImmutableConstraint, MutableConstraint, hash};
103+
pub use crate::constraint::{Call, ImmutableConstraint, MutableConstraint};
104+
pub use crate::hash::hash;
104105
pub use crate::input::{Args, Input, assert_hashable_or_trackable};
105106
pub use crate::track::{Surfaces, to_parts_mut_mut, to_parts_mut_ref, to_parts_ref};
106107

0 commit comments

Comments
 (0)