11#![ feature( test) ]
22extern crate test;
33
4- use std:: {
5- alloc:: { alloc_zeroed, dealloc, Layout } ,
6- ptr:: NonNull ,
7- } ;
8-
9- // AlignedBuffer is like a Box<[u8; N]> except that it is always N-byte aligned
10- struct AlignedBuffer < const N : usize > ( NonNull < [ u8 ; N ] > ) ;
11-
12- impl < const N : usize > AlignedBuffer < N > {
13- fn layout ( ) -> Layout {
14- Layout :: from_size_align ( N , N ) . unwrap ( )
15- }
16-
17- fn new ( ) -> Self {
18- let p = unsafe { alloc_zeroed ( Self :: layout ( ) ) } as * mut [ u8 ; N ] ;
19- Self ( NonNull :: new ( p) . unwrap ( ) )
20- }
21-
22- fn buf ( & mut self ) -> & mut [ u8 ; N ] {
23- unsafe { self . 0 . as_mut ( ) }
24- }
25- }
26-
27- impl < const N : usize > Drop for AlignedBuffer < N > {
28- fn drop ( & mut self ) {
29- unsafe { dealloc ( self . 0 . as_ptr ( ) as * mut u8 , Self :: layout ( ) ) }
30- }
31- }
32-
33- // Used to benchmark the throughput of getrandom in an optimal scenario.
34- // The buffer is hot, and does not require initialization.
354#[ inline( always) ]
365fn bench < const N : usize > ( b : & mut test:: Bencher ) {
37- let mut ab = AlignedBuffer :: < N > :: new ( ) ;
38- let buf = ab. buf ( ) ;
396 b. iter ( || {
7+ let mut buf = [ 0u8 ; N ] ;
408 getrandom:: getrandom ( & mut buf[ ..] ) . unwrap ( ) ;
419 test:: black_box ( & buf) ;
4210 } ) ;
4311 b. bytes = N as u64 ;
4412}
4513
46- // Used to benchmark the throughput of getrandom is a slightly less optimal
47- // scenario. The buffer is still hot, but requires initialization.
4814#[ inline( always) ]
49- fn bench_with_init < const N : usize > ( b : & mut test:: Bencher ) {
50- let mut ab = AlignedBuffer :: < N > :: new ( ) ;
51- let buf = ab. buf ( ) ;
15+ fn bench_raw < const N : usize > ( b : & mut test:: Bencher ) {
5216 b. iter ( || {
53- for byte in buf. iter_mut ( ) {
54- * byte = 0 ;
55- }
56- getrandom:: getrandom ( & mut buf[ ..] ) . unwrap ( ) ;
17+ let mut buf = core:: mem:: MaybeUninit :: < [ u8 ; N ] > :: uninit ( ) ;
18+ // TODO: use `cast` on MSRV bump to 1.38
19+ unsafe { getrandom:: getrandom_raw ( buf. as_mut_ptr ( ) as * mut u8 , N ) . unwrap ( ) } ;
5720 test:: black_box ( & buf) ;
5821 } ) ;
5922 b. bytes = N as u64 ;
@@ -71,24 +34,24 @@ fn bench_seed(b: &mut test::Bencher) {
7134 bench :: < SEED > ( b) ;
7235}
7336#[ bench]
74- fn bench_seed_init ( b : & mut test:: Bencher ) {
75- bench_with_init :: < SEED > ( b) ;
37+ fn bench_seed_raw ( b : & mut test:: Bencher ) {
38+ bench_raw :: < SEED > ( b) ;
7639}
7740
7841#[ bench]
7942fn bench_page ( b : & mut test:: Bencher ) {
8043 bench :: < PAGE > ( b) ;
8144}
8245#[ bench]
83- fn bench_page_init ( b : & mut test:: Bencher ) {
84- bench_with_init :: < PAGE > ( b) ;
46+ fn bench_page_raw ( b : & mut test:: Bencher ) {
47+ bench_raw :: < PAGE > ( b) ;
8548}
8649
8750#[ bench]
8851fn bench_large ( b : & mut test:: Bencher ) {
8952 bench :: < LARGE > ( b) ;
9053}
9154#[ bench]
92- fn bench_large_init ( b : & mut test:: Bencher ) {
93- bench_with_init :: < LARGE > ( b) ;
55+ fn bench_large_raw ( b : & mut test:: Bencher ) {
56+ bench_raw :: < LARGE > ( b) ;
9457}
0 commit comments