authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-28 23:16:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 19:49:08-07:00
loge22102dfc6356a16e83341d0cd0526762c696ad6
tree73751b59c4c93c30dced895f5bb9f4a84f5fcd8a
parentc2cc1b37928034cdcd49d07819fbb4f87683cf87

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.

8 files changed, 17 insertions(+), 29 deletions(-)

src/Compilation.zig+8-20
......@@ -45,9 +45,9 @@ pub const Config = @import("Compilation/Config.zig");
4545
4646/// General-purpose allocator. Used for both temporary and long-term storage.
4747gpa: Allocator,
48/// Arena-allocated memory, mostly used during initialization. However, it can be used
49/// for other things requiring the same lifetime as the `Compilation`.
50arena: std.heap.ArenaAllocator,
48/// Arena-allocated memory, mostly used during initialization. However, it can
49/// be used for other things requiring the same lifetime as the `Compilation`.
50arena: Allocator,
5151/// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.
5252/// TODO: rename to zcu: ?*Zcu
5353module: ?*Module,
......@@ -1178,7 +1178,7 @@ fn addModuleTableToCacheHash(
11781178 }
11791179}
11801180
1181pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
1181pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compilation {
11821182 const output_mode = options.config.output_mode;
11831183 const is_dyn_lib = switch (output_mode) {
11841184 .Obj, .Exe => false,
......@@ -1197,13 +1197,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
11971197 const have_zcu = options.config.have_zcu;
11981198
11991199 const comp: *Compilation = comp: {
1200 // For allocations that have the same lifetime as Compilation. This
1201 // arena is used only during this initialization and then is freed in
1202 // deinit().
1203 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
1204 errdefer arena_allocator.deinit();
1205 const arena = arena_allocator.allocator();
1206
12071200 // We put the `Compilation` itself in the arena. Freeing the arena will free the module.
12081201 // It's initialized later after we prepare the initialization options.
12091202 const root_name = try arena.dupeZ(u8, options.root_name);
......@@ -1454,7 +1447,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
14541447
14551448 comp.* = .{
14561449 .gpa = gpa,
1457 .arena = undefined, // populated after we are finished with `arena`
1450 .arena = arena,
14581451 .module = opt_zcu,
14591452 .cache_use = undefined, // populated below
14601453 .bin_file = null, // populated below
......@@ -1696,7 +1689,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
16961689 if (opt_zcu) |zcu| zcu.llvm_object = try LlvmObject.create(arena, comp);
16971690 }
16981691
1699 comp.arena = arena_allocator;
17001692 break :comp comp;
17011693 };
17021694 errdefer comp.destroy();
......@@ -1971,10 +1963,6 @@ pub fn destroy(comp: *Compilation) void {
19711963 comp.clearMiscFailures();
19721964
19731965 comp.cache_parent.manifest_dir.close();
1974
1975 // This destroys `comp`.
1976 var arena_instance = comp.arena;
1977 arena_instance.deinit();
19781966}
19791967
19801968pub fn clearMiscFailures(comp: *Compilation) void {
......@@ -4082,7 +4070,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module
40824070 };
40834071 }
40844072
4085 const out_zig_path = try comp.local_cache_directory.join(comp.arena.allocator(), &.{
4073 const out_zig_path = try comp.local_cache_directory.join(comp.arena, &.{
40864074 "o", &digest, cimport_zig_basename,
40874075 });
40884076 if (comp.verbose_cimport) {
......@@ -6302,7 +6290,7 @@ fn buildOutputFromZig(
63026290 .output_mode = output_mode,
63036291 });
63046292
6305 const sub_compilation = try Compilation.create(gpa, .{
6293 const sub_compilation = try Compilation.create(gpa, arena, .{
63066294 .global_cache_directory = comp.global_cache_directory,
63076295 .local_cache_directory = comp.global_cache_directory,
63086296 .zig_lib_directory = comp.zig_lib_directory,
......@@ -6411,7 +6399,7 @@ pub fn build_crt_file(
64116399 item.owner = root_mod;
64126400 }
64136401
6414 const sub_compilation = try Compilation.create(gpa, .{
6402 const sub_compilation = try Compilation.create(gpa, arena, .{
64156403 .local_cache_directory = comp.global_cache_directory,
64166404 .global_cache_directory = comp.global_cache_directory,
64176405 .zig_lib_directory = comp.zig_lib_directory,
src/Sema.zig+1-1
......@@ -5761,7 +5761,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57615761 return sema.failWithOwnedErrorMsg(&child_block, msg);
57625762 }
57635763 const parent_mod = parent_block.ownerModule();
5764 const c_import_mod = Package.Module.create(comp.arena.allocator(), .{
5764 const c_import_mod = Package.Module.create(comp.arena, .{
57655765 .global_cache_directory = comp.global_cache_directory,
57665766 .paths = .{
57675767 .root = .{
src/glibc.zig+1-1
......@@ -1116,7 +1116,7 @@ fn buildSharedLib(
11161116 },
11171117 };
11181118
1119 const sub_compilation = try Compilation.create(comp.gpa, .{
1119 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
11201120 .local_cache_directory = zig_cache_directory,
11211121 .global_cache_directory = comp.global_cache_directory,
11221122 .zig_lib_directory = comp.zig_lib_directory,
src/libcxx.zig+2-2
......@@ -272,7 +272,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
272272 });
273273 }
274274
275 const sub_compilation = try Compilation.create(comp.gpa, .{
275 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
276276 .local_cache_directory = comp.global_cache_directory,
277277 .global_cache_directory = comp.global_cache_directory,
278278 .zig_lib_directory = comp.zig_lib_directory,
......@@ -459,7 +459,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
459459 });
460460 }
461461
462 const sub_compilation = try Compilation.create(comp.gpa, .{
462 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
463463 .local_cache_directory = comp.global_cache_directory,
464464 .global_cache_directory = comp.global_cache_directory,
465465 .zig_lib_directory = comp.zig_lib_directory,
src/libtsan.zig+1-1
......@@ -245,7 +245,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
245245 });
246246 }
247247
248 const sub_compilation = try Compilation.create(comp.gpa, .{
248 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
249249 .local_cache_directory = comp.global_cache_directory,
250250 .global_cache_directory = comp.global_cache_directory,
251251 .zig_lib_directory = comp.zig_lib_directory,
src/libunwind.zig+1-1
......@@ -123,7 +123,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
123123 .owner = root_mod,
124124 };
125125 }
126 const sub_compilation = try Compilation.create(comp.gpa, .{
126 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
127127 .self_exe_path = comp.self_exe_path,
128128 .local_cache_directory = comp.global_cache_directory,
129129 .global_cache_directory = comp.global_cache_directory,
src/main.zig+2-2
......@@ -3130,7 +3130,7 @@ fn buildOutputType(
31303130
31313131 gimmeMoreOfThoseSweetSweetFileDescriptors();
31323132
3133 const comp = Compilation.create(gpa, .{
3133 const comp = Compilation.create(gpa, arena, .{
31343134 .zig_lib_directory = zig_lib_directory,
31353135 .local_cache_directory = local_cache_directory,
31363136 .global_cache_directory = global_cache_directory,
......@@ -5508,7 +5508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
55085508
55095509 try root_mod.deps.put(arena, "@build", build_mod);
55105510
5511 const comp = Compilation.create(gpa, .{
5511 const comp = Compilation.create(gpa, arena, .{
55125512 .zig_lib_directory = zig_lib_directory,
55135513 .local_cache_directory = local_cache_directory,
55145514 .global_cache_directory = global_cache_directory,
src/musl.zig+1-1
......@@ -251,7 +251,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
251251 .builtin_mod = null,
252252 });
253253
254 const sub_compilation = try Compilation.create(comp.gpa, .{
254 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
255255 .local_cache_directory = comp.global_cache_directory,
256256 .global_cache_directory = comp.global_cache_directory,
257257 .zig_lib_directory = comp.zig_lib_directory,