Skip to content

Commit

Permalink
experiment: don't do aligned allocation when unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Dec 23, 2024
1 parent 4f8a6b3 commit ba55688
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/allocators/mimalloc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,20 @@ pub const MI_SMALL_SIZE_MAX = MI_SMALL_WSIZE_MAX * @import("std").zig.c_translat
pub const MI_ALIGNMENT_MAX = (@as(c_int, 16) * @as(c_int, 1024)) * @as(c_ulong, 1024);

const std = @import("std");
pub fn canUseAlignedAlloc(len: usize, alignment: usize) bool {
return alignment > 0 and std.math.isPowerOfTwo(alignment) and !mi_malloc_satisfies_alignment(alignment, len);
pub fn canUseAlignedAlloc(_: usize, alignment: usize) bool {
// Must be nonzero, power-of-two, etc.
if (alignment == 0 or !std.math.isPowerOfTwo(alignment)) {
return false;
}

// Our "native alignment" check:
// Suppose we consider anything up to 16 bytes as "already satisfied" by mimalloc.
// (mimalloc often aligns small allocations to 16 bytes on 64-bit.)
if (alignment <= MI_MAX_ALIGN_SIZE) {
return false; // no need for aligned allocation
}

return false;
}
const MI_MAX_ALIGN_SIZE = 16;
inline fn mi_malloc_satisfies_alignment(alignment: usize, size: usize) bool {
Expand Down

0 comments on commit ba55688

Please sign in to comment.