Debian Codegraph @

debian/rust-vendor/blake3/benches/bench.rs

raw
1 #![feature(test)]
2
3 extern crate test;
4
5 use arrayvec::ArrayVec;
6 use blake3::OUT_LEN;
7 use blake3::platform::{MAX_SIMD_DEGREE, Platform};
8 use blake3::{BLOCK_LEN, CHUNK_LEN};
9 use rand::prelude::*;
10 use test::Bencher;
11
12 const KIB: usize = 1024;
13
14 // This struct randomizes two things:
15 // 1. The actual bytes of input.
16 // 2. The page offset the input starts at.
17 pub struct RandomInput {
18 buf: Vec<u8>,
19 len: usize,
20 offsets: Vec<usize>,
21 offset_index: usize,
22 }
23
24 impl RandomInput {
25 pub fn new(b: &mut Bencher, len: usize) -> Self {
26 b.bytes += len as u64;
27 let page_size: usize = page_size::get();
28 let mut buf = vec![0u8; len + page_size];
29 let mut rng = rand::rng();
30 rng.fill_bytes(&mut buf);
31 let mut offsets: Vec<usize> = (0..page_size).collect();
32 offsets.shuffle(&mut rng);
33 Self {
34 buf,
35 len,
36 offsets,
37 offset_index: 0,
38 }
39 }
40
41 pub fn get(&mut self) -> &[u8] {
42 let offset = self.offsets[self.offset_index];
43 self.offset_index += 1;
44 if self.offset_index >= self.offsets.len() {
45 self.offset_index = 0;
46 }
47 &self.buf[offset..][..self.len]
48 }
49 }
50
51 fn bench_single_compression_fn(b: &mut Bencher, platform: Platform) {
52 let mut state = [1u32; 8];
53 let mut r = RandomInput::new(b, 64);
54 let input = (&r.get()[..64]).try_into().unwrap();
55 b.iter(|| platform.compress_in_place(&mut state, input, 64 as u8, 0, 0));
56 }
57
58 #[bench]
59 fn bench_single_compression_portable(b: &mut Bencher) {
60 bench_single_compression_fn(b, Platform::portable());
61 }
62
63 #[bench]
64 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
65 fn bench_single_compression_sse2(b: &mut Bencher) {
66 if let Some(platform) = Platform::sse2() {
67 bench_single_compression_fn(b, platform);
68 }
69 }
70
71 #[bench]
72 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
73 fn bench_single_compression_sse41(b: &mut Bencher) {
74 if let Some(platform) = Platform::sse41() {
75 bench_single_compression_fn(b, platform);
76 }
77 }
78
79 #[bench]
80 #[cfg(blake3_avx512_ffi)]
81 fn bench_single_compression_avx512(b: &mut Bencher) {
82 if let Some(platform) = Platform::avx512() {
83 bench_single_compression_fn(b, platform);
84 }
85 }
86
87 fn bench_many_chunks_fn(b: &mut Bencher, platform: Platform) {
88 let degree = platform.simd_degree();
89 let mut inputs = Vec::new();
90 for _ in 0..degree {
91 inputs.push(RandomInput::new(b, CHUNK_LEN));
92 }
93 b.iter(|| {
94 let input_arrays: ArrayVec<&[u8; CHUNK_LEN], MAX_SIMD_DEGREE> = inputs
95 .iter_mut()
96 .take(degree)
97 .map(|i| (&i.get()[..CHUNK_LEN]).try_into().unwrap())
98 .collect();
99 let mut out = [0; MAX_SIMD_DEGREE * OUT_LEN];
100 platform.hash_many(
101 &input_arrays[..],
102 &[0; 8],
103 0,
104 blake3::IncrementCounter::Yes,
105 0,
106 0,
107 0,
108 &mut out,
109 );
110 });
111 }
112
113 #[bench]
114 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
115 fn bench_many_chunks_sse2(b: &mut Bencher) {
116 if let Some(platform) = Platform::sse2() {
117 bench_many_chunks_fn(b, platform);
118 }
119 }
120
121 #[bench]
122 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
123 fn bench_many_chunks_sse41(b: &mut Bencher) {
124 if let Some(platform) = Platform::sse41() {
125 bench_many_chunks_fn(b, platform);
126 }
127 }
128
129 #[bench]
130 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
131 fn bench_many_chunks_avx2(b: &mut Bencher) {
132 if let Some(platform) = Platform::avx2() {
133 bench_many_chunks_fn(b, platform);
134 }
135 }
136
137 #[bench]
138 #[cfg(blake3_avx512_ffi)]
139 fn bench_many_chunks_avx512(b: &mut Bencher) {
140 if let Some(platform) = Platform::avx512() {
141 bench_many_chunks_fn(b, platform);
142 }
143 }
144
145 #[bench]
146 #[cfg(blake3_neon)]
147 fn bench_many_chunks_neon(b: &mut Bencher) {
148 bench_many_chunks_fn(b, Platform::neon().unwrap());
149 }
150
151 #[bench]
152 #[cfg(blake3_wasm32_simd)]
153 fn bench_many_chunks_wasm(b: &mut Bencher) {
154 bench_many_chunks_fn(b, Platform::wasm32_simd().unwrap());
155 }
156
157 // TODO: When we get const generics we can unify this with the chunks code.
158 fn bench_many_parents_fn(b: &mut Bencher, platform: Platform) {
159 let degree = platform.simd_degree();
160 let mut inputs = Vec::new();
161 for _ in 0..degree {
162 inputs.push(RandomInput::new(b, BLOCK_LEN));
163 }
164 b.iter(|| {
165 let input_arrays: ArrayVec<&[u8; BLOCK_LEN], MAX_SIMD_DEGREE> = inputs
166 .iter_mut()
167 .take(degree)
168 .map(|i| (&i.get()[..BLOCK_LEN]).try_into().unwrap())
169 .collect();
170 let mut out = [0; MAX_SIMD_DEGREE * OUT_LEN];
171 platform.hash_many(
172 &input_arrays[..],
173 &[0; 8],
174 0,
175 blake3::IncrementCounter::No,
176 0,
177 0,
178 0,
179 &mut out,
180 );
181 });
182 }
183
184 #[bench]
185 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
186 fn bench_many_parents_sse2(b: &mut Bencher) {
187 if let Some(platform) = Platform::sse2() {
188 bench_many_parents_fn(b, platform);
189 }
190 }
191
192 #[bench]
193 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
194 fn bench_many_parents_sse41(b: &mut Bencher) {
195 if let Some(platform) = Platform::sse41() {
196 bench_many_parents_fn(b, platform);
197 }
198 }
199
200 #[bench]
201 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
202 fn bench_many_parents_avx2(b: &mut Bencher) {
203 if let Some(platform) = Platform::avx2() {
204 bench_many_parents_fn(b, platform);
205 }
206 }
207
208 #[bench]
209 #[cfg(blake3_avx512_ffi)]
210 fn bench_many_parents_avx512(b: &mut Bencher) {
211 if let Some(platform) = Platform::avx512() {
212 bench_many_parents_fn(b, platform);
213 }
214 }
215
216 #[bench]
217 #[cfg(blake3_neon)]
218 fn bench_many_parents_neon(b: &mut Bencher) {
219 bench_many_parents_fn(b, Platform::neon().unwrap());
220 }
221
222 #[bench]
223 #[cfg(blake3_wasm32_simd)]
224 fn bench_many_parents_wasm(b: &mut Bencher) {
225 bench_many_parents_fn(b, Platform::wasm32_simd().unwrap());
226 }
227
228 fn bench_atonce(b: &mut Bencher, len: usize) {
229 let mut input = RandomInput::new(b, len);
230 b.iter(|| blake3::hash(input.get()));
231 }
232
233 #[bench]
234 fn bench_atonce_0001_block(b: &mut Bencher) {
235 bench_atonce(b, BLOCK_LEN);
236 }
237
238 #[bench]
239 fn bench_atonce_0001_kib(b: &mut Bencher) {
240 bench_atonce(b, 1 * KIB);
241 }
242
243 #[bench]
244 fn bench_atonce_0002_kib(b: &mut Bencher) {
245 bench_atonce(b, 2 * KIB);
246 }
247
248 #[bench]
249 fn bench_atonce_0004_kib(b: &mut Bencher) {
250 bench_atonce(b, 4 * KIB);
251 }
252
253 #[bench]
254 fn bench_atonce_0008_kib(b: &mut Bencher) {
255 bench_atonce(b, 8 * KIB);
256 }
257
258 #[bench]
259 fn bench_atonce_0016_kib(b: &mut Bencher) {
260 bench_atonce(b, 16 * KIB);
261 }
262
263 #[bench]
264 fn bench_atonce_0032_kib(b: &mut Bencher) {
265 bench_atonce(b, 32 * KIB);
266 }
267
268 #[bench]
269 fn bench_atonce_0064_kib(b: &mut Bencher) {
270 bench_atonce(b, 64 * KIB);
271 }
272
273 #[bench]
274 fn bench_atonce_0128_kib(b: &mut Bencher) {
275 bench_atonce(b, 128 * KIB);
276 }
277
278 #[bench]
279 fn bench_atonce_0256_kib(b: &mut Bencher) {
280 bench_atonce(b, 256 * KIB);
281 }
282
283 #[bench]
284 fn bench_atonce_0512_kib(b: &mut Bencher) {
285 bench_atonce(b, 512 * KIB);
286 }
287
288 #[bench]
289 fn bench_atonce_1024_kib(b: &mut Bencher) {
290 bench_atonce(b, 1024 * KIB);
291 }
292
293 fn bench_incremental(b: &mut Bencher, len: usize) {
294 let mut input = RandomInput::new(b, len);
295 b.iter(|| blake3::Hasher::new().update(input.get()).finalize());
296 }
297
298 #[bench]
299 fn bench_incremental_0001_block(b: &mut Bencher) {
300 bench_incremental(b, BLOCK_LEN);
301 }
302
303 #[bench]
304 fn bench_incremental_0001_kib(b: &mut Bencher) {
305 bench_incremental(b, 1 * KIB);
306 }
307
308 #[bench]
309 fn bench_incremental_0002_kib(b: &mut Bencher) {
310 bench_incremental(b, 2 * KIB);
311 }
312
313 #[bench]
314 fn bench_incremental_0004_kib(b: &mut Bencher) {
315 bench_incremental(b, 4 * KIB);
316 }
317
318 #[bench]
319 fn bench_incremental_0008_kib(b: &mut Bencher) {
320 bench_incremental(b, 8 * KIB);
321 }
322
323 #[bench]
324 fn bench_incremental_0016_kib(b: &mut Bencher) {
325 bench_incremental(b, 16 * KIB);
326 }
327
328 #[bench]
329 fn bench_incremental_0032_kib(b: &mut Bencher) {
330 bench_incremental(b, 32 * KIB);
331 }
332
333 #[bench]
334 fn bench_incremental_0064_kib(b: &mut Bencher) {
335 bench_incremental(b, 64 * KIB);
336 }
337
338 #[bench]
339 fn bench_incremental_0128_kib(b: &mut Bencher) {
340 bench_incremental(b, 128 * KIB);
341 }
342
343 #[bench]
344 fn bench_incremental_0256_kib(b: &mut Bencher) {
345 bench_incremental(b, 256 * KIB);
346 }
347
348 #[bench]
349 fn bench_incremental_0512_kib(b: &mut Bencher) {
350 bench_incremental(b, 512 * KIB);
351 }
352
353 #[bench]
354 fn bench_incremental_1024_kib(b: &mut Bencher) {
355 bench_incremental(b, 1024 * KIB);
356 }
357
358 fn bench_reference(b: &mut Bencher, len: usize) {
359 let mut input = RandomInput::new(b, len);
360 b.iter(|| {
361 let mut hasher = reference_impl::Hasher::new();
362 hasher.update(input.get());
363 let mut out = [0; 32];
364 hasher.finalize(&mut out);
365 out
366 });
367 }
368
369 #[bench]
370 fn bench_reference_0001_block(b: &mut Bencher) {
371 bench_reference(b, BLOCK_LEN);
372 }
373
374 #[bench]
375 fn bench_reference_0001_kib(b: &mut Bencher) {
376 bench_reference(b, 1 * KIB);
377 }
378
379 #[bench]
380 fn bench_reference_0002_kib(b: &mut Bencher) {
381 bench_reference(b, 2 * KIB);
382 }
383
384 #[bench]
385 fn bench_reference_0004_kib(b: &mut Bencher) {
386 bench_reference(b, 4 * KIB);
387 }
388
389 #[bench]
390 fn bench_reference_0008_kib(b: &mut Bencher) {
391 bench_reference(b, 8 * KIB);
392 }
393
394 #[bench]
395 fn bench_reference_0016_kib(b: &mut Bencher) {
396 bench_reference(b, 16 * KIB);
397 }
398
399 #[bench]
400 fn bench_reference_0032_kib(b: &mut Bencher) {
401 bench_reference(b, 32 * KIB);
402 }
403
404 #[bench]
405 fn bench_reference_0064_kib(b: &mut Bencher) {
406 bench_reference(b, 64 * KIB);
407 }
408
409 #[bench]
410 fn bench_reference_0128_kib(b: &mut Bencher) {
411 bench_reference(b, 128 * KIB);
412 }
413
414 #[bench]
415 fn bench_reference_0256_kib(b: &mut Bencher) {
416 bench_reference(b, 256 * KIB);
417 }
418
419 #[bench]
420 fn bench_reference_0512_kib(b: &mut Bencher) {
421 bench_reference(b, 512 * KIB);
422 }
423
424 #[bench]
425 fn bench_reference_1024_kib(b: &mut Bencher) {
426 bench_reference(b, 1024 * KIB);
427 }
428
429 #[cfg(feature = "rayon")]
430 fn bench_rayon(b: &mut Bencher, len: usize) {
431 let mut input = RandomInput::new(b, len);
432 b.iter(|| blake3::Hasher::new().update_rayon(input.get()).finalize());
433 }
434
435 #[bench]
436 #[cfg(feature = "rayon")]
437 fn bench_rayon_0001_block(b: &mut Bencher) {
438 bench_rayon(b, BLOCK_LEN);
439 }
440
441 #[bench]
442 #[cfg(feature = "rayon")]
443 fn bench_rayon_0001_kib(b: &mut Bencher) {
444 bench_rayon(b, 1 * KIB);
445 }
446
447 #[bench]
448 #[cfg(feature = "rayon")]
449 fn bench_rayon_0002_kib(b: &mut Bencher) {
450 bench_rayon(b, 2 * KIB);
451 }
452
453 #[bench]
454 #[cfg(feature = "rayon")]
455 fn bench_rayon_0004_kib(b: &mut Bencher) {
456 bench_rayon(b, 4 * KIB);
457 }
458
459 #[bench]
460 #[cfg(feature = "rayon")]
461 fn bench_rayon_0008_kib(b: &mut Bencher) {
462 bench_rayon(b, 8 * KIB);
463 }
464
465 #[bench]
466 #[cfg(feature = "rayon")]
467 fn bench_rayon_0016_kib(b: &mut Bencher) {
468 bench_rayon(b, 16 * KIB);
469 }
470
471 #[bench]
472 #[cfg(feature = "rayon")]
473 fn bench_rayon_0032_kib(b: &mut Bencher) {
474 bench_rayon(b, 32 * KIB);
475 }
476
477 #[bench]
478 #[cfg(feature = "rayon")]
479 fn bench_rayon_0064_kib(b: &mut Bencher) {
480 bench_rayon(b, 64 * KIB);
481 }
482
483 #[bench]
484 #[cfg(feature = "rayon")]
485 fn bench_rayon_0128_kib(b: &mut Bencher) {
486 bench_rayon(b, 128 * KIB);
487 }
488
489 #[bench]
490 #[cfg(feature = "rayon")]
491 fn bench_rayon_0256_kib(b: &mut Bencher) {
492 bench_rayon(b, 256 * KIB);
493 }
494
495 #[bench]
496 #[cfg(feature = "rayon")]
497 fn bench_rayon_0512_kib(b: &mut Bencher) {
498 bench_rayon(b, 512 * KIB);
499 }
500
501 #[bench]
502 #[cfg(feature = "rayon")]
503 fn bench_rayon_1024_kib(b: &mut Bencher) {
504 bench_rayon(b, 1024 * KIB);
505 }
506
507 // This checks that update() splits up its input in increasing powers of 2, so
508 // that it can recover a high degree of parallelism when the number of bytes
509 // hashed so far is uneven. The performance of this benchmark should be
510 // reasonably close to bench_incremental_0064_kib, within 80% or so. When we
511 // had a bug in this logic (https://github.com/BLAKE3-team/BLAKE3/issues/69),
512 // performance was less than half.
513 #[bench]
514 fn bench_two_updates(b: &mut Bencher) {
515 let len = 65536;
516 let mut input = RandomInput::new(b, len);
517 b.iter(|| {
518 let mut hasher = blake3::Hasher::new();
519 let input = input.get();
520 hasher.update(&input[..1]);
521 hasher.update(&input[1..]);
522 hasher.finalize()
523 });
524 }
525
526 fn bench_xof(b: &mut Bencher, len: usize) {
527 b.bytes = len as u64;
528 let mut output = [0u8; 64 * BLOCK_LEN];
529 let output_slice = &mut output[..len];
530 let mut xof = blake3::Hasher::new().finalize_xof();
531 b.iter(|| xof.fill(output_slice));
532 }
533
534 #[bench]
535 fn bench_xof_01_block(b: &mut Bencher) {
536 bench_xof(b, 1 * BLOCK_LEN);
537 }
538
539 #[bench]
540 fn bench_xof_02_blocks(b: &mut Bencher) {
541 bench_xof(b, 2 * BLOCK_LEN);
542 }
543
544 #[bench]
545 fn bench_xof_03_blocks(b: &mut Bencher) {
546 bench_xof(b, 3 * BLOCK_LEN);
547 }
548
549 #[bench]
550 fn bench_xof_04_blocks(b: &mut Bencher) {
551 bench_xof(b, 4 * BLOCK_LEN);
552 }
553
554 #[bench]
555 fn bench_xof_05_blocks(b: &mut Bencher) {
556 bench_xof(b, 5 * BLOCK_LEN);
557 }
558
559 #[bench]
560 fn bench_xof_06_blocks(b: &mut Bencher) {
561 bench_xof(b, 6 * BLOCK_LEN);
562 }
563
564 #[bench]
565 fn bench_xof_07_blocks(b: &mut Bencher) {
566 bench_xof(b, 7 * BLOCK_LEN);
567 }
568
569 #[bench]
570 fn bench_xof_08_blocks(b: &mut Bencher) {
571 bench_xof(b, 8 * BLOCK_LEN);
572 }
573
574 #[bench]
575 fn bench_xof_09_blocks(b: &mut Bencher) {
576 bench_xof(b, 9 * BLOCK_LEN);
577 }
578
579 #[bench]
580 fn bench_xof_10_blocks(b: &mut Bencher) {
581 bench_xof(b, 10 * BLOCK_LEN);
582 }
583
584 #[bench]
585 fn bench_xof_11_blocks(b: &mut Bencher) {
586 bench_xof(b, 11 * BLOCK_LEN);
587 }
588
589 #[bench]
590 fn bench_xof_12_blocks(b: &mut Bencher) {
591 bench_xof(b, 12 * BLOCK_LEN);
592 }
593
594 #[bench]
595 fn bench_xof_13_blocks(b: &mut Bencher) {
596 bench_xof(b, 13 * BLOCK_LEN);
597 }
598
599 #[bench]
600 fn bench_xof_14_blocks(b: &mut Bencher) {
601 bench_xof(b, 14 * BLOCK_LEN);
602 }
603
604 #[bench]
605 fn bench_xof_15_blocks(b: &mut Bencher) {
606 bench_xof(b, 15 * BLOCK_LEN);
607 }
608
609 #[bench]
610 fn bench_xof_16_blocks(b: &mut Bencher) {
611 bench_xof(b, 16 * BLOCK_LEN);
612 }
613
614 #[bench]
615 fn bench_xof_32_blocks(b: &mut Bencher) {
616 bench_xof(b, 32 * BLOCK_LEN);
617 }
618
619 #[bench]
620 fn bench_xof_64_blocks(b: &mut Bencher) {
621 bench_xof(b, 64 * BLOCK_LEN);
622 }