|
4 | 4 | #![allow(clippy::unwrap_used)] |
5 | 5 | #![allow(clippy::expect_used)] |
6 | 6 |
|
| 7 | +use std::alloc::{GlobalAlloc, Layout}; |
| 8 | + |
7 | 9 | use std::clone::Clone; |
8 | 10 | use std::fmt::Display; |
9 | 11 | use std::str::FromStr; |
@@ -63,9 +65,52 @@ pub use vortex::error::vortex_panic; |
63 | 65 | use vortex::io::session::RuntimeSessionExt; |
64 | 66 | use vortex::session::VortexSession; |
65 | 67 |
|
66 | | -// All benchmarks run with mimalloc for consistency. |
| 68 | +/// A wrapper around mimalloc that forces all allocations to be at least 256-byte aligned. |
| 69 | +/// |
| 70 | +/// This ensures that small heap objects (e.g. `ArcInner`) start on a cache-line boundary, |
| 71 | +/// preventing false sharing with neighboring allocations. |
| 72 | +struct CacheAlignedMiMalloc; |
| 73 | + |
| 74 | +unsafe impl GlobalAlloc for CacheAlignedMiMalloc { |
| 75 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 76 | + let aligned = layout |
| 77 | + .align_to(256) |
| 78 | + .expect("alignment overflow") |
| 79 | + .pad_to_align(); |
| 80 | + unsafe { mimalloc::MiMalloc.alloc(aligned) } |
| 81 | + } |
| 82 | + |
| 83 | + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| 84 | + let aligned = layout |
| 85 | + .align_to(256) |
| 86 | + .expect("alignment overflow") |
| 87 | + .pad_to_align(); |
| 88 | + unsafe { mimalloc::MiMalloc.dealloc(ptr, aligned) } |
| 89 | + } |
| 90 | + |
| 91 | + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { |
| 92 | + let aligned = layout |
| 93 | + .align_to(256) |
| 94 | + .expect("alignment overflow") |
| 95 | + .pad_to_align(); |
| 96 | + unsafe { mimalloc::MiMalloc.alloc_zeroed(aligned) } |
| 97 | + } |
| 98 | + |
| 99 | + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { |
| 100 | + let aligned = layout |
| 101 | + .align_to(256) |
| 102 | + .expect("alignment overflow") |
| 103 | + .pad_to_align(); |
| 104 | + let new_layout = Layout::from_size_align(new_size, 256) |
| 105 | + .expect("layout overflow") |
| 106 | + .pad_to_align(); |
| 107 | + unsafe { mimalloc::MiMalloc.realloc(ptr, aligned, new_layout.size()) } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// All benchmarks run with mimalloc (256-byte aligned) for consistency. |
67 | 112 | #[global_allocator] |
68 | | -static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; |
| 113 | +static GLOBAL: CacheAlignedMiMalloc = CacheAlignedMiMalloc; |
69 | 114 |
|
70 | 115 | pub static SESSION: LazyLock<VortexSession> = |
71 | 116 | LazyLock::new(|| VortexSession::default().with_tokio()); |
|
0 commit comments