1+ #[ cfg( target_family = "wasm" ) ]
2+ use std:: alloc:: System ;
13use std:: {
2- alloc:: { GlobalAlloc , Layout , System } ,
4+ alloc:: { GlobalAlloc , Layout } ,
35 env, fs,
46 fs:: read_to_string,
57 future:: Future ,
@@ -9,20 +11,38 @@ use std::{
911} ;
1012
1113#[ global_allocator]
12- static GLOBAL : NeverGrowInPlaceAllocator = NeverGrowInPlaceAllocator ;
14+ #[ cfg( not( target_family = "wasm" ) ) ]
15+ static GLOBAL : NeverGrowInPlaceAllocator < mimalloc:: MiMalloc > =
16+ NeverGrowInPlaceAllocator :: new ( mimalloc:: MiMalloc ) ;
1317
14- /// Delegates `alloc`/`dealloc` to [`System`] but omits [`GlobalAlloc::realloc`],
15- /// forcing the default "alloc-new + copy + dealloc-old" path so that benchmarks
16- /// never benefit from non-deterministic in-place growth provided by `libc::realloc`.
17- struct NeverGrowInPlaceAllocator ;
18+ #[ global_allocator]
19+ #[ cfg( target_family = "wasm" ) ]
20+ static GLOBAL : NeverGrowInPlaceAllocator < System > = NeverGrowInPlaceAllocator :: new ( System ) ;
21+
22+ /// Delegates `alloc`/`dealloc` to the wrapped allocator but omits
23+ /// [`GlobalAlloc::realloc`], forcing the default "alloc-new + copy + dealloc-old"
24+ /// path so that benchmarks never benefit from non-deterministic in-place growth
25+ /// provided by the underlying allocator's `realloc`. Wrapping `mimalloc::MiMalloc`
26+ /// (instead of using it directly) also keeps `alloc` / `dealloc` visible to
27+ /// CodSpeed's mimalloc white-box allocator tracking.
28+ struct NeverGrowInPlaceAllocator < A > {
29+ allocator : A ,
30+ }
31+
32+ impl < A > NeverGrowInPlaceAllocator < A > {
33+ const fn new ( allocator : A ) -> Self {
34+ Self { allocator }
35+ }
36+ }
1837
19- unsafe impl GlobalAlloc for NeverGrowInPlaceAllocator {
38+ // SAFETY: Methods simply delegate to the wrapped allocator.
39+ unsafe impl < A : GlobalAlloc > GlobalAlloc for NeverGrowInPlaceAllocator < A > {
2040 unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
21- unsafe { System . alloc ( layout) }
41+ self . allocator . alloc ( layout)
2242 }
2343
2444 unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
25- unsafe { System . dealloc ( ptr, layout) }
45+ self . allocator . dealloc ( ptr, layout)
2646 }
2747}
2848
0 commit comments