authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 17:45:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 19:27:16-07:00
logcb1f3e0ac416ccc3a4cf5fead70807bd7a66d9b8
tree841582a55e4912a8852aada6059b084f6cba831c
parenta7d1edae8f3d673ecbdd88b13044d7edc51b28af

Maker.Step.Compile: leak into the global arena less


4 files changed, 46 insertions(+), 48 deletions(-)

lib/compiler/Maker/Step.zig+5-3
...@@ -360,9 +360,11 @@ pub fn captureChildProcess(s: *Step, maker: *Maker, options: CaptureChildProcess...@@ -360,9 +360,11 @@ pub fn captureChildProcess(s: *Step, maker: *Maker, options: CaptureChildProcess
360 return result;360 return result;
361}361}
362362
363fn clearFailedCommand(s: *Step, gpa: Allocator) void {363pub fn clearFailedCommand(s: *Step, gpa: Allocator) void {
364 if (s.result_failed_command) |cmd| gpa.free(cmd);364 if (s.result_failed_command) |cmd| {
365 s.result_failed_command = null;365 gpa.free(cmd);
366 s.result_failed_command = null;
367 }
366}368}
367369
368pub const FailError = error{ OutOfMemory, MakeFailed };370pub const FailError = error{ OutOfMemory, MakeFailed };
lib/compiler/Maker/Step/Compile.zig+37-37
...@@ -19,8 +19,6 @@ const PkgConfig = @import("../PkgConfig.zig");...@@ -19,8 +19,6 @@ const PkgConfig = @import("../PkgConfig.zig");
19/// Populated when there is compiler process that lives across multiple calls19/// Populated when there is compiler process that lives across multiple calls
20/// to `make`.20/// to `make`.
21zig_process: ?*Step.ZigProcess = null,21zig_process: ?*Step.ZigProcess = null,
22/// Persisted to reuse memory on subsequent calls to `make`.
23zig_args: std.ArrayList([]const u8) = .empty,
24/// Populated by InstallArtifact.22/// Populated by InstallArtifact.
25installed_path: ?Path = null,23installed_path: ?Path = null,
26/// Populated by `make`, used by `Run`.24/// Populated by `make`, used by `Run`.
...@@ -33,25 +31,29 @@ pub fn make(...@@ -33,25 +31,29 @@ pub fn make(
33 progress_node: std.Progress.Node,31 progress_node: std.Progress.Node,
34) Step.ExtendedMakeError!void {32) Step.ExtendedMakeError!void {
35 const graph = maker.graph;33 const graph = maker.graph;
36 const arena = graph.arena; // TODO don't leak into process arena34 const gpa = maker.gpa;
37 const conf = &maker.scanned_config.configuration;35 const conf = &maker.scanned_config.configuration;
38 const conf_step = compile_index.ptr(conf);36 const conf_step = compile_index.ptr(conf);
39 const conf_comp = conf_step.extended.get(conf.extra).compile;37 const conf_comp = conf_step.extended.get(conf.extra).compile;
4038
41 // Reset / repopulate persistent state.39 var arena_allocator: std.heap.ArenaAllocator = .init(gpa);
42 compile.zig_args.clearRetainingCapacity();40 defer arena_allocator.deinit();
41 const arena = arena_allocator.allocator();
42
43 var argv: std.ArrayList([]const u8) = .empty;
44 defer argv.deinit(gpa);
4345
44 try lowerZigArgs(compile, compile_index, maker, progress_node, &compile.zig_args, false);46 try lowerZigArgs(arena, compile, compile_index, maker, progress_node, &argv, false);
4547
46 const maybe_output_dir = Step.evalZigProcess(48 const maybe_output_dir = Step.evalZigProcess(
47 compile_index,49 compile_index,
48 maker,50 maker,
49 compile.zig_args.items,51 argv.items,
50 progress_node,52 progress_node,
51 (graph.incremental == true) and (maker.watch or maker.web_server != null),53 (graph.incremental == true) and (maker.watch or maker.web_server != null),
52 ) catch |err| switch (err) {54 ) catch |err| switch (err) {
53 error.NeedCompileErrorCheck => {55 error.NeedCompileErrorCheck => {
54 try checkCompileErrors(maker, compile_index);56 try checkCompileErrors(arena, maker, compile_index);
55 return;57 return;
56 },58 },
57 else => |e| return e,59 else => |e| return e,
...@@ -63,14 +65,14 @@ pub fn make(...@@ -63,14 +65,14 @@ pub fn make(
63 // Update generated files65 // Update generated files
64 if (maybe_output_dir) |output_dir| {66 if (maybe_output_dir) |output_dir| {
65 if (conf_comp.emit_directory.value) |gf| maker.generatedPath(gf).* = output_dir;67 if (conf_comp.emit_directory.value) |gf| maker.generatedPath(gf).* = output_dir;
66 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_bin.value, .bin);68 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_bin.value, .bin);
67 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_pdb.value, .pdb);69 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_pdb.value, .pdb);
68 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_implib.value, .implib);70 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_implib.value, .implib);
69 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_h.value, .h);71 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_h.value, .h);
70 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_docs.value, .docs);72 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_docs.value, .docs);
71 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_asm.value, .@"asm");73 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_asm.value, .@"asm");
72 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_llvm_ir.value, .llvm_ir);74 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_llvm_ir.value, .llvm_ir);
73 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_llvm_bc.value, .llvm_bc);75 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_llvm_bc.value, .llvm_bc);
74 }76 }
7577
76 if (conf_comp.flags3.kind == .lib and conf_comp.flags2.linkage == .dynamic and78 if (conf_comp.flags3.kind == .lib and conf_comp.flags2.linkage == .dynamic and
...@@ -84,8 +86,9 @@ pub fn make(...@@ -84,8 +86,9 @@ pub fn make(
84}86}
8587
86fn updateGeneratedFile(88fn updateGeneratedFile(
87 conf_comp: *const Configuration.Step.Compile,
88 maker: *Maker,89 maker: *Maker,
90 arena: Allocator,
91 conf_comp: *const Configuration.Step.Compile,
89 out_path: std.Build.Cache.Path,92 out_path: std.Build.Cache.Path,
90 target: *const Configuration.TargetQuery,93 target: *const Configuration.TargetQuery,
91 opt_gf: ?Configuration.GeneratedFileIndex,94 opt_gf: ?Configuration.GeneratedFileIndex,
...@@ -94,7 +97,6 @@ fn updateGeneratedFile(...@@ -94,7 +97,6 @@ fn updateGeneratedFile(
94 const gf = opt_gf orelse return;97 const gf = opt_gf orelse return;
95 const graph = maker.graph;98 const graph = maker.graph;
96 const conf = &maker.scanned_config.configuration;99 const conf = &maker.scanned_config.configuration;
97 const arena = graph.arena; // TODO don't leak into process arena
98 const name = try ea.cacheName(arena, .{100 const name = try ea.cacheName(arena, .{
99 .root_name = conf_comp.root_name.slice(conf),101 .root_name = conf_comp.root_name.slice(conf),
100 .cpu_arch = target.flags.cpu_arch.unwrap().?,102 .cpu_arch = target.flags.cpu_arch.unwrap().?,
...@@ -112,7 +114,7 @@ fn updateGeneratedFile(...@@ -112,7 +114,7 @@ fn updateGeneratedFile(
112 else114 else
113 null,115 null,
114 });116 });
115 maker.generatedPath(gf).* = try out_path.join(arena, name);117 maker.generatedPath(gf).* = try out_path.join(graph.arena, name);
116}118}
117119
118/// List of importable modules in a compilation's module graph, including120/// List of importable modules in a compilation's module graph, including
...@@ -147,6 +149,7 @@ const ModuleListContext = struct {...@@ -147,6 +149,7 @@ const ModuleListContext = struct {
147};149};
148150
149fn lowerZigArgs(151fn lowerZigArgs(
152 arena: Allocator,
150 compile: *Compile,153 compile: *Compile,
151 compile_index: Configuration.Step.Index,154 compile_index: Configuration.Step.Index,
152 maker: *Maker,155 maker: *Maker,
...@@ -156,7 +159,6 @@ fn lowerZigArgs(...@@ -156,7 +159,6 @@ fn lowerZigArgs(
156) Step.ExtendedMakeError!void {159) Step.ExtendedMakeError!void {
157 const step = maker.stepByIndex(compile_index);160 const step = maker.stepByIndex(compile_index);
158 const graph = maker.graph;161 const graph = maker.graph;
159 const arena = graph.arena; // TODO don't leak into the process arena
160 const gpa = maker.gpa;162 const gpa = maker.gpa;
161 const conf = &maker.scanned_config.configuration;163 const conf = &maker.scanned_config.configuration;
162 const conf_step = compile_index.ptr(conf);164 const conf_step = compile_index.ptr(conf);
...@@ -508,7 +510,7 @@ fn lowerZigArgs(...@@ -508,7 +510,7 @@ fn lowerZigArgs(
508 if (cli_named_modules.modules.getIndex(mod_index)) |module_cli_index| {510 if (cli_named_modules.modules.getIndex(mod_index)) |module_cli_index| {
509 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];511 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];
510 const module_index = cli_named_modules.modules.keys()[module_cli_index];512 const module_index = cli_named_modules.modules.keys()[module_cli_index];
511 try appendModuleFlags(module_index, zig_args, compile_index, maker);513 try appendModuleFlags(arena, module_index, zig_args, compile_index, maker);
512514
513 const imports = mod.import_table.get(conf).imports.mal;515 const imports = mod.import_table.get(conf).imports.mal;
514516
...@@ -953,21 +955,23 @@ pub fn rebuildInFuzzMode(...@@ -953,21 +955,23 @@ pub fn rebuildInFuzzMode(
953 const gpa = maker.gpa;955 const gpa = maker.gpa;
954 const step = maker.stepByIndex(compile_index);956 const step = maker.stepByIndex(compile_index);
955957
958 var arena_allocator: std.heap.ArenaAllocator = .init(gpa);
959 defer arena_allocator.deinit();
960 const arena = arena_allocator.allocator();
961
956 step.result_error_msgs.clearRetainingCapacity();962 step.result_error_msgs.clearRetainingCapacity();
957 step.result_stderr = "";963 step.result_stderr = "";
958964
959 step.result_error_bundle.deinit(gpa);965 step.result_error_bundle.deinit(gpa);
960 step.result_error_bundle = std.zig.ErrorBundle.empty;966 step.result_error_bundle = std.zig.ErrorBundle.empty;
961967
962 if (step.result_failed_command) |cmd| {968 step.clearFailedCommand(gpa);
963 gpa.free(cmd);969
964 step.result_failed_command = null;970 var argv: std.ArrayList([]const u8) = .empty;
965 }971 defer argv.deinit(gpa);
966972
967 const zig_args = &compile.zig_args;973 try lowerZigArgs(arena, compile, compile_index, maker, progress_node, &argv, true);
968 zig_args.clearRetainingCapacity();974 const maybe_output_bin_path = try Step.evalZigProcess(compile_index, maker, argv.items, progress_node, false);
969 try lowerZigArgs(compile, compile_index, maker, progress_node, zig_args, true);
970 const maybe_output_bin_path = try Step.evalZigProcess(compile_index, maker, zig_args.items, progress_node, false);
971 return maybe_output_bin_path.?;975 return maybe_output_bin_path.?;
972}976}
973977
...@@ -980,10 +984,8 @@ fn addFlag(gpa: Allocator, args: *std.ArrayList([]const u8), comptime name: []co...@@ -980,10 +984,8 @@ fn addFlag(gpa: Allocator, args: *std.ArrayList([]const u8), comptime name: []co
980 try args.append(gpa, if (cond) "-f" ++ name else "-fno-" ++ name);984 try args.append(gpa, if (cond) "-f" ++ name else "-fno-" ++ name);
981}985}
982986
983fn checkCompileErrors(maker: *Maker, step_index: Configuration.Step.Index) Step.ExtendedMakeError!void {987fn checkCompileErrors(arena: Allocator, maker: *Maker, step_index: Configuration.Step.Index) Step.ExtendedMakeError!void {
984 const step = maker.stepByIndex(step_index);988 const step = maker.stepByIndex(step_index);
985 const graph = maker.graph;
986 const arena = graph.arena; // TODO don't leak into the process arena
987 const conf = &maker.scanned_config.configuration;989 const conf = &maker.scanned_config.configuration;
988 const conf_step = step_index.ptr(conf);990 const conf_step = step_index.ptr(conf);
989 const conf_comp = conf_step.extended.get(conf.extra).compile;991 const conf_comp = conf_step.extended.get(conf.extra).compile;
...@@ -1226,14 +1228,13 @@ fn getModuleList(...@@ -1226,14 +1228,13 @@ fn getModuleList(
1226}1228}
12271229
1228fn appendModuleFlags(1230fn appendModuleFlags(
1231 arena: Allocator,
1229 module_index: Configuration.Module.Index,1232 module_index: Configuration.Module.Index,
1230 zig_args: *std.ArrayList([]const u8),1233 zig_args: *std.ArrayList([]const u8),
1231 asking_step: Configuration.Step.Index,1234 asking_step: Configuration.Step.Index,
1232 maker: *const Maker,1235 maker: *const Maker,
1233) !void {1236) !void {
1234 const gpa = maker.gpa;1237 const gpa = maker.gpa;
1235 const graph = maker.graph;
1236 const arena = graph.arena; // TODO don't leak into the process arena
1237 const conf = &maker.scanned_config.configuration;1238 const conf = &maker.scanned_config.configuration;
1238 const m = module_index.get(conf);1239 const m = module_index.get(conf);
12391240
...@@ -1317,7 +1318,7 @@ fn appendModuleFlags(...@@ -1317,7 +1318,7 @@ fn appendModuleFlags(
13171318
1318 try zig_args.ensureUnusedCapacity(gpa, 2 * m.include_dirs.len);1319 try zig_args.ensureUnusedCapacity(gpa, 2 * m.include_dirs.len);
1319 for (0..m.include_dirs.len) |i|1320 for (0..m.include_dirs.len) |i|
1320 try appendIncludeDirFlags(m.include_dirs.get(conf.extra, i), zig_args, asking_step, maker);1321 try appendIncludeDirFlags(arena, m.include_dirs.get(conf.extra, i), zig_args, asking_step, maker);
13211322
1322 try zig_args.ensureUnusedCapacity(gpa, m.c_macros.slice.len);1323 try zig_args.ensureUnusedCapacity(gpa, m.c_macros.slice.len);
1323 for (m.c_macros.slice) |c_macro|1324 for (m.c_macros.slice) |c_macro|
...@@ -1344,13 +1345,12 @@ fn appendModuleFlags(...@@ -1344,13 +1345,12 @@ fn appendModuleFlags(
13441345
1345/// Assumes unused capacity for at least 2 items.1346/// Assumes unused capacity for at least 2 items.
1346pub fn appendIncludeDirFlags(1347pub fn appendIncludeDirFlags(
1348 arena: Allocator,
1347 include_dir: Configuration.Module.IncludeDir,1349 include_dir: Configuration.Module.IncludeDir,
1348 zig_args: *std.ArrayList([]const u8),1350 zig_args: *std.ArrayList([]const u8),
1349 asking_step: Configuration.Step.Index,1351 asking_step: Configuration.Step.Index,
1350 maker: *const Maker,1352 maker: *const Maker,
1351) !void {1353) !void {
1352 const graph = maker.graph;
1353 const arena = graph.arena; // TODO don't leak into the process arena
1354 const conf = &maker.scanned_config.configuration;1354 const conf = &maker.scanned_config.configuration;
13551355
1356 switch (include_dir) {1356 switch (include_dir) {
lib/compiler/Maker/Step/Run.zig+3-7
...@@ -1605,10 +1605,7 @@ pub fn rerunInFuzzMode(...@@ -1605,10 +1605,7 @@ pub fn rerunInFuzzMode(
1605 argv_list.appendAssumeCapacity("--listen=-");1605 argv_list.appendAssumeCapacity("--listen=-");
1606 }1606 }
16071607
1608 if (step.result_failed_command) |cmd| {1608 step.clearFailedCommand(gpa);
1609 gpa.free(cmd);
1610 step.result_failed_command = null;
1611 }
16121609
1613 const has_side_effects = false;1610 const has_side_effects = false;
1614 var rand_int: u64 = undefined;1611 var rand_int: u64 = undefined;
...@@ -1930,8 +1927,7 @@ fn runCommand(...@@ -1930,8 +1927,7 @@ fn runCommand(
1930 },1927 },
1931 }1928 }
19321929
1933 gpa.free(step.result_failed_command.?);1930 step.clearFailedCommand(gpa);
1934 step.result_failed_command = null;
1935 try graph.handleVerbose(cwd_string, &environ_map, interp_argv.items);1931 try graph.handleVerbose(cwd_string, &environ_map, interp_argv.items);
19361932
1937 break :term spawnChildAndCollect(1933 break :term spawnChildAndCollect(
...@@ -2138,7 +2134,7 @@ fn spawnChildAndCollect(...@@ -2138,7 +2134,7 @@ fn spawnChildAndCollect(
2138 .inherit;2134 .inherit;
21392135
2140 // If an error occurs, it's caused by this command:2136 // If an error occurs, it's caused by this command:
2141 assert(step.result_failed_command == null);2137 step.clearFailedCommand(gpa);
2142 step.result_failed_command = try std.zig.allocPrintCmd(gpa, argv, .{2138 step.result_failed_command = try std.zig.allocPrintCmd(gpa, argv, .{
2143 .cwd = switch (child_cwd) {2139 .cwd = switch (child_cwd) {
2144 .path => |p| p,2140 .path => |p| p,
lib/compiler/Maker/Step/TranslateC.zig+1-1
...@@ -53,7 +53,7 @@ pub fn make(...@@ -53,7 +53,7 @@ pub fn make(
5353
54 try argv.ensureUnusedCapacity(arena, conf_tc.include_dirs.len * 2);54 try argv.ensureUnusedCapacity(arena, conf_tc.include_dirs.len * 2);
55 for (0..conf_tc.include_dirs.len) |i|55 for (0..conf_tc.include_dirs.len) |i|
56 try Step.Compile.appendIncludeDirFlags(conf_tc.include_dirs.get(conf.extra, i), &argv, step_index, maker);56 try Step.Compile.appendIncludeDirFlags(arena, conf_tc.include_dirs.get(conf.extra, i), &argv, step_index, maker);
5757
58 for (conf_tc.c_macros.slice) |c_macro| {58 for (conf_tc.c_macros.slice) |c_macro| {
59 (try argv.addManyAsArray(arena, 2)).* = .{ "-D", c_macro.slice(conf) };59 (try argv.addManyAsArray(arena, 2)).* = .{ "-D", c_macro.slice(conf) };