| author | |
| committer | |
| log | e22102dfc6356a16e83341d0cd0526762c696ad6 |
| tree | 73751b59c4c93c30dced895f5bb9f4a84f5fcd8a |
| parent | c2cc1b37928034cdcd49d07819fbb4f87683cf87 |
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"); | ... | @@ -45,9 +45,9 @@ pub const Config = @import("Compilation/Config.zig"); |
| 45 | 45 | ||
| 46 | /// General-purpose allocator. Used for both temporary and long-term storage. | 46 | /// General-purpose allocator. Used for both temporary and long-term storage. |
| 47 | gpa: Allocator, | 47 | gpa: Allocator, |
| 48 | /// Arena-allocated memory, mostly used during initialization. However, it can be used | 48 | /// Arena-allocated memory, mostly used during initialization. However, it can |
| 49 | /// for other things requiring the same lifetime as the `Compilation`. | 49 | /// be used for other things requiring the same lifetime as the `Compilation`. |
| 50 | arena: std.heap.ArenaAllocator, | 50 | arena: Allocator, |
| 51 | /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. | 51 | /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. |
| 52 | /// TODO: rename to zcu: ?*Zcu | 52 | /// TODO: rename to zcu: ?*Zcu |
| 53 | module: ?*Module, | 53 | module: ?*Module, |
| ... | @@ -1178,7 +1178,7 @@ fn addModuleTableToCacheHash( | ... | @@ -1178,7 +1178,7 @@ fn addModuleTableToCacheHash( |
| 1178 | } | 1178 | } |
| 1179 | } | 1179 | } |
| 1180 | 1180 | ||
| 1181 | pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { | 1181 | pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compilation { |
| 1182 | const output_mode = options.config.output_mode; | 1182 | const output_mode = options.config.output_mode; |
| 1183 | const is_dyn_lib = switch (output_mode) { | 1183 | const is_dyn_lib = switch (output_mode) { |
| 1184 | .Obj, .Exe => false, | 1184 | .Obj, .Exe => false, |
| ... | @@ -1197,13 +1197,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { | ... | @@ -1197,13 +1197,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { |
| 1197 | const have_zcu = options.config.have_zcu; | 1197 | const have_zcu = options.config.have_zcu; |
| 1198 | 1198 | ||
| 1199 | const comp: *Compilation = comp: { | 1199 | 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 | |||
| 1207 | // We put the `Compilation` itself in the arena. Freeing the arena will free the module. | 1200 | // We put the `Compilation` itself in the arena. Freeing the arena will free the module. |
| 1208 | // It's initialized later after we prepare the initialization options. | 1201 | // It's initialized later after we prepare the initialization options. |
| 1209 | const root_name = try arena.dupeZ(u8, options.root_name); | 1202 | const root_name = try arena.dupeZ(u8, options.root_name); |
| ... | @@ -1454,7 +1447,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { | ... | @@ -1454,7 +1447,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { |
| 1454 | 1447 | ||
| 1455 | comp.* = .{ | 1448 | comp.* = .{ |
| 1456 | .gpa = gpa, | 1449 | .gpa = gpa, |
| 1457 | .arena = undefined, // populated after we are finished with `arena` | 1450 | .arena = arena, |
| 1458 | .module = opt_zcu, | 1451 | .module = opt_zcu, |
| 1459 | .cache_use = undefined, // populated below | 1452 | .cache_use = undefined, // populated below |
| 1460 | .bin_file = null, // populated below | 1453 | .bin_file = null, // populated below |
| ... | @@ -1696,7 +1689,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { | ... | @@ -1696,7 +1689,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { |
| 1696 | if (opt_zcu) |zcu| zcu.llvm_object = try LlvmObject.create(arena, comp); | 1689 | if (opt_zcu) |zcu| zcu.llvm_object = try LlvmObject.create(arena, comp); |
| 1697 | } | 1690 | } |
| 1698 | 1691 | ||
| 1699 | comp.arena = arena_allocator; | ||
| 1700 | break :comp comp; | 1692 | break :comp comp; |
| 1701 | }; | 1693 | }; |
| 1702 | errdefer comp.destroy(); | 1694 | errdefer comp.destroy(); |
| ... | @@ -1971,10 +1963,6 @@ pub fn destroy(comp: *Compilation) void { | ... | @@ -1971,10 +1963,6 @@ pub fn destroy(comp: *Compilation) void { |
| 1971 | comp.clearMiscFailures(); | 1963 | comp.clearMiscFailures(); |
| 1972 | 1964 | ||
| 1973 | comp.cache_parent.manifest_dir.close(); | 1965 | comp.cache_parent.manifest_dir.close(); |
| 1974 | |||
| 1975 | // This destroys `comp`. | ||
| 1976 | var arena_instance = comp.arena; | ||
| 1977 | arena_instance.deinit(); | ||
| 1978 | } | 1966 | } |
| 1979 | 1967 | ||
| 1980 | pub fn clearMiscFailures(comp: *Compilation) void { | 1968 | pub fn clearMiscFailures(comp: *Compilation) void { |
| ... | @@ -4082,7 +4070,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module | ... | @@ -4082,7 +4070,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module |
| 4082 | }; | 4070 | }; |
| 4083 | } | 4071 | } |
| 4084 | 4072 | ||
| 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, &.{ |
| 4086 | "o", &digest, cimport_zig_basename, | 4074 | "o", &digest, cimport_zig_basename, |
| 4087 | }); | 4075 | }); |
| 4088 | if (comp.verbose_cimport) { | 4076 | if (comp.verbose_cimport) { |
| ... | @@ -6302,7 +6290,7 @@ fn buildOutputFromZig( | ... | @@ -6302,7 +6290,7 @@ fn buildOutputFromZig( |
| 6302 | .output_mode = output_mode, | 6290 | .output_mode = output_mode, |
| 6303 | }); | 6291 | }); |
| 6304 | 6292 | ||
| 6305 | const sub_compilation = try Compilation.create(gpa, .{ | 6293 | const sub_compilation = try Compilation.create(gpa, arena, .{ |
| 6306 | .global_cache_directory = comp.global_cache_directory, | 6294 | .global_cache_directory = comp.global_cache_directory, |
| 6307 | .local_cache_directory = comp.global_cache_directory, | 6295 | .local_cache_directory = comp.global_cache_directory, |
| 6308 | .zig_lib_directory = comp.zig_lib_directory, | 6296 | .zig_lib_directory = comp.zig_lib_directory, |
| ... | @@ -6411,7 +6399,7 @@ pub fn build_crt_file( | ... | @@ -6411,7 +6399,7 @@ pub fn build_crt_file( |
| 6411 | item.owner = root_mod; | 6399 | item.owner = root_mod; |
| 6412 | } | 6400 | } |
| 6413 | 6401 | ||
| 6414 | const sub_compilation = try Compilation.create(gpa, .{ | 6402 | const sub_compilation = try Compilation.create(gpa, arena, .{ |
| 6415 | .local_cache_directory = comp.global_cache_directory, | 6403 | .local_cache_directory = comp.global_cache_directory, |
| 6416 | .global_cache_directory = comp.global_cache_directory, | 6404 | .global_cache_directory = comp.global_cache_directory, |
| 6417 | .zig_lib_directory = comp.zig_lib_directory, | 6405 | .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 | ... | @@ -5761,7 +5761,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5761 | return sema.failWithOwnedErrorMsg(&child_block, msg); | 5761 | return sema.failWithOwnedErrorMsg(&child_block, msg); |
| 5762 | } | 5762 | } |
| 5763 | const parent_mod = parent_block.ownerModule(); | 5763 | 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, .{ |
| 5765 | .global_cache_directory = comp.global_cache_directory, | 5765 | .global_cache_directory = comp.global_cache_directory, |
| 5766 | .paths = .{ | 5766 | .paths = .{ |
| 5767 | .root = .{ | 5767 | .root = .{ |
src/glibc.zig+1-1| ... | @@ -1116,7 +1116,7 @@ fn buildSharedLib( | ... | @@ -1116,7 +1116,7 @@ fn buildSharedLib( |
| 1116 | }, | 1116 | }, |
| 1117 | }; | 1117 | }; |
| 1118 | 1118 | ||
| 1119 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 1119 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 1120 | .local_cache_directory = zig_cache_directory, | 1120 | .local_cache_directory = zig_cache_directory, |
| 1121 | .global_cache_directory = comp.global_cache_directory, | 1121 | .global_cache_directory = comp.global_cache_directory, |
| 1122 | .zig_lib_directory = comp.zig_lib_directory, | 1122 | .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 { | ... | @@ -272,7 +272,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 272 | }); | 272 | }); |
| 273 | } | 273 | } |
| 274 | 274 | ||
| 275 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 275 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 276 | .local_cache_directory = comp.global_cache_directory, | 276 | .local_cache_directory = comp.global_cache_directory, |
| 277 | .global_cache_directory = comp.global_cache_directory, | 277 | .global_cache_directory = comp.global_cache_directory, |
| 278 | .zig_lib_directory = comp.zig_lib_directory, | 278 | .zig_lib_directory = comp.zig_lib_directory, |
| ... | @@ -459,7 +459,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { | ... | @@ -459,7 +459,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 459 | }); | 459 | }); |
| 460 | } | 460 | } |
| 461 | 461 | ||
| 462 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 462 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 463 | .local_cache_directory = comp.global_cache_directory, | 463 | .local_cache_directory = comp.global_cache_directory, |
| 464 | .global_cache_directory = comp.global_cache_directory, | 464 | .global_cache_directory = comp.global_cache_directory, |
| 465 | .zig_lib_directory = comp.zig_lib_directory, | 465 | .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 { | ... | @@ -245,7 +245,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 245 | }); | 245 | }); |
| 246 | } | 246 | } |
| 247 | 247 | ||
| 248 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 248 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 249 | .local_cache_directory = comp.global_cache_directory, | 249 | .local_cache_directory = comp.global_cache_directory, |
| 250 | .global_cache_directory = comp.global_cache_directory, | 250 | .global_cache_directory = comp.global_cache_directory, |
| 251 | .zig_lib_directory = comp.zig_lib_directory, | 251 | .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 { | ... | @@ -123,7 +123,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 123 | .owner = root_mod, | 123 | .owner = root_mod, |
| 124 | }; | 124 | }; |
| 125 | } | 125 | } |
| 126 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 126 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 127 | .self_exe_path = comp.self_exe_path, | 127 | .self_exe_path = comp.self_exe_path, |
| 128 | .local_cache_directory = comp.global_cache_directory, | 128 | .local_cache_directory = comp.global_cache_directory, |
| 129 | .global_cache_directory = comp.global_cache_directory, | 129 | .global_cache_directory = comp.global_cache_directory, |
src/main.zig+2-2| ... | @@ -3130,7 +3130,7 @@ fn buildOutputType( | ... | @@ -3130,7 +3130,7 @@ fn buildOutputType( |
| 3130 | 3130 | ||
| 3131 | gimmeMoreOfThoseSweetSweetFileDescriptors(); | 3131 | gimmeMoreOfThoseSweetSweetFileDescriptors(); |
| 3132 | 3132 | ||
| 3133 | const comp = Compilation.create(gpa, .{ | 3133 | const comp = Compilation.create(gpa, arena, .{ |
| 3134 | .zig_lib_directory = zig_lib_directory, | 3134 | .zig_lib_directory = zig_lib_directory, |
| 3135 | .local_cache_directory = local_cache_directory, | 3135 | .local_cache_directory = local_cache_directory, |
| 3136 | .global_cache_directory = global_cache_directory, | 3136 | .global_cache_directory = global_cache_directory, |
| ... | @@ -5508,7 +5508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -5508,7 +5508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 5508 | 5508 | ||
| 5509 | try root_mod.deps.put(arena, "@build", build_mod); | 5509 | try root_mod.deps.put(arena, "@build", build_mod); |
| 5510 | 5510 | ||
| 5511 | const comp = Compilation.create(gpa, .{ | 5511 | const comp = Compilation.create(gpa, arena, .{ |
| 5512 | .zig_lib_directory = zig_lib_directory, | 5512 | .zig_lib_directory = zig_lib_directory, |
| 5513 | .local_cache_directory = local_cache_directory, | 5513 | .local_cache_directory = local_cache_directory, |
| 5514 | .global_cache_directory = global_cache_directory, | 5514 | .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 | ... | @@ -251,7 +251,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr |
| 251 | .builtin_mod = null, | 251 | .builtin_mod = null, |
| 252 | }); | 252 | }); |
| 253 | 253 | ||
| 254 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 254 | const sub_compilation = try Compilation.create(comp.gpa, arena, .{ |
| 255 | .local_cache_directory = comp.global_cache_directory, | 255 | .local_cache_directory = comp.global_cache_directory, |
| 256 | .global_cache_directory = comp.global_cache_directory, | 256 | .global_cache_directory = comp.global_cache_directory, |
| 257 | .zig_lib_directory = comp.zig_lib_directory, | 257 | .zig_lib_directory = comp.zig_lib_directory, |