Skip to content

Commit

Permalink
refactor: consolidate allocators (#16061)
Browse files Browse the repository at this point in the history
Co-authored-by: DonIsaac <[email protected]>
  • Loading branch information
DonIsaac and DonIsaac authored Jan 8, 2025
1 parent 9757ee4 commit a733421
Show file tree
Hide file tree
Showing 21 changed files with 43 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! A nullable allocator the same size as `std.mem.Allocator`.
const std = @import("std");
const NullableAllocator = @This();
const bun = @import("root").bun;

const NullableAllocator = @This();

ptr: *anyopaque = undefined,
// Utilize the null pointer optimization on the vtable instead of
// the regular ptr because some allocator implementations might tag their
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ pub const LinuxMemFdAllocator = struct {
}

pub fn deref(this: *LinuxMemFdAllocator) void {
if (this.ref_count.fetchSub(1, .monotonic) == 1) {
_ = bun.sys.close(this.fd);
this.destroy();
switch (this.ref_count.fetchSub(1, .monotonic)) {
1 => {
_ = bun.sys.close(this.fd);
this.destroy();
},
0 => {
// TODO: @branchHint(.cold) after Zig 0.14 upgrade
if (comptime bun.Environment.isDebug) {
std.debug.panic("LinuxMemFdAllocator ref_count underflow", .{});
}
},
else => {},
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const bun = @import("root").bun;
const log = bun.Output.scoped(.mimalloc, true);
const assert = bun.assert;
const Allocator = mem.Allocator;
const mimalloc = @import("./allocators/mimalloc.zig");
const FeatureFlags = @import("./feature_flags.zig");
const Environment = @import("./env.zig");
const mimalloc = @import("./mimalloc.zig");
const FeatureFlags = @import("../feature_flags.zig");
const Environment = @import("../env.zig");

fn mimalloc_free(
_: *anyopaque,
Expand Down
6 changes: 3 additions & 3 deletions src/mimalloc_arena.zig → src/allocators/mimalloc_arena.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const mem = @import("std").mem;
const builtin = @import("std").builtin;
const std = @import("std");

const mimalloc = @import("./allocators/mimalloc.zig");
const Environment = @import("./env.zig");
const FeatureFlags = @import("./feature_flags.zig");
const mimalloc = @import("./mimalloc.zig");
const Environment = @import("../env.zig");
const FeatureFlags = @import("../feature_flags.zig");
const Allocator = mem.Allocator;
const assert = bun.assert;
const bun = @import("root").bun;
Expand Down
2 changes: 1 addition & 1 deletion src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4485,5 +4485,5 @@ const JSModuleLoader = JSC.JSModuleLoader;
const EventLoopHandle = JSC.EventLoopHandle;
const JSInternalPromise = JSC.JSInternalPromise;

const ThreadlocalArena = @import("../mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const Chunk = bun.bundle_v2.Chunk;
2 changes: 1 addition & 1 deletion src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const JSAst = bun.JSAst;
const JSParser = bun.js_parser;
const JSPrinter = bun.js_printer;
const ScanPassResult = JSParser.ScanPassResult;
const Mimalloc = @import("../../mimalloc_arena.zig");
const Mimalloc = @import("../../allocators/mimalloc_arena.zig");
const Runtime = @import("../../runtime.zig").Runtime;
const JSLexer = bun.js_lexer;
const Expr = JSAst.Expr;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const JSAst = bun.JSAst;
const JSParser = bun.js_parser;
const JSPrinter = bun.js_printer;
const ScanPassResult = JSParser.ScanPassResult;
const Mimalloc = @import("../../mimalloc_arena.zig");
const Mimalloc = @import("../../allocators/mimalloc_arena.zig");
const Runtime = @import("../../runtime.zig").Runtime;
const JSLexer = bun.js_lexer;
const Expr = JSAst.Expr;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const Fallback = Runtime.Fallback;
const MimeType = HTTP.MimeType;
const Blob = JSC.WebCore.Blob;
const BoringSSL = bun.BoringSSL;
const Arena = @import("../../mimalloc_arena.zig").Arena;
const Arena = @import("../../allocators/mimalloc_arena.zig").Arena;
const SendfileContext = struct {
fd: bun.FileDescriptor,
socket_fd: bun.FileDescriptor = bun.invalid_fd,
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const ErrorableString = bun.JSC.ErrorableString;
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const C = bun.C;

const Exception = bun.JSC.Exception;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const C = bun.C;

const Allocator = std.mem.Allocator;
Expand Down
16 changes: 8 additions & 8 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ pub const use_mimalloc = true;
pub const default_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").c_allocator;
@import("./allocators/memory_allocator.zig").c_allocator;

/// Zeroing memory allocator
pub const z_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").z_allocator;
@import("./allocators/memory_allocator.zig").z_allocator;

pub const huge_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").huge_allocator;
@import("./allocators/memory_allocator.zig").huge_allocator;

pub const auto_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").auto_allocator;
@import("./allocators/memory_allocator.zig").auto_allocator;

pub const callmod_inline: std.builtin.CallModifier = if (builtin.mode == .Debug) .auto else .always_inline;
pub const callconv_inline: std.builtin.CallingConvention = if (builtin.mode == .Debug) .Unspecified else .Inline;
Expand Down Expand Up @@ -556,7 +556,7 @@ pub const StringBuilder = @import("./string_builder.zig");

pub const LinearFifo = @import("./linear_fifo.zig").LinearFifo;
pub const linux = struct {
pub const memfd_allocator = @import("./linux_memfd_allocator.zig").LinuxMemFdAllocator;
pub const memfd_allocator = @import("./allocators/linux_memfd_allocator.zig").LinuxMemFdAllocator;
};

/// hash a string
Expand Down Expand Up @@ -887,7 +887,7 @@ pub fn openDirAbsoluteNotForDeletingOrRenaming(path_: []const u8) !std.fs.Dir {
return fd.asDir();
}

pub const MimallocArena = @import("./mimalloc_arena.zig").Arena;
pub const MimallocArena = @import("./allocators/mimalloc_arena.zig").Arena;
pub fn getRuntimeFeatureFlag(comptime flag: [:0]const u8) bool {
return struct {
const state = enum(u8) { idk, disabled, enabled };
Expand Down Expand Up @@ -1607,7 +1607,7 @@ pub const fast_debug_build_mode = fast_debug_build_cmd != .None and

pub const MultiArrayList = @import("./multi_array_list.zig").MultiArrayList;
pub const StringJoiner = @import("./StringJoiner.zig");
pub const NullableAllocator = @import("./NullableAllocator.zig");
pub const NullableAllocator = @import("./allocators/NullableAllocator.zig");

pub const renamer = @import("./renamer.zig");
// TODO: Rename to SourceMap as this is a struct.
Expand Down Expand Up @@ -2044,7 +2044,7 @@ pub fn HiveRef(comptime T: type, comptime capacity: u16) type {
};
}

pub const MaxHeapAllocator = @import("./max_heap_allocator.zig").MaxHeapAllocator;
pub const MaxHeapAllocator = @import("./allocators/max_heap_allocator.zig").MaxHeapAllocator;

pub const tracy = @import("./tracy.zig");
pub const trace = tracy.trace;
Expand Down
2 changes: 1 addition & 1 deletion src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DotEnv = @import("env_loader.zig");
const which = @import("which.zig").which;
const JSC = bun.JSC;
const AsyncHTTP = bun.http.AsyncHTTP;
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;

const OpaqueWrap = JSC.OpaqueWrap;
const VirtualMachine = JSC.VirtualMachine;
Expand Down
2 changes: 1 addition & 1 deletion src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const Ref = @import("../ast/base.zig").Ref;
const Define = @import("../defines.zig").Define;
const DebugOptions = @import("../cli.zig").Command.DebugOptions;
const ThreadPoolLib = @import("../thread_pool.zig");
const ThreadlocalArena = @import("../mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const BabyList = @import("../baby_list.zig").BabyList;
const Fs = @import("../fs.zig");
const schema = @import("../api/schema.zig");
Expand Down
2 changes: 1 addition & 1 deletion src/css/css_internals.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const bun = @import("root").bun;
const std = @import("std");
const builtin = @import("builtin");
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const JSC = bun.JSC;
Expand Down
2 changes: 1 addition & 1 deletion src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ThreadPool = bun.ThreadPool;
const ObjectPool = @import("./pool.zig").ObjectPool;
const posix = std.posix;
const SOCK = posix.SOCK;
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;
const ZlibPool = @import("./http/zlib.zig");
const BoringSSL = bun.BoringSSL;
const Progress = bun.Progress;
Expand Down
2 changes: 1 addition & 1 deletion src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const clap = bun.clap;
const ExtractTarball = @import("./extract_tarball.zig");
pub const Npm = @import("./npm.zig");
const Bitset = bun.bit_set.DynamicBitSetUnmanaged;
const z_allocator = @import("../memory_allocator.zig").z_allocator;
const z_allocator = @import("../allocators/memory_allocator.zig").z_allocator;
const Syscall = bun.sys;
const RunCommand = @import("../cli/run_command.zig").RunCommand;
const PackageManagerCommand = @import("../cli/package_manager_command.zig").PackageManagerCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/install/lockfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const clap = bun.clap;
const ExtractTarball = @import("./extract_tarball.zig");
const Npm = @import("./npm.zig");
const Bitset = bun.bit_set.DynamicBitSetUnmanaged;
const z_allocator = @import("../memory_allocator.zig").z_allocator;
const z_allocator = @import("../allocators/memory_allocator.zig").z_allocator;
const Lockfile = @This();

const IdentityContext = @import("../identity_context.zig").IdentityContext;
Expand Down
2 changes: 1 addition & 1 deletion src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ComptimeStringMap = bun.ComptimeStringMap;
const JSPrinter = @import("./js_printer.zig");
const js_lexer = @import("./js_lexer.zig");
const TypeScript = @import("./js_parser.zig").TypeScript;
const ThreadlocalArena = @import("./mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("./allocators/mimalloc_arena.zig").Arena;
const MimeType = bun.http.MimeType;
const OOM = bun.OOM;
const Loader = bun.options.Loader;
Expand Down
2 changes: 1 addition & 1 deletion src/main_wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export fn init(heapsize: u32) void {
buffer_writer = writer.ctx;
}
}
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;

var log: Logger.Log = undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/resolver/resolve_path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const tester = @import("../test/tester.zig");
const std = @import("std");
const strings = @import("../string_immutable.zig");
const FeatureFlags = @import("../feature_flags.zig");
const default_allocator = @import("../memory_allocator.zig").c_allocator;
const default_allocator = @import("../allocators/memory_allocator.zig").c_allocator;
const bun = @import("root").bun;
const Fs = @import("../fs.zig");

Expand Down

0 comments on commit a733421

Please sign in to comment.