Skip to content

Commit ed5ada4

Browse files
committed
Remove unecessary allocations in builder (#120)
1 parent 10195e6 commit ed5ada4

File tree

1 file changed

+79
-60
lines changed

1 file changed

+79
-60
lines changed

build.zig

Lines changed: 79 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ const StepBuilder = struct {
9494
comptime source: BuildRuleSource,
9595
config: anytype,
9696
) void {
97+
const arena = self.b.allocator;
98+
9799
self.rules.append(
98-
self.b.allocator,
100+
arena,
99101
buildRule(
100102
self.b,
101103
source,
@@ -120,40 +122,45 @@ const StepBuilder = struct {
120122
exclude: ?[]const std.Build.LazyPath = null,
121123
},
122124
) void {
125+
const arena = self.b.allocator;
126+
123127
if (paths.include) |includes|
124-
for (includes) |path| self.include_paths.append(self.b.allocator, path) catch @panic("OOM");
128+
for (includes) |path| self.include_paths.append(arena, path) catch @panic("OOM");
125129
if (paths.exclude) |excludes|
126-
for (excludes) |path| self.exclude_paths.append(self.b.allocator, path) catch @panic("OOM");
130+
for (excludes) |path| self.exclude_paths.append(arena, path) catch @panic("OOM");
127131
}
128132

129-
/// Returns a build step and cleans itself up.
130133
pub fn build(self: *StepBuilder) *std.Build.Step {
131-
defer self.deinit();
134+
const b = self.b;
135+
const arena = b.allocator;
136+
137+
const include_paths = include_paths: {
138+
if (self.include_paths.items.len > 0) {
139+
break :include_paths self.include_paths.items;
140+
} else {
141+
var list = arena.alloc(std.Build.LazyPath, 1) catch @panic("OOM");
142+
list[0] = b.path("./");
143+
break :include_paths list;
144+
}
145+
};
132146

133147
return buildStep(
134-
self.b,
148+
b,
135149
self.rules.items,
136150
.{
137151
.target = self.target,
138152
.optimize = self.optimize,
139153
.zlinter = .{
140-
.dependency = self.b.dependencyFromBuildZig(
154+
.dependency = b.dependencyFromBuildZig(
141155
@"build.zig",
142156
.{},
143157
),
144158
},
145-
.include_paths = self.include_paths,
146-
.exclude_paths = self.exclude_paths,
159+
.include_paths = include_paths,
160+
.exclude_paths = self.exclude_paths.items,
147161
},
148162
);
149163
}
150-
151-
fn deinit(self: *StepBuilder) void {
152-
self.include_paths.deinit(self.b.allocator);
153-
self.exclude_paths.deinit(self.b.allocator);
154-
for (self.rules.items) |*r| r.deinit(self.b.allocator);
155-
self.* = undefined;
156-
}
157164
};
158165

159166
/// zlinters own build file for running its tests and itself on itself
@@ -332,8 +339,8 @@ pub fn build(b: *std.Build) void {
332339

333340
const lint_cmd = b.step("lint", "Lint the linters own source code.");
334341
lint_cmd.dependOn(step: {
342+
const include_paths = shims.ArrayList(std.Build.LazyPath).empty;
335343
var exclude_paths = shims.ArrayList(std.Build.LazyPath).empty;
336-
defer exclude_paths.deinit(b.allocator);
337344

338345
exclude_paths.append(b.allocator, b.path("integration_tests/test_cases")) catch @panic("OOM");
339346
exclude_paths.append(b.allocator, b.path("integration_tests/src/test_case_references.zig")) catch @panic("OOM");
@@ -393,7 +400,8 @@ pub fn build(b: *std.Build) void {
393400
.{
394401
.target = target,
395402
.optimize = optimize,
396-
.exclude_paths = exclude_paths,
403+
.include_paths = include_paths.items,
404+
.exclude_paths = exclude_paths.items,
397405
.zlinter = .{ .module = zlinter_lib_module },
398406
},
399407
);
@@ -459,8 +467,8 @@ fn buildStep(
459467
dependency: *std.Build.Dependency,
460468
module: *std.Build.Module,
461469
},
462-
include_paths: ?shims.ArrayList(std.Build.LazyPath) = null,
463-
exclude_paths: ?shims.ArrayList(std.Build.LazyPath) = null,
470+
include_paths: []const std.Build.LazyPath,
471+
exclude_paths: []const std.Build.LazyPath,
464472
},
465473
) *std.Build.Step {
466474
const zlinter_lib_module: *std.Build.Module, const exe_file: std.Build.LazyPath, const build_rules_exe_file: std.Build.LazyPath = switch (options.zlinter) {
@@ -508,32 +516,12 @@ fn buildStep(
508516
.use_llvm = true,
509517
});
510518

511-
const zlinter_run = ZlinterRun.create(b, zlinter_exe);
512-
513-
if (b.args) |args| zlinter_run.addArgs(args);
514-
if (b.verbose) zlinter_run.addArgs(&.{"--verbose"});
515-
516-
var include_path_added = false;
517-
if (options.include_paths) |include_paths| {
518-
include_path_added = include_paths.items.len > 0;
519-
for (include_paths.items) |path| {
520-
zlinter_run.addIncludePath(b, path);
521-
}
522-
}
523-
if (!include_path_added)
524-
zlinter_run.addIncludePath(b, b.path("./"));
525-
526-
if (options.exclude_paths) |exclude_paths| {
527-
for (exclude_paths.items) |path|
528-
zlinter_run.addExcludePath(path.getPath3(b, &zlinter_run.step).subPathOrDot());
529-
}
530-
531-
zlinter_run.addArgs(&.{ "--zig_exe", b.graph.zig_exe });
532-
if (b.graph.global_cache_root.path) |p|
533-
zlinter_run.addArgs(&.{ "--global_cache_root", p });
534-
535-
if (b.graph.zig_lib_directory.path) |p|
536-
zlinter_run.addArgs(&.{ "--zig_lib_directory", p });
519+
const zlinter_run = ZlinterRun.create(
520+
b,
521+
zlinter_exe,
522+
options.include_paths,
523+
options.exclude_paths,
524+
);
537525

538526
return &zlinter_run.step;
539527
}
@@ -734,17 +722,22 @@ const ZlinterRun = struct {
734722
argv: shims.ArrayList(Arg),
735723

736724
/// Include paths configured within the build file.
737-
include_paths: shims.ArrayList([]const u8),
725+
include_paths: []const std.Build.LazyPath,
738726

739727
/// Exclude paths confiured within the build file.
740-
exclude_paths: shims.ArrayList([]const u8),
728+
exclude_paths: []const std.Build.LazyPath,
741729

742730
const Arg = union(enum) {
743731
artifact: *std.Build.Step.Compile,
744732
bytes: []const u8,
745733
};
746734

747-
pub fn create(owner: *std.Build, exe: *std.Build.Step.Compile) *ZlinterRun {
735+
pub fn create(
736+
owner: *std.Build,
737+
exe: *std.Build.Step.Compile,
738+
include_paths: []const std.Build.LazyPath,
739+
exclude_paths: []const std.Build.LazyPath,
740+
) *ZlinterRun {
748741
const arena = owner.allocator;
749742

750743
const self = arena.create(ZlinterRun) catch @panic("OOM");
@@ -756,31 +749,57 @@ const ZlinterRun = struct {
756749
.makeFn = make,
757750
}),
758751
.argv = .empty,
759-
.exclude_paths = .empty,
760-
.include_paths = .empty,
752+
.exclude_paths = exclude_paths,
753+
.include_paths = include_paths,
761754
};
762755

756+
for (include_paths) |path| {
757+
addWatchInput(owner, &self.step, path, .lintable_file) catch @panic("OOM");
758+
}
759+
763760
self.argv.append(arena, .{ .artifact = exe }) catch @panic("OOM");
764761

762+
if (owner.args) |args| self.addArgs(args);
763+
if (owner.verbose) self.addArgs(&.{"--verbose"});
764+
765+
self.addArgs(&.{ "--zig_exe", owner.graph.zig_exe });
766+
if (owner.graph.global_cache_root.path) |p|
767+
self.addArgs(&.{ "--global_cache_root", p });
768+
769+
if (owner.graph.zig_lib_directory.path) |p|
770+
self.addArgs(&.{ "--zig_lib_directory", p });
771+
765772
const bin_file = exe.getEmittedBin();
766773
bin_file.addStepDependencies(&self.step);
767774

768775
return self;
769776
}
770777

771-
pub fn addArgs(run: *ZlinterRun, args: []const []const u8) void {
778+
fn addArgs(run: *ZlinterRun, args: []const []const u8) void {
772779
const b = run.step.owner;
773780
for (args) |arg|
774781
run.argv.append(b.allocator, .{ .bytes = b.dupe(arg) }) catch @panic("OOM");
775782
}
776783

777-
pub fn addIncludePath(run: *ZlinterRun, owner: *std.Build, path: std.Build.LazyPath) void {
778-
addWatchInput(owner, &run.step, path, .lintable_file) catch @panic("OOM");
779-
run.include_paths.append(run.step.owner.allocator, run.step.owner.dupe(path.getPath3(owner, &run.step).subPathOrDot())) catch @panic("OOM");
780-
}
784+
fn subPaths(
785+
step: *std.Build.Step,
786+
paths: []const std.Build.LazyPath,
787+
) error{OutOfMemory}![]const []const u8 {
788+
const b = step.owner;
789+
790+
var list: shims.ArrayList([]const u8) = try .initCapacity(
791+
b.allocator,
792+
paths.len,
793+
);
794+
errdefer list.deinit(b.allocator);
795+
796+
for (paths) |path| {
797+
list.appendAssumeCapacity(
798+
path.getPath3(b, step).subPathOrDot(),
799+
);
800+
}
781801

782-
pub fn addExcludePath(run: *ZlinterRun, exclude_path: []const u8) void {
783-
run.exclude_paths.append(run.step.owner.allocator, run.step.owner.dupe(exclude_path)) catch @panic("OOM");
802+
return try list.toOwnedSlice(b.allocator);
784803
}
785804

786805
fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) !void {
@@ -789,8 +808,8 @@ const ZlinterRun = struct {
789808
const arena = b.allocator;
790809

791810
const build_info_zon_bytes: []const u8 = toZonString(BuildInfo{
792-
.include_paths = if (run.include_paths.items.len > 0) run.include_paths.items else null,
793-
.exclude_paths = if (run.exclude_paths.items.len > 0) run.exclude_paths.items else null,
811+
.include_paths = if (run.include_paths.len > 0) try subPaths(&run.step, run.include_paths) else null,
812+
.exclude_paths = if (run.exclude_paths.len > 0) try subPaths(&run.step, run.exclude_paths) else null,
794813
}, b.allocator);
795814

796815
const env_map = arena.create(std.process.EnvMap) catch @panic("OOM");

0 commit comments

Comments
 (0)