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 {...@@ -14,6 +14,7 @@ pub const CollectOptions = struct {
14pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOptions) !u.Module {14pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOptions) !u.Module {
15 const m = try u.ModFile.init(gpa, mpath);15 const m = try u.ModFile.init(gpa, mpath);
16 const moduledeps = &std.ArrayList(u.Module).init(gpa);16 const moduledeps = &std.ArrayList(u.Module).init(gpa);
17 defer moduledeps.deinit();
17 try moduledeps.append(try collect_deps(dir, mpath, options));18 try moduledeps.append(try collect_deps(dir, mpath, options));
18 for (m.devdeps) |d| {19 for (m.devdeps) |d| {
19 try get_module_from_dep(moduledeps, d, dir, m.name, options);20 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...@@ -26,7 +27,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt
26 .c_include_dirs = &.{},27 .c_include_dirs = &.{},
27 .c_source_flags = &.{},28 .c_source_flags = &.{},
28 .c_source_files = &.{},29 .c_source_files = &.{},
29 .deps = moduledeps.items,30 .deps = moduledeps.toOwnedSlice(),
30 .clean_path = "",31 .clean_path = "",
31 .only_os = &.{},32 .only_os = &.{},
32 .except_os = &.{},33 .except_os = &.{},
...@@ -37,6 +38,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt...@@ -37,6 +38,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt
37pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions) anyerror!u.Module {38pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions) anyerror!u.Module {
38 const m = try u.ModFile.init(gpa, mpath);39 const m = try u.ModFile.init(gpa, mpath);
39 const moduledeps = &std.ArrayList(u.Module).init(gpa);40 const moduledeps = &std.ArrayList(u.Module).init(gpa);
41 defer moduledeps.deinit();
40 for (m.deps) |d| {42 for (m.deps) |d| {
41 try get_module_from_dep(moduledeps, d, dir, m.name, options);43 try get_module_from_dep(moduledeps, d, dir, m.name, options);
42 }44 }
...@@ -48,7 +50,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions)...@@ -48,7 +50,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions)
48 .c_include_dirs = m.c_include_dirs,50 .c_include_dirs = m.c_include_dirs,
49 .c_source_flags = m.c_source_flags,51 .c_source_flags = m.c_source_flags,
50 .c_source_files = m.c_source_files,52 .c_source_files = m.c_source_files,
51 .deps = moduledeps.items,53 .deps = moduledeps.toOwnedSlice(),
52 .clean_path = "../..",54 .clean_path = "../..",
53 .only_os = &.{},55 .only_os = &.{},
54 .except_os = &.{},56 .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 {...@@ -33,11 +33,12 @@ pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T {
3333
34pub fn split(in: []const u8, delim: []const u8) ![][]const u8 {34pub fn split(in: []const u8, delim: []const u8) ![][]const u8 {
35 const list = &std.ArrayList([]const u8).init(gpa);35 const list = &std.ArrayList([]const u8).init(gpa);
36 defer list.deinit();
36 const iter = &std.mem.split(in, delim);37 const iter = &std.mem.split(in, delim);
37 while (iter.next()) |str| {38 while (iter.next()) |str| {
38 try list.append(str);39 try list.append(str);
39 }40 }
40 return list.items;41 return list.toOwnedSlice();
41}42}
4243
43pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 {44pub 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 {...@@ -172,12 +173,13 @@ pub fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 {
172173
173pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 {174pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 {
174 const list = &std.ArrayList([]const u8).init(gpa);175 const list = &std.ArrayList([]const u8).init(gpa);
176 defer list.deinit();
175 for (input) |item| {177 for (input) |item| {
176 if (!std.mem.eql(u8, item, search)) {178 if (!std.mem.eql(u8, item, search)) {
177 try list.append(item);179 try list.append(item);
178 }180 }
179 }181 }
180 return list.items;182 return list.toOwnedSlice();
181}183}
182184
183pub fn last(in: [][]const u8) ![]const u8 {185pub fn last(in: [][]const u8) ![]const u8 {
src/util/modfile.zig+4-2
...@@ -40,7 +40,9 @@ pub const ModFile = struct {...@@ -40,7 +40,9 @@ pub const ModFile = struct {
40 const main = mapping.get_string("main");40 const main = mapping.get_string("main");
4141
42 const dep_list = try dep_list_by_name(alloc, mapping, "dependencies");42 const dep_list = try dep_list_by_name(alloc, mapping, "dependencies");
43 defer dep_list.deinit();
43 const devdep_list = try dep_list_by_name(alloc, mapping, "dev_dependencies");44 const devdep_list = try dep_list_by_name(alloc, mapping, "dev_dependencies");
45 defer devdep_list.deinit();
4446
45 return Self{47 return Self{
46 .alloc = alloc,48 .alloc = alloc,
...@@ -50,9 +52,9 @@ pub const ModFile = struct {...@@ -50,9 +52,9 @@ pub const ModFile = struct {
50 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),52 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
51 .c_source_flags = try mapping.get_string_array(alloc, "c_source_flags"),53 .c_source_flags = try mapping.get_string_array(alloc, "c_source_flags"),
52 .c_source_files = try mapping.get_string_array(alloc, "c_source_files"),54 .c_source_files = try mapping.get_string_array(alloc, "c_source_files"),
53 .deps = dep_list.items,55 .deps = dep_list.toOwnedSlice(),
54 .yaml = mapping,56 .yaml = mapping,
55 .devdeps = devdep_list.items,57 .devdeps = devdep_list.toOwnedSlice(),
56 };58 };
57 }59 }
5860
src/util/module.zig+2
...@@ -46,9 +46,11 @@ pub const Module = struct {...@@ -46,9 +46,11 @@ pub const Module = struct {
4646
47 pub fn get_hash(self: Module, cdpath: []const u8) ![]const u8 {47 pub fn get_hash(self: Module, cdpath: []const u8) ![]const u8 {
48 const file_list_1 = &std.ArrayList([]const u8).init(gpa);48 const file_list_1 = &std.ArrayList([]const u8).init(gpa);
49 defer file_list_1.deinit();
49 try u.file_list(try u.concat(&.{ cdpath, "/", self.clean_path }), file_list_1);50 try u.file_list(try u.concat(&.{ cdpath, "/", self.clean_path }), file_list_1);
5051
51 const file_list_2 = &std.ArrayList([]const u8).init(gpa);52 const file_list_2 = &std.ArrayList([]const u8).init(gpa);
53 defer file_list_2.deinit();
52 for (file_list_1.items) |item| {54 for (file_list_1.items) |item| {
53 const _a = u.trim_prefix(item, cdpath)[1..];55 const _a = u.trim_prefix(item, cdpath)[1..];
54 const _b = u.trim_prefix(_a, self.clean_path)[1..];56 const _b = u.trim_prefix(_a, self.clean_path)[1..];
src/util/yaml.zig+3-1
...@@ -95,6 +95,7 @@ pub const Mapping = struct {...@@ -95,6 +95,7 @@ pub const Mapping = struct {
9595
96 pub fn get_string_array(self: Mapping, alloc: *std.mem.Allocator, k: []const u8) ![][]const u8 {96 pub fn get_string_array(self: Mapping, alloc: *std.mem.Allocator, k: []const u8) ![][]const u8 {
97 const list = &std.ArrayList([]const u8).init(alloc);97 const list = &std.ArrayList([]const u8).init(alloc);
98 defer list.deinit();
98 if (self.get(k)) |val| {99 if (self.get(k)) |val| {
99 if (val == .sequence) {100 if (val == .sequence) {
100 for (val.sequence) |item, i| {101 for (val.sequence) |item, i| {
...@@ -105,7 +106,7 @@ pub const Mapping = struct {...@@ -105,7 +106,7 @@ pub const Mapping = struct {
105 }106 }
106 }107 }
107 }108 }
108 return list.items;109 return list.toOwnedSlice();
109 }110 }
110111
111 pub fn format(self: Mapping, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {112 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 {...@@ -191,6 +192,7 @@ fn condense_event_list(list: *std.ArrayList(Item), lines: Array) !void {
191 try new_list.append(list.items[i]);192 try new_list.append(list.items[i]);
192 }193 }
193194
195 list.deinit();
194 list.* = new_list;196 list.* = new_list;
195}197}
196198