diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-12-28 23:16:31 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-01-01 19:49:08 -0700 |
| commit | e22102dfc6356a16e83341d0cd0526762c696ad6 (patch) | |
| tree | 73751b59c4c93c30dced895f5bb9f4a84f5fcd8a /src/Compilation.zig | |
| parent | c2cc1b37928034cdcd49d07819fbb4f87683cf87 (diff) | |
| download | zig-e22102dfc6356a16e83341d0cd0526762c696ad6.tar.gz zig-e22102dfc6356a16e83341d0cd0526762c696ad6.zip | |
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.
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 28 |
1 files changed, 8 insertions, 20 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, |
