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
360360 return result;
361361}
362362
363fn clearFailedCommand(s: *Step, gpa: Allocator) void {
364 if (s.result_failed_command) |cmd| gpa.free(cmd);
365 s.result_failed_command = null;
363pub fn clearFailedCommand(s: *Step, gpa: Allocator) void {
364 if (s.result_failed_command) |cmd| {
365 gpa.free(cmd);
366 s.result_failed_command = null;
367 }
366368}
367369
368370pub const FailError = error{ OutOfMemory, MakeFailed };
lib/compiler/Maker/Step/Compile.zig+37-37
......@@ -19,8 +19,6 @@ const PkgConfig = @import("../PkgConfig.zig");
1919/// Populated when there is compiler process that lives across multiple calls
2020/// to `make`.
2121zig_process: ?*Step.ZigProcess = null,
22/// Persisted to reuse memory on subsequent calls to `make`.
23zig_args: std.ArrayList([]const u8) = .empty,
2422/// Populated by InstallArtifact.
2523installed_path: ?Path = null,
2624/// Populated by `make`, used by `Run`.
......@@ -33,25 +31,29 @@ pub fn make(
3331 progress_node: std.Progress.Node,
3432) Step.ExtendedMakeError!void {
3533 const graph = maker.graph;
36 const arena = graph.arena; // TODO don't leak into process arena
34 const gpa = maker.gpa;
3735 const conf = &maker.scanned_config.configuration;
3836 const conf_step = compile_index.ptr(conf);
3937 const conf_comp = conf_step.extended.get(conf.extra).compile;
4038
41 // Reset / repopulate persistent state.
42 compile.zig_args.clearRetainingCapacity();
39 var arena_allocator: std.heap.ArenaAllocator = .init(gpa);
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
4648 const maybe_output_dir = Step.evalZigProcess(
4749 compile_index,
4850 maker,
49 compile.zig_args.items,
51 argv.items,
5052 progress_node,
5153 (graph.incremental == true) and (maker.watch or maker.web_server != null),
5254 ) catch |err| switch (err) {
5355 error.NeedCompileErrorCheck => {
54 try checkCompileErrors(maker, compile_index);
56 try checkCompileErrors(arena, maker, compile_index);
5557 return;
5658 },
5759 else => |e| return e,
......@@ -63,14 +65,14 @@ pub fn make(
6365 // Update generated files
6466 if (maybe_output_dir) |output_dir| {
6567 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);
67 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_pdb.value, .pdb);
68 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_implib.value, .implib);
69 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_h.value, .h);
70 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_docs.value, .docs);
71 try updateGeneratedFile(&conf_comp, maker, 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);
73 try updateGeneratedFile(&conf_comp, maker, output_dir, &target, conf_comp.generated_llvm_bc.value, .llvm_bc);
68 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_bin.value, .bin);
69 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_pdb.value, .pdb);
70 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_implib.value, .implib);
71 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_h.value, .h);
72 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_docs.value, .docs);
73 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_asm.value, .@"asm");
74 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_llvm_ir.value, .llvm_ir);
75 try updateGeneratedFile(maker, arena, &conf_comp, output_dir, &target, conf_comp.generated_llvm_bc.value, .llvm_bc);
7476 }
7577
7678 if (conf_comp.flags3.kind == .lib and conf_comp.flags2.linkage == .dynamic and
......@@ -84,8 +86,9 @@ pub fn make(
8486}
8587
8688fn updateGeneratedFile(
87 conf_comp: *const Configuration.Step.Compile,
8889 maker: *Maker,
90 arena: Allocator,
91 conf_comp: *const Configuration.Step.Compile,
8992 out_path: std.Build.Cache.Path,
9093 target: *const Configuration.TargetQuery,
9194 opt_gf: ?Configuration.GeneratedFileIndex,
......@@ -94,7 +97,6 @@ fn updateGeneratedFile(
9497 const gf = opt_gf orelse return;
9598 const graph = maker.graph;
9699 const conf = &maker.scanned_config.configuration;
97 const arena = graph.arena; // TODO don't leak into process arena
98100 const name = try ea.cacheName(arena, .{
99101 .root_name = conf_comp.root_name.slice(conf),
100102 .cpu_arch = target.flags.cpu_arch.unwrap().?,
......@@ -112,7 +114,7 @@ fn updateGeneratedFile(
112114 else
113115 null,
114116 });
115 maker.generatedPath(gf).* = try out_path.join(arena, name);
117 maker.generatedPath(gf).* = try out_path.join(graph.arena, name);
116118}
117119
118120/// List of importable modules in a compilation's module graph, including
......@@ -147,6 +149,7 @@ const ModuleListContext = struct {
147149};
148150
149151fn lowerZigArgs(
152 arena: Allocator,
150153 compile: *Compile,
151154 compile_index: Configuration.Step.Index,
152155 maker: *Maker,
......@@ -156,7 +159,6 @@ fn lowerZigArgs(
156159) Step.ExtendedMakeError!void {
157160 const step = maker.stepByIndex(compile_index);
158161 const graph = maker.graph;
159 const arena = graph.arena; // TODO don't leak into the process arena
160162 const gpa = maker.gpa;
161163 const conf = &maker.scanned_config.configuration;
162164 const conf_step = compile_index.ptr(conf);
......@@ -508,7 +510,7 @@ fn lowerZigArgs(
508510 if (cli_named_modules.modules.getIndex(mod_index)) |module_cli_index| {
509511 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];
510512 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
513515 const imports = mod.import_table.get(conf).imports.mal;
514516
......@@ -953,21 +955,23 @@ pub fn rebuildInFuzzMode(
953955 const gpa = maker.gpa;
954956 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
956962 step.result_error_msgs.clearRetainingCapacity();
957963 step.result_stderr = "";
958964
959965 step.result_error_bundle.deinit(gpa);
960966 step.result_error_bundle = std.zig.ErrorBundle.empty;
961967
962 if (step.result_failed_command) |cmd| {
963 gpa.free(cmd);
964 step.result_failed_command = null;
965 }
968 step.clearFailedCommand(gpa);
969
970 var argv: std.ArrayList([]const u8) = .empty;
971 defer argv.deinit(gpa);
966972
967 const zig_args = &compile.zig_args;
968 zig_args.clearRetainingCapacity();
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);
973 try lowerZigArgs(arena, compile, compile_index, maker, progress_node, &argv, true);
974 const maybe_output_bin_path = try Step.evalZigProcess(compile_index, maker, argv.items, progress_node, false);
971975 return maybe_output_bin_path.?;
972976}
973977
......@@ -980,10 +984,8 @@ fn addFlag(gpa: Allocator, args: *std.ArrayList([]const u8), comptime name: []co
980984 try args.append(gpa, if (cond) "-f" ++ name else "-fno-" ++ name);
981985}
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 {
984988 const step = maker.stepByIndex(step_index);
985 const graph = maker.graph;
986 const arena = graph.arena; // TODO don't leak into the process arena
987989 const conf = &maker.scanned_config.configuration;
988990 const conf_step = step_index.ptr(conf);
989991 const conf_comp = conf_step.extended.get(conf.extra).compile;
......@@ -1226,14 +1228,13 @@ fn getModuleList(
12261228}
12271229
12281230fn appendModuleFlags(
1231 arena: Allocator,
12291232 module_index: Configuration.Module.Index,
12301233 zig_args: *std.ArrayList([]const u8),
12311234 asking_step: Configuration.Step.Index,
12321235 maker: *const Maker,
12331236) !void {
12341237 const gpa = maker.gpa;
1235 const graph = maker.graph;
1236 const arena = graph.arena; // TODO don't leak into the process arena
12371238 const conf = &maker.scanned_config.configuration;
12381239 const m = module_index.get(conf);
12391240
......@@ -1317,7 +1318,7 @@ fn appendModuleFlags(
13171318
13181319 try zig_args.ensureUnusedCapacity(gpa, 2 * m.include_dirs.len);
13191320 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
13221323 try zig_args.ensureUnusedCapacity(gpa, m.c_macros.slice.len);
13231324 for (m.c_macros.slice) |c_macro|
......@@ -1344,13 +1345,12 @@ fn appendModuleFlags(
13441345
13451346/// Assumes unused capacity for at least 2 items.
13461347pub fn appendIncludeDirFlags(
1348 arena: Allocator,
13471349 include_dir: Configuration.Module.IncludeDir,
13481350 zig_args: *std.ArrayList([]const u8),
13491351 asking_step: Configuration.Step.Index,
13501352 maker: *const Maker,
13511353) !void {
1352 const graph = maker.graph;
1353 const arena = graph.arena; // TODO don't leak into the process arena
13541354 const conf = &maker.scanned_config.configuration;
13551355
13561356 switch (include_dir) {
lib/compiler/Maker/Step/Run.zig+3-7
......@@ -1605,10 +1605,7 @@ pub fn rerunInFuzzMode(
16051605 argv_list.appendAssumeCapacity("--listen=-");
16061606 }
16071607
1608 if (step.result_failed_command) |cmd| {
1609 gpa.free(cmd);
1610 step.result_failed_command = null;
1611 }
1608 step.clearFailedCommand(gpa);
16121609
16131610 const has_side_effects = false;
16141611 var rand_int: u64 = undefined;
......@@ -1930,8 +1927,7 @@ fn runCommand(
19301927 },
19311928 }
19321929
1933 gpa.free(step.result_failed_command.?);
1934 step.result_failed_command = null;
1930 step.clearFailedCommand(gpa);
19351931 try graph.handleVerbose(cwd_string, &environ_map, interp_argv.items);
19361932
19371933 break :term spawnChildAndCollect(
......@@ -2138,7 +2134,7 @@ fn spawnChildAndCollect(
21382134 .inherit;
21392135
21402136 // If an error occurs, it's caused by this command:
2141 assert(step.result_failed_command == null);
2137 step.clearFailedCommand(gpa);
21422138 step.result_failed_command = try std.zig.allocPrintCmd(gpa, argv, .{
21432139 .cwd = switch (child_cwd) {
21442140 .path => |p| p,
lib/compiler/Maker/Step/TranslateC.zig+1-1
......@@ -53,7 +53,7 @@ pub fn make(
5353
5454 try argv.ensureUnusedCapacity(arena, conf_tc.include_dirs.len * 2);
5555 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
5858 for (conf_tc.c_macros.slice) |c_macro| {
5959 (try argv.addManyAsArray(arena, 2)).* = .{ "-D", c_macro.slice(conf) };