Debian Codegraph @

debian/rust-vendor/getrandom/benches/buffer.rs

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