Merge pull request #2778 from ethcore/siphasher

Using SipHashes from crates.io
This commit is contained in:
Robert Habermeier 2016-10-21 17:36:06 +02:00 committed by GitHub
commit 115f782935
3 changed files with 28 additions and 12 deletions

9
Cargo.lock generated
View File

@ -319,6 +319,9 @@ dependencies = [
[[package]] [[package]]
name = "ethcore-bloom-journal" name = "ethcore-bloom-journal"
version = "0.1.0" version = "0.1.0"
dependencies = [
"siphasher 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "ethcore-dapps" name = "ethcore-dapps"
@ -1568,6 +1571,11 @@ dependencies = [
"gcc 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", "gcc 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "siphasher"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.1.3" version = "0.1.3"
@ -2048,6 +2056,7 @@ dependencies = [
"checksum serde_codegen_internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f877e2781ed0a323295d1c9f0e26556117b5a11489fc47b1848dfb98b3173d21" "checksum serde_codegen_internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f877e2781ed0a323295d1c9f0e26556117b5a11489fc47b1848dfb98b3173d21"
"checksum serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e10f8a9d94b06cf5d3bef66475f04c8ff90950f1be7004c357ff9472ccbaebc" "checksum serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e10f8a9d94b06cf5d3bef66475f04c8ff90950f1be7004c357ff9472ccbaebc"
"checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c" "checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c"
"checksum siphasher 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c44e42fa187b5a8782489cf7740cc27c3125806be2bf33563cf5e02e9533fcd"
"checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e" "checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e"
"checksum slab 0.2.0 (git+https://github.com/carllerche/slab?rev=5476efcafb)" = "<none>" "checksum slab 0.2.0 (git+https://github.com/carllerche/slab?rev=5476efcafb)" = "<none>"
"checksum slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6dbdd334bd28d328dad1c41b0ea662517883d8880d8533895ef96c8003dec9c4" "checksum slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6dbdd334bd28d328dad1c41b0ea662517883d8880d8533895ef96c8003dec9c4"

View File

@ -7,3 +7,6 @@ license = "GPL3"
[lib] [lib]
path = "src/lib.rs" path = "src/lib.rs"
[dependencies]
siphasher = "0.1.1"

View File

@ -14,11 +14,18 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
extern crate siphasher;
use std::cmp; use std::cmp;
use std::mem; use std::mem;
use std::f64; use std::f64;
use std::hash::{Hash, Hasher, SipHasher}; use std::hash::{Hash, Hasher};
use std::collections::HashSet; 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 /// BitVec structure with journalling
/// Every time any of the blocks is getting set it's index is tracked /// Every time any of the blocks is getting set it's index is tracked
@ -73,7 +80,8 @@ pub struct Bloom {
bitmap: BitVecJournal, bitmap: BitVecJournal,
bitmap_bits: u64, bitmap_bits: u64,
k_num: u32, 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 { impl Bloom {
@ -85,7 +93,7 @@ impl Bloom {
let bitmap_bits = (bitmap_size as u64) * 8u64; let bitmap_bits = (bitmap_size as u64) * 8u64;
let k_num = Bloom::optimal_k_num(bitmap_bits, items_count); let k_num = Bloom::optimal_k_num(bitmap_bits, items_count);
let bitmap = BitVecJournal::new(bitmap_bits as usize); let bitmap = BitVecJournal::new(bitmap_bits as usize);
let sips = [Bloom::sip_new(), Bloom::sip_new()]; let sips = [SipHasher::new(), SipHasher::new()];
Bloom { Bloom {
bitmap: bitmap, bitmap: bitmap,
bitmap_bits: bitmap_bits, bitmap_bits: bitmap_bits,
@ -99,7 +107,7 @@ impl Bloom {
let bitmap_size = parts.len() * 8; let bitmap_size = parts.len() * 8;
let bitmap_bits = (bitmap_size as u64) * 8u64; let bitmap_bits = (bitmap_size as u64) * 8u64;
let bitmap = BitVecJournal::from_parts(parts); let bitmap = BitVecJournal::from_parts(parts);
let sips = [Bloom::sip_new(), Bloom::sip_new()]; let sips = [SipHasher::new(), SipHasher::new()];
Bloom { Bloom {
bitmap: bitmap, bitmap: bitmap,
bitmap_bits: bitmap_bits, bitmap_bits: bitmap_bits,
@ -170,12 +178,12 @@ impl Bloom {
cmp::max(k_num, 1) 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 where T: Hash
{ {
if k_i < 2 { if k_i < NUMBER_OF_HASHERS as u32 {
let sip = &mut self.sips[k_i as usize].clone(); let mut sip = self.sips[k_i as usize].clone();
item.hash(sip); item.hash(&mut sip);
let hash = sip.finish(); let hash = sip.finish();
hashes[k_i as usize] = hash; hashes[k_i as usize] = hash;
hash hash
@ -184,10 +192,6 @@ impl Bloom {
} }
} }
fn sip_new() -> SipHasher {
SipHasher::new()
}
/// Drains the bloom journal returning the updated bloom part /// Drains the bloom journal returning the updated bloom part
pub fn drain_journal(&mut self) -> BloomJournal { pub fn drain_journal(&mut self) -> BloomJournal {
BloomJournal { BloomJournal {