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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#![feature(test)]
extern crate test;
use blake3_guts::BLOCK_LEN;
use rand::prelude::*;
use test::Bencher;
const KIB: usize = 1024;
// This struct randomizes two things:
// 1. The actual bytes of input.
// 2. The page offset the input starts at.
pub struct RandomInput {
buf: Vec<u8>,
len: usize,
offsets: Vec<usize>,
offset_index: usize,
}
impl RandomInput {
pub fn new(b: &mut Bencher, len: usize) -> Self {
b.bytes += len as u64;
let page_size: usize = page_size::get();
let mut buf = vec![0u8; len + page_size];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut buf);
let mut offsets: Vec<usize> = (0..page_size).collect();
offsets.shuffle(&mut rng);
Self {
buf,
len,
offsets,
offset_index: 0,
}
}
pub fn get(&mut self) -> &[u8] {
let offset = self.offsets[self.offset_index];
self.offset_index += 1;
if self.offset_index >= self.offsets.len() {
self.offset_index = 0;
}
&self.buf[offset..][..self.len]
}
}
fn bench_atonce(b: &mut Bencher, len: usize) {
let mut input = RandomInput::new(b, len);
b.iter(|| blake3::hash(input.get()));
}
#[bench]
fn bench_atonce_0001_block(b: &mut Bencher) {
bench_atonce(b, BLOCK_LEN);
}
#[bench]
fn bench_atonce_0001_kib(b: &mut Bencher) {
bench_atonce(b, 1 * KIB);
}
#[bench]
fn bench_atonce_0002_kib(b: &mut Bencher) {
bench_atonce(b, 2 * KIB);
}
#[bench]
fn bench_atonce_0004_kib(b: &mut Bencher) {
bench_atonce(b, 4 * KIB);
}
#[bench]
fn bench_atonce_0008_kib(b: &mut Bencher) {
bench_atonce(b, 8 * KIB);
}
#[bench]
fn bench_atonce_0016_kib(b: &mut Bencher) {
bench_atonce(b, 16 * KIB);
}
#[bench]
fn bench_atonce_0032_kib(b: &mut Bencher) {
bench_atonce(b, 32 * KIB);
}
#[bench]
fn bench_atonce_0064_kib(b: &mut Bencher) {
bench_atonce(b, 64 * KIB);
}
#[bench]
fn bench_atonce_0128_kib(b: &mut Bencher) {
bench_atonce(b, 128 * KIB);
}
#[bench]
fn bench_atonce_0256_kib(b: &mut Bencher) {
bench_atonce(b, 256 * KIB);
}
#[bench]
fn bench_atonce_0512_kib(b: &mut Bencher) {
bench_atonce(b, 512 * KIB);
}
#[bench]
fn bench_atonce_1024_kib(b: &mut Bencher) {
bench_atonce(b, 1024 * KIB);
}
fn bench_incremental(b: &mut Bencher, len: usize) {
let mut input = RandomInput::new(b, len);
b.iter(|| blake3::Hasher::new().update(input.get()).finalize());
}
#[bench]
fn bench_incremental_0001_block(b: &mut Bencher) {
bench_incremental(b, BLOCK_LEN);
}
#[bench]
fn bench_incremental_0001_kib(b: &mut Bencher) {
bench_incremental(b, 1 * KIB);
}
#[bench]
fn bench_incremental_0002_kib(b: &mut Bencher) {
bench_incremental(b, 2 * KIB);
}
#[bench]
fn bench_incremental_0004_kib(b: &mut Bencher) {
bench_incremental(b, 4 * KIB);
}
#[bench]
fn bench_incremental_0008_kib(b: &mut Bencher) {
bench_incremental(b, 8 * KIB);
}
#[bench]
fn bench_incremental_0016_kib(b: &mut Bencher) {
bench_incremental(b, 16 * KIB);
}
#[bench]
fn bench_incremental_0032_kib(b: &mut Bencher) {
bench_incremental(b, 32 * KIB);
}
#[bench]
fn bench_incremental_0064_kib(b: &mut Bencher) {
bench_incremental(b, 64 * KIB);
}
#[bench]
fn bench_incremental_0128_kib(b: &mut Bencher) {
bench_incremental(b, 128 * KIB);
}
#[bench]
fn bench_incremental_0256_kib(b: &mut Bencher) {
bench_incremental(b, 256 * KIB);
}
#[bench]
fn bench_incremental_0512_kib(b: &mut Bencher) {
bench_incremental(b, 512 * KIB);
}
#[bench]
fn bench_incremental_1024_kib(b: &mut Bencher) {
bench_incremental(b, 1024 * KIB);
}
fn bench_reference(b: &mut Bencher, len: usize) {
let mut input = RandomInput::new(b, len);
b.iter(|| {
let mut hasher = reference_impl::Hasher::new();
hasher.update(input.get());
let mut out = [0; 32];
hasher.finalize(&mut out);
out
});
}
#[bench]
fn bench_reference_0001_block(b: &mut Bencher) {
bench_reference(b, BLOCK_LEN);
}
#[bench]
fn bench_reference_0001_kib(b: &mut Bencher) {
bench_reference(b, 1 * KIB);
}
#[bench]
fn bench_reference_0002_kib(b: &mut Bencher) {
bench_reference(b, 2 * KIB);
}
#[bench]
fn bench_reference_0004_kib(b: &mut Bencher) {
bench_reference(b, 4 * KIB);
}
#[bench]
fn bench_reference_0008_kib(b: &mut Bencher) {
bench_reference(b, 8 * KIB);
}
#[bench]
fn bench_reference_0016_kib(b: &mut Bencher) {
bench_reference(b, 16 * KIB);
}
#[bench]
fn bench_reference_0032_kib(b: &mut Bencher) {
bench_reference(b, 32 * KIB);
}
#[bench]
fn bench_reference_0064_kib(b: &mut Bencher) {
bench_reference(b, 64 * KIB);
}
#[bench]
fn bench_reference_0128_kib(b: &mut Bencher) {
bench_reference(b, 128 * KIB);
}
#[bench]
fn bench_reference_0256_kib(b: &mut Bencher) {
bench_reference(b, 256 * KIB);
}
#[bench]
fn bench_reference_0512_kib(b: &mut Bencher) {
bench_reference(b, 512 * KIB);
}
#[bench]
fn bench_reference_1024_kib(b: &mut Bencher) {
bench_reference(b, 1024 * KIB);
}
#[cfg(feature = "rayon")]
fn bench_rayon(b: &mut Bencher, len: usize) {
let mut input = RandomInput::new(b, len);
b.iter(|| blake3::Hasher::new().update_rayon(input.get()).finalize());
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0001_block(b: &mut Bencher) {
bench_rayon(b, BLOCK_LEN);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0001_kib(b: &mut Bencher) {
bench_rayon(b, 1 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0002_kib(b: &mut Bencher) {
bench_rayon(b, 2 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0004_kib(b: &mut Bencher) {
bench_rayon(b, 4 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0008_kib(b: &mut Bencher) {
bench_rayon(b, 8 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0016_kib(b: &mut Bencher) {
bench_rayon(b, 16 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0032_kib(b: &mut Bencher) {
bench_rayon(b, 32 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0064_kib(b: &mut Bencher) {
bench_rayon(b, 64 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0128_kib(b: &mut Bencher) {
bench_rayon(b, 128 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0256_kib(b: &mut Bencher) {
bench_rayon(b, 256 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_0512_kib(b: &mut Bencher) {
bench_rayon(b, 512 * KIB);
}
#[bench]
#[cfg(feature = "rayon")]
fn bench_rayon_1024_kib(b: &mut Bencher) {
bench_rayon(b, 1024 * KIB);
}
// This checks that update() splits up its input in increasing powers of 2, so
// that it can recover a high degree of parallelism when the number of bytes
// hashed so far is uneven. The performance of this benchmark should be
// reasonably close to bench_incremental_0064_kib, within 80% or so. When we
// had a bug in this logic (https://github.com/BLAKE3-team/BLAKE3/issues/69),
// performance was less than half.
#[bench]
fn bench_two_updates(b: &mut Bencher) {
let len = 65536;
let mut input = RandomInput::new(b, len);
b.iter(|| {
let mut hasher = blake3::Hasher::new();
let input = input.get();
hasher.update(&input[..1]);
hasher.update(&input[1..]);
hasher.finalize()
});
}
|