Skip to content

Commit 32bed63

Browse files
committed
move IdentityHasher into its own file
1 parent cc9e03c commit 32bed63

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

src/serde/identity_hash.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use rand::Rng;
2+
use std::hash::{BuildHasher, Hasher};
3+
4+
#[derive(Default, Clone, Copy)]
5+
pub struct IdentityHash(u64, u64);
6+
7+
impl IdentityHash {
8+
fn new(salt: u64) -> Self {
9+
Self(0, salt)
10+
}
11+
}
12+
13+
impl Hasher for IdentityHash {
14+
fn finish(&self) -> u64 {
15+
self.0
16+
}
17+
18+
fn write(&mut self, bytes: &[u8]) {
19+
self.0 =
20+
u64::from_le_bytes(bytes[0..8].try_into().expect("expected 32 byte hashes")) ^ self.1;
21+
}
22+
23+
fn write_u64(&mut self, _i: u64) {
24+
panic!("This hasher only takes bytes");
25+
}
26+
}
27+
28+
pub struct RandomState(u64);
29+
30+
impl Default for RandomState {
31+
fn default() -> Self {
32+
let mut rng = rand::thread_rng();
33+
Self(rng.gen())
34+
}
35+
}
36+
37+
impl BuildHasher for RandomState {
38+
type Hasher = IdentityHash;
39+
40+
fn build_hasher(&self) -> Self::Hasher {
41+
IdentityHash::new(self.0)
42+
}
43+
}

src/serde/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod de;
33
mod de_br;
44
mod de_tree;
55
mod errors;
6+
mod identity_hash;
67
mod incremental;
78
mod object_cache;
89
mod parse_atom;
@@ -19,6 +20,7 @@ mod test;
1920
pub use de::node_from_bytes;
2021
pub use de_br::{node_from_bytes_backrefs, node_from_bytes_backrefs_record};
2122
pub use de_tree::{parse_triples, ParsedTriple};
23+
pub use identity_hash::RandomState;
2224
pub use incremental::{Serializer, UndoState};
2325
pub use object_cache::{serialized_length, treehash, ObjectCache};
2426
pub use ser::{node_to_bytes, node_to_bytes_limit};

src/serde/read_cache_lookup.rs

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::serde::RandomState;
12
use bitvec::prelude::*;
23
use bitvec::vec::BitVec;
3-
use rand::Rng;
44
/// When deserializing a clvm object, a stack of deserialized child objects
55
/// is created, which can be used with back-references. A `ReadCacheLookup` keeps
66
/// track of the state of this stack and all child objects under each root
@@ -19,51 +19,9 @@ use rand::Rng;
1919
///
2020
/// All hashes correspond to sha256 tree hashes.
2121
use std::collections::{HashMap, HashSet};
22-
use std::hash::{BuildHasher, Hasher};
2322

2423
use super::bytes32::{hash_blob, hash_blobs, Bytes32};
2524

26-
#[derive(Default, Clone, Copy)]
27-
pub struct IdentityHash(u64, u64);
28-
29-
impl IdentityHash {
30-
fn new(salt: u64) -> Self {
31-
Self(0, salt)
32-
}
33-
}
34-
35-
impl Hasher for IdentityHash {
36-
fn finish(&self) -> u64 {
37-
self.0
38-
}
39-
40-
fn write(&mut self, bytes: &[u8]) {
41-
self.0 =
42-
u64::from_le_bytes(bytes[0..8].try_into().expect("expected 32 byte hashes")) ^ self.1;
43-
}
44-
45-
fn write_u64(&mut self, _i: u64) {
46-
panic!("This hasher only takes bytes");
47-
}
48-
}
49-
50-
pub struct RandomState(u64);
51-
52-
impl RandomState {
53-
fn new() -> Self {
54-
let mut rng = rand::thread_rng();
55-
Self(rng.gen())
56-
}
57-
}
58-
59-
impl BuildHasher for RandomState {
60-
type Hasher = IdentityHash;
61-
62-
fn build_hasher(&self) -> Self::Hasher {
63-
IdentityHash::new(self.0)
64-
}
65-
}
66-
6725
#[derive(Debug)]
6826
pub struct ReadCacheLookup {
6927
root_hash: Bytes32,
@@ -91,9 +49,9 @@ impl ReadCacheLookup {
9149
let read_stack = Vec::with_capacity(1000);
9250
// all keys in count and parent_lookup are tree-hashes. There's no need
9351
// to hash them again for the hash map
94-
let mut count = HashMap::with_hasher(RandomState::new());
52+
let mut count = HashMap::with_hasher(RandomState::default());
9553
count.insert(root_hash, 1);
96-
let parent_lookup = HashMap::with_hasher(RandomState::new());
54+
let parent_lookup = HashMap::with_hasher(RandomState::default());
9755
Self {
9856
root_hash,
9957
read_stack,
@@ -178,8 +136,10 @@ impl ReadCacheLookup {
178136

179137
// all the values we put in this hash set are themselves sha256 hashes.
180138
// There's no point in hashing the hashes
181-
let mut seen_ids =
182-
HashSet::<&Bytes32, RandomState>::with_capacity_and_hasher(1000, RandomState::new());
139+
let mut seen_ids = HashSet::<&Bytes32, RandomState>::with_capacity_and_hasher(
140+
1000,
141+
RandomState::default(),
142+
);
183143

184144
let max_bytes_for_path_encoding = serialized_length - 2; // 1 byte for 0xfe, 1 min byte for savings
185145
let max_path_length: usize = (max_bytes_for_path_encoding.saturating_mul(8) - 1)

0 commit comments

Comments
 (0)