Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Using SipHashes from crates.io #2778

Merged
merged 2 commits into from
Oct 21, 2016
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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions util/bloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ license = "GPL3"

[lib]
path = "src/lib.rs"

[dependencies]
siphasher = "0.1.1"
28 changes: 16 additions & 12 deletions util/bloom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.


extern crate siphasher;

use std::cmp;
use std::mem;
use std::f64;
use std::hash::{Hash, Hasher, SipHasher};
use std::hash::{Hash, Hasher};
use std::collections::HashSet;
use siphasher::sip::SipHasher;

// TODO [ToDr] Both hashers are exactly the same - no point to keep two.
const NUMBER_OF_HASHERS: usize = 2;

/// BitVec structure with journalling
/// Every time any of the blocks is getting set it's index is tracked
Expand Down Expand Up @@ -73,7 +80,8 @@ pub struct Bloom {
bitmap: BitVecJournal,
bitmap_bits: u64,
k_num: u32,
sips: [SipHasher; 2],
// TODO [ToDr] Both hashers are exactly the same - no point to keep two.
sips: [SipHasher; NUMBER_OF_HASHERS],
}

impl Bloom {
Expand All @@ -85,7 +93,7 @@ impl Bloom {
let bitmap_bits = (bitmap_size as u64) * 8u64;
let k_num = Bloom::optimal_k_num(bitmap_bits, items_count);
let bitmap = BitVecJournal::new(bitmap_bits as usize);
let sips = [Bloom::sip_new(), Bloom::sip_new()];
let sips = [SipHasher::new(), SipHasher::new()];
Bloom {
bitmap: bitmap,
bitmap_bits: bitmap_bits,
Expand All @@ -99,7 +107,7 @@ impl Bloom {
let bitmap_size = parts.len() * 8;
let bitmap_bits = (bitmap_size as u64) * 8u64;
let bitmap = BitVecJournal::from_parts(parts);
let sips = [Bloom::sip_new(), Bloom::sip_new()];
let sips = [SipHasher::new(), SipHasher::new()];
Bloom {
bitmap: bitmap,
bitmap_bits: bitmap_bits,
Expand Down Expand Up @@ -170,12 +178,12 @@ impl Bloom {
cmp::max(k_num, 1)
}

fn bloom_hash<T>(&self, hashes: &mut [u64; 2], item: &T, k_i: u32) -> u64
fn bloom_hash<T>(&self, hashes: &mut [u64; NUMBER_OF_HASHERS], item: &T, k_i: u32) -> u64
where T: Hash
{
if k_i < 2 {
let sip = &mut self.sips[k_i as usize].clone();
item.hash(sip);
if k_i < NUMBER_OF_HASHERS as u32 {
let mut sip = self.sips[k_i as usize].clone();
item.hash(&mut sip);
let hash = sip.finish();
hashes[k_i as usize] = hash;
hash
Expand All @@ -184,10 +192,6 @@ impl Bloom {
}
}

fn sip_new() -> SipHasher {
SipHasher::new()
}

/// Drains the bloom journal returning the updated bloom part
pub fn drain_journal(&mut self) -> BloomJournal {
BloomJournal {
Expand Down