From d6e709509774d1a208b3564f0950fe15c4d8a2fb Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 2 Jul 2022 17:34:01 +0200 Subject: [PATCH] chore: optimize build script --- build.zig | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index a4c3d80..956fc1e 100755 --- a/build.zig +++ b/build.zig @@ -220,19 +220,25 @@ fn installMmcPackJson(archive: *c.archive, entry: *c.archive_entry) !void { .formatVersion = 1, }; - const json = try std.json.stringifyAlloc( - std.heap.c_allocator, + // We run the serializer twice, because we need to know the size ahead of time for zip. + // This is faster than allocating the json on the heap. + var counter = std.io.countingWriter(std.io.null_writer); + try std.json.stringify( data, .{ .emit_null_optional_fields = false }, + counter.writer(), ); - defer std.heap.c_allocator.free(json); entrySetFile(entry); - c.archive_entry_set_size(entry, @intCast(i64, json.len)); + c.archive_entry_set_size(entry, @intCast(i64, counter.bytes_written)); c.archive_entry_set_pathname(entry, "mmc-pack.json"); - try handleArchiveErr(c.archive_write_header(archive, entry), archive); - try (ArchiveWriter{ .context = archive }).writeAll(json); + + try std.json.stringify( + data, + .{ .emit_null_optional_fields = false }, + ArchiveWriter{ .context = archive }, + ); } fn readMods(list: *std.ArrayList([]u8), alloc: std.mem.Allocator) !void {