Simplify mimalloc alignment check (#21497)

This slightly reduces memory use.

Maximum memory use of `bun test html-rewriter`, averaged across 100
iterations:

* 101975 kB without this change
* 101634 kB with this change

I also tried changing the code to always use the aligned allocation
functions, but this slightly increased memory use, to 102160 kB.

(For internal tracking: fixes ENG-19866)
This commit is contained in:
taylor.fish
2025-07-30 14:30:47 -07:00
committed by GitHub
parent 3de884f2c9
commit a1f44caa87
4 changed files with 10 additions and 13 deletions

View File

@@ -13,7 +13,7 @@ fn mimalloc_free(
// but its good to have that assertion
// let's only enable it in debug mode
if (comptime Environment.isDebug) {
if (mimalloc.canUseAlignedAlloc(buf.len, alignment.toByteUnits()))
if (mimalloc.mustUseAlignedAlloc(alignment))
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, alignment.toByteUnits())
else
mimalloc.mi_free_size(buf.ptr, buf.len);
@@ -28,7 +28,7 @@ const MimallocAllocator = struct {
if (comptime Environment.enable_logs)
log("mi_alloc({d}, {d})", .{ len, alignment.toByteUnits() });
const ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment.toByteUnits()))
const ptr: ?*anyopaque = if (mimalloc.mustUseAlignedAlloc(alignment))
mimalloc.mi_malloc_aligned(len, alignment.toByteUnits())
else
mimalloc.mi_malloc(len);
@@ -82,7 +82,7 @@ const ZAllocator = struct {
fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
log("ZAllocator.alignedAlloc: {d}\n", .{len});
const ptr = if (mimalloc.canUseAlignedAlloc(len, alignment.toByteUnits()))
const ptr = if (mimalloc.mustUseAlignedAlloc(alignment))
mimalloc.mi_zalloc_aligned(len, alignment.toByteUnits())
else
mimalloc.mi_zalloc(len);