From a733421261a25a6266b04f7327362ce6a65258eb Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Wed, 8 Jan 2025 16:39:13 -0600 Subject: [PATCH] refactor: consolidate allocators (#16061) Co-authored-by: DonIsaac --- src/{ => allocators}/NullableAllocator.zig | 3 ++- src/{ => allocators}/linux_memfd_allocator.zig | 15 ++++++++++++--- src/{ => allocators}/max_heap_allocator.zig | 0 src/{ => allocators}/memory_allocator.zig | 6 +++--- src/{ => allocators}/mimalloc_arena.zig | 6 +++--- src/bake/DevServer.zig | 2 +- src/bun.js/api/JSBundler.zig | 2 +- src/bun.js/api/JSTranspiler.zig | 2 +- src/bun.js/api/server.zig | 2 +- src/bun.js/javascript.zig | 2 +- src/bun.js/module_loader.zig | 2 +- src/bun.zig | 16 ++++++++-------- src/bun_js.zig | 2 +- src/bundler/bundle_v2.zig | 2 +- src/css/css_internals.zig | 2 +- src/http.zig | 2 +- src/install/install.zig | 2 +- src/install/lockfile.zig | 2 +- src/js_ast.zig | 2 +- src/main_wasm.zig | 2 +- src/resolver/resolve_path.zig | 2 +- 21 files changed, 43 insertions(+), 33 deletions(-) rename src/{ => allocators}/NullableAllocator.zig (99%) rename src/{ => allocators}/linux_memfd_allocator.zig (93%) rename src/{ => allocators}/max_heap_allocator.zig (100%) rename src/{ => allocators}/memory_allocator.zig (97%) rename src/{ => allocators}/mimalloc_arena.zig (98%) diff --git a/src/NullableAllocator.zig b/src/allocators/NullableAllocator.zig similarity index 99% rename from src/NullableAllocator.zig rename to src/allocators/NullableAllocator.zig index 289eb98f7d7747..ccbf9725796e3d 100644 --- a/src/NullableAllocator.zig +++ b/src/allocators/NullableAllocator.zig @@ -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 diff --git a/src/linux_memfd_allocator.zig b/src/allocators/linux_memfd_allocator.zig similarity index 93% rename from src/linux_memfd_allocator.zig rename to src/allocators/linux_memfd_allocator.zig index c2ec4f7f07cdae..0206fd9398ef60 100644 --- a/src/linux_memfd_allocator.zig +++ b/src/allocators/linux_memfd_allocator.zig @@ -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 => {}, } } diff --git a/src/max_heap_allocator.zig b/src/allocators/max_heap_allocator.zig similarity index 100% rename from src/max_heap_allocator.zig rename to src/allocators/max_heap_allocator.zig diff --git a/src/memory_allocator.zig b/src/allocators/memory_allocator.zig similarity index 97% rename from src/memory_allocator.zig rename to src/allocators/memory_allocator.zig index 18f7e651cce2b9..07b84ef7b3476f 100644 --- a/src/memory_allocator.zig +++ b/src/allocators/memory_allocator.zig @@ -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, diff --git a/src/mimalloc_arena.zig b/src/allocators/mimalloc_arena.zig similarity index 98% rename from src/mimalloc_arena.zig rename to src/allocators/mimalloc_arena.zig index d44ba21b765dab..48d0a30acc5d9b 100644 --- a/src/mimalloc_arena.zig +++ b/src/allocators/mimalloc_arena.zig @@ -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; diff --git a/src/bake/DevServer.zig b/src/bake/DevServer.zig index 5f59b524011936..09c6cbf9971d2a 100644 --- a/src/bake/DevServer.zig +++ b/src/bake/DevServer.zig @@ -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; diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index a75e260f72ced4..75efdc43c3f38d 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -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; diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index 77d642b3f94652..0a2462cc77758b 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -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; diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index c2ab4bcfb0378d..686bdaf49f67ad 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -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, diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index e3f0b6f5a5d7df..57bd18ade7b168 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -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; diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 8b7103acf1cfe1..db1272731814ec 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -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; diff --git a/src/bun.zig b/src/bun.zig index 6bb9b890a5d5c4..0dd0dfdfad3d14 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -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; @@ -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 @@ -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 }; @@ -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. @@ -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; diff --git a/src/bun_js.zig b/src/bun_js.zig index 7ac6f5d26c19de..dae06365549e5d 100644 --- a/src/bun_js.zig +++ b/src/bun_js.zig @@ -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; diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index 99d7e0a03a7c27..6cd32b15dda9de 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -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"); diff --git a/src/css/css_internals.zig b/src/css/css_internals.zig index 09a86a80a08fc6..4c0d86dc45cd1c 100644 --- a/src/css/css_internals.zig +++ b/src/css/css_internals.zig @@ -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; diff --git a/src/http.zig b/src/http.zig index 47339a45435814..49e077a0376945 100644 --- a/src/http.zig +++ b/src/http.zig @@ -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; diff --git a/src/install/install.zig b/src/install/install.zig index 546dbf2ef478b7..6464ccac495778 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -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; diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index dad78519540bfe..63b21143522d1c 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -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; diff --git a/src/js_ast.zig b/src/js_ast.zig index 4263c9a60c8517..331e0bbc1d84f7 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -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; diff --git a/src/main_wasm.zig b/src/main_wasm.zig index 3a1cfc5c833573..f94e2bc914001c 100644 --- a/src/main_wasm.zig +++ b/src/main_wasm.zig @@ -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; diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 46f2dcd03a4aff..daa531a1c7618b 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -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");