1- #[ cfg( not( feature = "no_alloc" ) ) ]
2- extern crate alloc;
1+ #[ cfg( not( feature = "no_alloc" ) ) ] extern crate alloc;
32extern crate criterion;
43
5- use core:: { hint:: black_box, time:: Duration } ;
6- use criterion:: { criterion_main, Criterion } ;
7- use memapi:: { type_props:: SizedProps , Alloc , AllocExt , DefaultAlloc , Layout } ;
8- use std:: ptr;
4+ use {
5+ core:: { hint:: black_box, time:: Duration } ,
6+ criterion:: { Criterion , criterion_main} ,
7+ memapi:: { Alloc , AllocExt , DefaultAlloc , Layout , type_props:: SizedProps } ,
8+ std:: ptr
9+ } ;
910
1011fn bench_alloc_dealloc ( c : & mut Criterion ) {
1112 c. bench_function ( "allod_dealloc" , |b| {
1213 b. iter ( || unsafe {
1314 let alloc = black_box ( DefaultAlloc ) ;
1415 let layout = black_box ( Layout :: from_size_align_unchecked (
1516 black_box ( usize:: SZ ) ,
16- black_box ( usize:: ALN ) ,
17+ black_box ( usize:: ALN )
1718 ) ) ;
1819 let mem = black_box ( alloc. alloc ( black_box ( layout) ) ) . unwrap ( ) ;
1920 mem. as_ptr ( ) . cast :: < usize > ( ) . write ( black_box ( 193874 ) ) ;
@@ -27,10 +28,7 @@ fn bench_alloc_filled_1k(c: &mut Criterion) {
2728 b. iter ( || {
2829 let alloc = black_box ( DefaultAlloc ) ;
2930 let layout = unsafe {
30- black_box ( Layout :: from_size_align_unchecked (
31- black_box ( 1024 ) ,
32- black_box ( 1 ) ,
33- ) )
31+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) )
3432 } ;
3533 let byte = black_box ( 0xA5_u8 ) ;
3634 let ptr = black_box ( alloc. falloc ( layout, byte) ) . unwrap ( ) ;
@@ -46,21 +44,14 @@ fn bench_grow_filled_1k_to_4k(c: &mut Criterion) {
4644 c. bench_function ( "grow_filled_1k_to_4k" , |b| {
4745 b. iter ( || unsafe {
4846 let alloc = black_box ( DefaultAlloc ) ;
49- let old_layout = black_box ( Layout :: from_size_align_unchecked (
50- black_box ( 1024 ) ,
51- black_box ( 1 ) ,
52- ) ) ;
53- let new_layout = black_box ( Layout :: from_size_align_unchecked (
54- black_box ( 4096 ) ,
55- black_box ( 1 ) ,
56- ) ) ;
47+ let old_layout =
48+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) ) ;
49+ let new_layout =
50+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 4096 ) , black_box ( 1 ) ) ) ;
5751 let ptr = black_box ( alloc. falloc ( old_layout, black_box ( 0x11_u8 ) ) ) . unwrap ( ) ;
5852
59- let grown = black_box (
60- alloc
61- . fgrow ( ptr, old_layout, new_layout, black_box ( 0x22_u8 ) )
62- . unwrap ( ) ,
63- ) ;
53+ let grown =
54+ black_box ( alloc. fgrow ( ptr, old_layout, new_layout, black_box ( 0x22_u8 ) ) . unwrap ( ) ) ;
6455
6556 ptr:: write_bytes ( grown. as_ptr ( ) , 0 , new_layout. size ( ) ) ;
6657 alloc. dealloc ( grown, new_layout) ;
@@ -72,21 +63,14 @@ fn bench_realloc_filled_4k_to_1k(c: &mut Criterion) {
7263 c. bench_function ( "realloc_filled_4k_to_1k" , |b| {
7364 b. iter ( || unsafe {
7465 let alloc = black_box ( DefaultAlloc ) ;
75- let old_layout = black_box ( Layout :: from_size_align_unchecked (
76- black_box ( 4096 ) ,
77- black_box ( 1 ) ,
78- ) ) ;
79- let new_layout = black_box ( Layout :: from_size_align_unchecked (
80- black_box ( 1024 ) ,
81- black_box ( 1 ) ,
82- ) ) ;
66+ let old_layout =
67+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 4096 ) , black_box ( 1 ) ) ) ;
68+ let new_layout =
69+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) ) ;
8370 let ptr = black_box ( alloc. falloc ( old_layout, black_box ( 0xEE_u8 ) ) ) . unwrap ( ) ;
8471
85- let shrunk = black_box (
86- alloc
87- . refalloc ( ptr, old_layout, new_layout, black_box ( 0xFF_u8 ) )
88- . unwrap ( ) ,
89- ) ;
72+ let shrunk =
73+ black_box ( alloc. refalloc ( ptr, old_layout, new_layout, black_box ( 0xFF_u8 ) ) . unwrap ( ) ) ;
9074
9175 ptr:: write_bytes ( shrunk. as_ptr ( ) , 0 , new_layout. size ( ) ) ;
9276 alloc. dealloc ( shrunk, new_layout) ;
@@ -102,7 +86,7 @@ fn bench_alloc_dealloc_base(c: &mut Criterion) {
10286 b. iter ( || unsafe {
10387 let layout = black_box ( Layout :: from_size_align_unchecked (
10488 black_box ( usize:: SZ ) ,
105- black_box ( usize:: ALN ) ,
89+ black_box ( usize:: ALN )
10690 ) ) ;
10791 let mem = black_box ( alloc ( black_box ( layout) ) ) ;
10892 mem. cast :: < usize > ( ) . write ( black_box ( 193874 ) ) ;
@@ -119,7 +103,7 @@ fn bench_alloc_default_u64_base(c: &mut Criterion) {
119103 b. iter ( || unsafe {
120104 let layout = black_box ( Layout :: from_size_align_unchecked (
121105 black_box ( u64:: SZ ) ,
122- black_box ( u64:: ALN ) ,
106+ black_box ( u64:: ALN )
123107 ) ) ;
124108 let mem = black_box ( alloc ( black_box ( layout) ) ) ;
125109 mem. cast :: < u64 > ( ) . write ( black_box ( u64:: default ( ) ) ) ;
@@ -136,7 +120,7 @@ fn bench_alloc_write_u128_base(c: &mut Criterion) {
136120 b. iter ( || unsafe {
137121 let layout = black_box ( Layout :: from_size_align_unchecked (
138122 black_box ( u128:: SZ ) ,
139- black_box ( u128:: ALN ) ,
123+ black_box ( u128:: ALN )
140124 ) ) ;
141125 let mem = black_box ( alloc ( black_box ( layout) ) ) ;
142126 let value = black_box ( 0xDEADBEEF_DEADBEEF_DEADBEEF_DEADBEEF_u128 ) ;
@@ -152,10 +136,8 @@ fn bench_alloc_filled_1k_base(c: &mut Criterion) {
152136
153137 c. bench_function ( "base_alloc_filled_1k" , |b| {
154138 b. iter ( || unsafe {
155- let layout = black_box ( Layout :: from_size_align_unchecked (
156- black_box ( 1024 ) ,
157- black_box ( 1 ) ,
158- ) ) ;
139+ let layout =
140+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) ) ;
159141 let byte = black_box ( 0xA5_u8 ) ;
160142 let mem = black_box ( alloc ( black_box ( layout) ) ) ;
161143 core:: ptr:: write_bytes ( mem, byte, layout. size ( ) ) ;
@@ -171,27 +153,23 @@ fn bench_grow_filled_1k_to_4k_base(c: &mut Criterion) {
171153
172154 c. bench_function ( "base_grow_filled_1k_to_4k" , |b| {
173155 b. iter ( || unsafe {
174- let old_layout = black_box ( Layout :: from_size_align_unchecked (
175- black_box ( 1024 ) ,
176- black_box ( 1 ) ,
177- ) ) ;
178- let new_layout = black_box ( Layout :: from_size_align_unchecked (
179- black_box ( 4096 ) ,
180- black_box ( 1 ) ,
181- ) ) ;
156+ let old_layout =
157+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) ) ;
158+ let new_layout =
159+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 4096 ) , black_box ( 1 ) ) ) ;
182160 let ptr = black_box ( alloc ( black_box ( old_layout) ) ) ;
183161 core:: ptr:: write_bytes ( ptr, black_box ( 0x11_u8 ) , old_layout. size ( ) ) ;
184162
185163 let grown = black_box ( realloc (
186164 black_box ( ptr) ,
187165 black_box ( old_layout) ,
188- black_box ( new_layout. size ( ) ) ,
166+ black_box ( new_layout. size ( ) )
189167 ) ) ;
190168 // Fill the newly added tail region
191169 core:: ptr:: write_bytes (
192170 grown. add ( old_layout. size ( ) ) ,
193171 black_box ( 0x22_u8 ) ,
194- new_layout. size ( ) - old_layout. size ( ) ,
172+ new_layout. size ( ) - old_layout. size ( )
195173 ) ;
196174
197175 core:: ptr:: write_bytes ( grown, 0u8 , new_layout. size ( ) ) ;
@@ -206,28 +184,24 @@ fn bench_realloc_filled_4k_to_1k_base(c: &mut Criterion) {
206184
207185 c. bench_function ( "base_realloc_filled_4k_to_1k" , |b| {
208186 b. iter ( || unsafe {
209- let old_layout = black_box ( Layout :: from_size_align_unchecked (
210- black_box ( 4096 ) ,
211- black_box ( 1 ) ,
212- ) ) ;
213- let new_layout = black_box ( Layout :: from_size_align_unchecked (
214- black_box ( 1024 ) ,
215- black_box ( 1 ) ,
216- ) ) ;
187+ let old_layout =
188+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 4096 ) , black_box ( 1 ) ) ) ;
189+ let new_layout =
190+ black_box ( Layout :: from_size_align_unchecked ( black_box ( 1024 ) , black_box ( 1 ) ) ) ;
217191 let ptr = black_box ( alloc ( black_box ( old_layout) ) ) ;
218192 // Initial fill
219193 core:: ptr:: write_bytes ( ptr, black_box ( 0xEE_u8 ) , old_layout. size ( ) ) ;
220194 // Pre-fill the soon-to-be-freed tail region before shrinking
221195 core:: ptr:: write_bytes (
222196 ptr. add ( new_layout. size ( ) ) ,
223197 black_box ( 0xFF_u8 ) ,
224- old_layout. size ( ) - new_layout. size ( ) ,
198+ old_layout. size ( ) - new_layout. size ( )
225199 ) ;
226200
227201 let shrunk = black_box ( realloc (
228202 black_box ( ptr) ,
229203 black_box ( old_layout) ,
230- black_box ( new_layout. size ( ) ) ,
204+ black_box ( new_layout. size ( ) )
231205 ) ) ;
232206 core:: ptr:: write_bytes ( shrunk, 0u8 , new_layout. size ( ) ) ;
233207 dealloc ( shrunk, black_box ( new_layout) ) ;
@@ -243,7 +217,7 @@ fn bench_dealloc_typed_usize_base(c: &mut Criterion) {
243217 b. iter ( || unsafe {
244218 let layout = black_box ( Layout :: from_size_align_unchecked (
245219 black_box ( usize:: SZ ) ,
246- black_box ( usize:: ALN ) ,
220+ black_box ( usize:: ALN )
247221 ) ) ;
248222 let mem = black_box ( alloc ( black_box ( layout) ) ) ;
249223 mem. cast :: < usize > ( ) . write ( black_box ( 193874_usize ) ) ;
0 commit comments