Skip to content

Commit cd7cc32

Browse files
committed
overalign
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
1 parent 60847b0 commit cd7cc32

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

vortex-bench/src/lib.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![allow(clippy::unwrap_used)]
55
#![allow(clippy::expect_used)]
66

7+
use std::alloc::{GlobalAlloc, Layout};
8+
79
use std::clone::Clone;
810
use std::fmt::Display;
911
use std::str::FromStr;
@@ -63,9 +65,52 @@ pub use vortex::error::vortex_panic;
6365
use vortex::io::session::RuntimeSessionExt;
6466
use vortex::session::VortexSession;
6567

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.
67112
#[global_allocator]
68-
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
113+
static GLOBAL: CacheAlignedMiMalloc = CacheAlignedMiMalloc;
69114

70115
pub static SESSION: LazyLock<VortexSession> =
71116
LazyLock::new(|| VortexSession::default().with_tokio());

0 commit comments

Comments
 (0)