Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: don't do aligned allocation when unnecessary #15956

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 true;
}
const MI_MAX_ALIGN_SIZE = 16;
inline fn mi_malloc_satisfies_alignment(alignment: usize, size: usize) bool {
Expand Down
Loading