-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathbloom.rs
134 lines (123 loc) · 3.7 KB
/
bloom.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2020 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use array_bytes::hex2bytes_unchecked;
use criterion::{criterion_group, criterion_main, Criterion};
use ethbloom::{Bloom, Input};
use tiny_keccak::{Hasher, Keccak};
fn test_bloom() -> Bloom {
use std::str::FromStr;
Bloom::from_str(
"00000000000000000000000000000000\
00000000100000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000002020000000000000000000000\
00000000000000000000000800000000\
10000000000000000000000000000000\
00000000000000000000001000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000",
)
.unwrap()
}
fn keccak256(input: &[u8]) -> [u8; 32] {
let mut out = [0u8; 32];
let mut keccak256 = Keccak::v256();
keccak256.update(input);
keccak256.finalize(&mut out);
out
}
fn test_topic() -> Vec<u8> {
hex2bytes_unchecked("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc")
}
fn test_address() -> Vec<u8> {
hex2bytes_unchecked("ef2d6d194084c2de36e0dabfce45d046b37d1106")
}
fn test_dummy() -> Vec<u8> {
b"123456".to_vec()
}
fn test_dummy2() -> Vec<u8> {
b"654321".to_vec()
}
fn bench_accrue(c: &mut Criterion) {
c.bench_function("accrue_raw", |b| {
let mut bloom = Bloom::default();
let topic = test_topic();
let address = test_address();
b.iter(|| {
bloom.accrue(Input::Raw(&topic));
bloom.accrue(Input::Raw(&address));
})
});
c.bench_function("accrue_hash", |b| {
let mut bloom = Bloom::default();
let topic = keccak256(&test_topic());
let address = keccak256(&test_address());
b.iter(|| {
bloom.accrue(Input::Hash(&topic));
bloom.accrue(Input::Hash(&address));
})
});
}
fn bench_contains(c: &mut Criterion) {
c.bench_function("contains_input_raw", |b| {
let bloom = test_bloom();
let topic = test_topic();
let address = test_address();
b.iter(|| {
assert!(bloom.contains_input(Input::Raw(&topic)));
assert!(bloom.contains_input(Input::Raw(&address)));
})
});
c.bench_function("contains_input_hash", |b| {
let bloom = test_bloom();
let topic = keccak256(&test_topic());
let address = keccak256(&test_address());
b.iter(|| {
assert!(bloom.contains_input(Input::Hash(&topic)));
assert!(bloom.contains_input(Input::Hash(&address)));
})
});
}
fn bench_not_contains(c: &mut Criterion) {
c.bench_function("does_not_contain_raw", |b| {
let bloom = test_bloom();
let dummy = test_dummy();
let dummy2 = test_dummy2();
b.iter(|| {
assert!(!bloom.contains_input(Input::Raw(&dummy)));
assert!(!bloom.contains_input(Input::Raw(&dummy2)));
})
});
c.bench_function("does_not_contain_hash", |b| {
let bloom = test_bloom();
let dummy = keccak256(&test_dummy());
let dummy2 = keccak256(&test_dummy2());
b.iter(|| {
assert!(!bloom.contains_input(Input::Hash(&dummy)));
assert!(!bloom.contains_input(Input::Hash(&dummy2)));
})
});
c.bench_function("does_not_contain_random_hash", |b| {
let bloom = test_bloom();
let dummy: Vec<_> = (0..255u8).map(|i| keccak256(&[i])).collect();
b.iter(|| {
for d in &dummy {
assert!(!bloom.contains_input(Input::Hash(d)));
}
})
});
}
criterion_group!(benches, bench_accrue, bench_contains, bench_not_contains);
criterion_main!(benches);