authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-04 00:53:18 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-04 00:53:18 -07:00
logb374ff80002f97a0a04708bfbd7932f6d5e10aca
treeb4e51bfa9293d4e5da939849ad9e415520dda980
parentfa0acac8479c573059713ebb1981ac5a8ed42086

mild memory cleanup of ArrayList usage


5 files changed, 17 insertions(+), 7 deletions(-)

src/common.zig+4-2
......@@ -14,6 +14,7 @@ pub const CollectOptions = struct {
1414pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOptions) !u.Module {
1515 const m = try u.ModFile.init(gpa, mpath);
1616 const moduledeps = &std.ArrayList(u.Module).init(gpa);
17 defer moduledeps.deinit();
1718 try moduledeps.append(try collect_deps(dir, mpath, options));
1819 for (m.devdeps) |d| {
1920 try get_module_from_dep(moduledeps, d, dir, m.name, options);
......@@ -26,7 +27,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt
2627 .c_include_dirs = &.{},
2728 .c_source_flags = &.{},
2829 .c_source_files = &.{},
29 .deps = moduledeps.items,
30 .deps = moduledeps.toOwnedSlice(),
3031 .clean_path = "",
3132 .only_os = &.{},
3233 .except_os = &.{},
......@@ -37,6 +38,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt
3738pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions) anyerror!u.Module {
3839 const m = try u.ModFile.init(gpa, mpath);
3940 const moduledeps = &std.ArrayList(u.Module).init(gpa);
41 defer moduledeps.deinit();
4042 for (m.deps) |d| {
4143 try get_module_from_dep(moduledeps, d, dir, m.name, options);
4244 }
......@@ -48,7 +50,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions)
4850 .c_include_dirs = m.c_include_dirs,
4951 .c_source_flags = m.c_source_flags,
5052 .c_source_files = m.c_source_files,
51 .deps = moduledeps.items,
53 .deps = moduledeps.toOwnedSlice(),
5254 .clean_path = "../..",
5355 .only_os = &.{},
5456 .except_os = &.{},
src/util/funcs.zig+4-2
......@@ -33,11 +33,12 @@ pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T {
3333
3434pub fn split(in: []const u8, delim: []const u8) ![][]const u8 {
3535 const list = &std.ArrayList([]const u8).init(gpa);
36 defer list.deinit();
3637 const iter = &std.mem.split(in, delim);
3738 while (iter.next()) |str| {
3839 try list.append(str);
3940 }
40 return list.items;
41 return list.toOwnedSlice();
4142}
4243
4344pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 {
......@@ -172,12 +173,13 @@ pub fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 {
172173
173174pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 {
174175 const list = &std.ArrayList([]const u8).init(gpa);
176 defer list.deinit();
175177 for (input) |item| {
176178 if (!std.mem.eql(u8, item, search)) {
177179 try list.append(item);
178180 }
179181 }
180 return list.items;
182 return list.toOwnedSlice();
181183}
182184
183185pub fn last(in: [][]const u8) ![]const u8 {
src/util/modfile.zig+4-2
......@@ -40,7 +40,9 @@ pub const ModFile = struct {
4040 const main = mapping.get_string("main");
4141
4242 const dep_list = try dep_list_by_name(alloc, mapping, "dependencies");
43 defer dep_list.deinit();
4344 const devdep_list = try dep_list_by_name(alloc, mapping, "dev_dependencies");
45 defer devdep_list.deinit();
4446
4547 return Self{
4648 .alloc = alloc,
......@@ -50,9 +52,9 @@ pub const ModFile = struct {
5052 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
5153 .c_source_flags = try mapping.get_string_array(alloc, "c_source_flags"),
5254 .c_source_files = try mapping.get_string_array(alloc, "c_source_files"),
53 .deps = dep_list.items,
55 .deps = dep_list.toOwnedSlice(),
5456 .yaml = mapping,
55 .devdeps = devdep_list.items,
57 .devdeps = devdep_list.toOwnedSlice(),
5658 };
5759 }
5860
src/util/module.zig+2
......@@ -46,9 +46,11 @@ pub const Module = struct {
4646
4747 pub fn get_hash(self: Module, cdpath: []const u8) ![]const u8 {
4848 const file_list_1 = &std.ArrayList([]const u8).init(gpa);
49 defer file_list_1.deinit();
4950 try u.file_list(try u.concat(&.{ cdpath, "/", self.clean_path }), file_list_1);
5051
5152 const file_list_2 = &std.ArrayList([]const u8).init(gpa);
53 defer file_list_2.deinit();
5254 for (file_list_1.items) |item| {
5355 const _a = u.trim_prefix(item, cdpath)[1..];
5456 const _b = u.trim_prefix(_a, self.clean_path)[1..];
src/util/yaml.zig+3-1
......@@ -95,6 +95,7 @@ pub const Mapping = struct {
9595
9696 pub fn get_string_array(self: Mapping, alloc: *std.mem.Allocator, k: []const u8) ![][]const u8 {
9797 const list = &std.ArrayList([]const u8).init(alloc);
98 defer list.deinit();
9899 if (self.get(k)) |val| {
99100 if (val == .sequence) {
100101 for (val.sequence) |item, i| {
......@@ -105,7 +106,7 @@ pub const Mapping = struct {
105106 }
106107 }
107108 }
108 return list.items;
109 return list.toOwnedSlice();
109110 }
110111
111112 pub fn format(self: Mapping, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
......@@ -191,6 +192,7 @@ fn condense_event_list(list: *std.ArrayList(Item), lines: Array) !void {
191192 try new_list.append(list.items[i]);
192193 }
193194
195 list.deinit();
194196 list.* = new_list;
195197}
196198