From e22102dfc6356a16e83341d0cd0526762c696ad6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Dec 2023 23:16:31 -0700 Subject: [PATCH] Compilation: make create() take an arena allocator Instead of making its own inside create. 10 out of 10 calls to create() had already an arena in scope, so this commit means that 10 instances of Compilation now reuse an existing arena with the same lifetime rather than creating a redundant one. In other words, this very slightly optimizes initialization of the frontend in terms of memory allocation. --- src/Compilation.zig | 28 ++++++++-------------------- src/Sema.zig | 2 +- src/glibc.zig | 2 +- src/libcxx.zig | 4 ++-- src/libtsan.zig | 2 +- src/libunwind.zig | 2 +- src/main.zig | 4 ++-- src/musl.zig | 2 +- 8 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index e086e23830..66c136d076 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -45,9 +45,9 @@ pub const Config = @import("Compilation/Config.zig"); /// General-purpose allocator. Used for both temporary and long-term storage. gpa: Allocator, -/// Arena-allocated memory, mostly used during initialization. However, it can be used -/// for other things requiring the same lifetime as the `Compilation`. -arena: std.heap.ArenaAllocator, +/// Arena-allocated memory, mostly used during initialization. However, it can +/// be used for other things requiring the same lifetime as the `Compilation`. +arena: Allocator, /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. /// TODO: rename to zcu: ?*Zcu module: ?*Module, @@ -1178,7 +1178,7 @@ fn addModuleTableToCacheHash( } } -pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { +pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compilation { const output_mode = options.config.output_mode; const is_dyn_lib = switch (output_mode) { .Obj, .Exe => false, @@ -1197,13 +1197,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { const have_zcu = options.config.have_zcu; const comp: *Compilation = comp: { - // For allocations that have the same lifetime as Compilation. This - // arena is used only during this initialization and then is freed in - // deinit(). - var arena_allocator = std.heap.ArenaAllocator.init(gpa); - errdefer arena_allocator.deinit(); - const arena = arena_allocator.allocator(); - // We put the `Compilation` itself in the arena. Freeing the arena will free the module. // It's initialized later after we prepare the initialization options. const root_name = try arena.dupeZ(u8, options.root_name); @@ -1454,7 +1447,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { comp.* = .{ .gpa = gpa, - .arena = undefined, // populated after we are finished with `arena` + .arena = arena, .module = opt_zcu, .cache_use = undefined, // populated below .bin_file = null, // populated below @@ -1696,7 +1689,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { if (opt_zcu) |zcu| zcu.llvm_object = try LlvmObject.create(arena, comp); } - comp.arena = arena_allocator; break :comp comp; }; errdefer comp.destroy(); @@ -1971,10 +1963,6 @@ pub fn destroy(comp: *Compilation) void { comp.clearMiscFailures(); comp.cache_parent.manifest_dir.close(); - - // This destroys `comp`. - var arena_instance = comp.arena; - arena_instance.deinit(); } pub fn clearMiscFailures(comp: *Compilation) void { @@ -4082,7 +4070,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module }; } - const out_zig_path = try comp.local_cache_directory.join(comp.arena.allocator(), &.{ + const out_zig_path = try comp.local_cache_directory.join(comp.arena, &.{ "o", &digest, cimport_zig_basename, }); if (comp.verbose_cimport) { @@ -6302,7 +6290,7 @@ fn buildOutputFromZig( .output_mode = output_mode, }); - const sub_compilation = try Compilation.create(gpa, .{ + const sub_compilation = try Compilation.create(gpa, arena, .{ .global_cache_directory = comp.global_cache_directory, .local_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, @@ -6411,7 +6399,7 @@ pub fn build_crt_file( item.owner = root_mod; } - const sub_compilation = try Compilation.create(gpa, .{ + const sub_compilation = try Compilation.create(gpa, arena, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, diff --git a/src/Sema.zig b/src/Sema.zig index 3a54b20ea4..a01dd174a8 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -5761,7 +5761,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr return sema.failWithOwnedErrorMsg(&child_block, msg); } const parent_mod = parent_block.ownerModule(); - const c_import_mod = Package.Module.create(comp.arena.allocator(), .{ + const c_import_mod = Package.Module.create(comp.arena, .{ .global_cache_directory = comp.global_cache_directory, .paths = .{ .root = .{ diff --git a/src/glibc.zig b/src/glibc.zig index 838f55c879..00d76f32fb 100644 --- a/src/glibc.zig +++ b/src/glibc.zig @@ -1116,7 +1116,7 @@ fn buildSharedLib( }, }; - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .local_cache_directory = zig_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, diff --git a/src/libcxx.zig b/src/libcxx.zig index abcbc0187e..8e28c2174d 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -272,7 +272,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void { }); } - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, @@ -459,7 +459,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { }); } - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, diff --git a/src/libtsan.zig b/src/libtsan.zig index a736c32713..14a7db0888 100644 --- a/src/libtsan.zig +++ b/src/libtsan.zig @@ -245,7 +245,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void { }); } - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, diff --git a/src/libunwind.zig b/src/libunwind.zig index 0f23f8b352..5f0613c361 100644 --- a/src/libunwind.zig +++ b/src/libunwind.zig @@ -123,7 +123,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void { .owner = root_mod, }; } - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .self_exe_path = comp.self_exe_path, .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, diff --git a/src/main.zig b/src/main.zig index e026e3d7ac..091e4b2668 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3130,7 +3130,7 @@ fn buildOutputType( gimmeMoreOfThoseSweetSweetFileDescriptors(); - const comp = Compilation.create(gpa, .{ + const comp = Compilation.create(gpa, arena, .{ .zig_lib_directory = zig_lib_directory, .local_cache_directory = local_cache_directory, .global_cache_directory = global_cache_directory, @@ -5508,7 +5508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi try root_mod.deps.put(arena, "@build", build_mod); - const comp = Compilation.create(gpa, .{ + const comp = Compilation.create(gpa, arena, .{ .zig_lib_directory = zig_lib_directory, .local_cache_directory = local_cache_directory, .global_cache_directory = global_cache_directory, diff --git a/src/musl.zig b/src/musl.zig index 4a62d5eded..c244257a70 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -251,7 +251,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr .builtin_mod = null, }); - const sub_compilation = try Compilation.create(comp.gpa, .{ + const sub_compilation = try Compilation.create(comp.gpa, arena, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory,