From 4cf7830f5712ff9b98a0de9fb214f312e03ac829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 16 May 2022 19:23:14 +0300 Subject: [PATCH] scratch: free/alloc instead of realloc that way the existing contents in scratch do not need to be copied over. --- lib/DB.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/DB.zig b/lib/DB.zig index d27c97c..53d3413 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -400,14 +400,20 @@ fn additionalGids( try compress.appendUvarint(&blob, 0); var scratch = try allocator.alloc(u32, 256); - defer allocator.free(scratch); + var scratch_allocated: bool = true; + defer if (scratch_allocated) allocator.free(scratch); for (corpus.user2groups) |usergroups, user_idx| { if (usergroups.len == 0) { idx2offset[user_idx] = 0; continue; } idx2offset[user_idx] = blob.items.len; - scratch = try allocator.realloc(scratch, usergroups.len); + if (scratch.len < usergroups.len) { + allocator.free(scratch); + scratch_allocated = false; + scratch = try allocator.alloc(u32, usergroups.len); + scratch_allocated = true; + } scratch.len = usergroups.len; const corpusGids = corpus.groups.items(.gid); for (usergroups) |group_idx, i|