Debian Codegraph @

.cargo/vendor/getrandom/benches/buffer.rs

raw
1 #![feature(test, maybe_uninit_uninit_array_transpose)]
2 extern crate test;
3
4 use std::{
5 mem::{size_of, MaybeUninit},
6 slice,
7 };
8
9 // Call getrandom on a zero-initialized stack buffer
10 #[inline(always)]
11 fn bench_fill<const N: usize>() {
12 let mut buf = [0u8; N];
13 getrandom::fill(&mut buf).unwrap();
14 test::black_box(&buf[..]);
15 }
16
17 // Call fill_uninit on an uninitialized stack buffer
18 #[inline(always)]
19 fn bench_fill_uninit<const N: usize>() {
20 let mut uninit = [MaybeUninit::uninit(); N];
21 let buf: &[u8] = getrandom::fill_uninit(&mut uninit).unwrap();
22 test::black_box(buf);
23 }
24
25 #[bench]
26 pub fn bench_u32(b: &mut test::Bencher) {
27 #[inline(never)]
28 fn inner() -> u32 {
29 getrandom::u32().unwrap()
30 }
31 b.bytes = 4;
32 b.iter(inner);
33 }
34 #[bench]
35 pub fn bench_u32_via_fill(b: &mut test::Bencher) {
36 #[inline(never)]
37 fn inner() -> u32 {
38 let mut res = MaybeUninit::<u32>::uninit();
39 let dst: &mut [MaybeUninit<u8>] =
40 unsafe { slice::from_raw_parts_mut(res.as_mut_ptr().cast(), size_of::<u32>()) };
41 getrandom::fill_uninit(dst).unwrap();
42 unsafe { res.assume_init() }
43 }
44 b.bytes = 4;
45 b.iter(inner);
46 }
47
48 #[bench]
49 pub fn bench_u64(b: &mut test::Bencher) {
50 #[inline(never)]
51 fn inner() -> u64 {
52 getrandom::u64().unwrap()
53 }
54 b.bytes = 8;
55 b.iter(inner);
56 }
57
58 #[bench]
59 pub fn bench_u64_via_fill(b: &mut test::Bencher) {
60 #[inline(never)]
61 fn inner() -> u64 {
62 let mut res = MaybeUninit::<u64>::uninit();
63 let dst: &mut [MaybeUninit<u8>] =
64 unsafe { slice::from_raw_parts_mut(res.as_mut_ptr().cast(), size_of::<u64>()) };
65 getrandom::fill_uninit(dst).unwrap();
66 unsafe { res.assume_init() }
67 }
68 b.bytes = 8;
69 b.iter(inner);
70 }
71
72 // We benchmark using #[inline(never)] "inner" functions for two reasons:
73 // - Avoiding inlining reduces a source of variance when running benchmarks.
74 // - It is _much_ easier to get the assembly or IR for the inner loop.
75 //
76 // For example, using cargo-show-asm (https://github.com/pacak/cargo-show-asm),
77 // we can get the assembly for a particular benchmark's inner loop by running:
78 // cargo asm --bench buffer --release buffer::p384::bench_getrandom::inner
79 macro_rules! bench {
80 ( $name:ident, $size:expr ) => {
81 pub mod $name {
82 #[bench]
83 pub fn bench_fill(b: &mut test::Bencher) {
84 #[inline(never)]
85 fn inner() {
86 super::bench_fill::<{ $size }>()
87 }
88
89 b.bytes = $size as u64;
90 b.iter(inner);
91 }
92 #[bench]
93 pub fn bench_fill_uninit(b: &mut test::Bencher) {
94 #[inline(never)]
95 fn inner() {
96 super::bench_fill_uninit::<{ $size }>()
97 }
98
99 b.bytes = $size as u64;
100 b.iter(inner);
101 }
102 }
103 };
104 }
105
106 // 16 bytes (128 bits) is the size of an 128-bit AES key/nonce.
107 bench!(aes128, 128 / 8);
108
109 // 32 bytes (256 bits) is the seed sized used for rand::thread_rng
110 // and the `random` value in a ClientHello/ServerHello for TLS.
111 // This is also the size of a 256-bit AES/HMAC/P-256/Curve25519 key
112 // and/or nonce.
113 bench!(p256, 256 / 8);
114
115 // A P-384/HMAC-384 key and/or nonce.
116 bench!(p384, 384 / 8);
117
118 // Initializing larger buffers is not the primary use case of this library, as
119 // this should normally be done by a userspace CSPRNG. However, we have a test
120 // here to see the effects of a lower (amortized) syscall overhead.
121 bench!(page, 4096);