scratch: free/alloc instead of realloc

that way the existing contents in scratch do not need to be copied over.
This commit is contained in:
Motiejus Jakštys 2022-05-16 19:23:14 +03:00 committed by Motiejus Jakštys
parent 4f2c8707f4
commit 4cf7830f57

View File

@ -400,14 +400,20 @@ fn additionalGids(
try compress.appendUvarint(&blob, 0); try compress.appendUvarint(&blob, 0);
var scratch = try allocator.alloc(u32, 256); 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| { for (corpus.user2groups) |usergroups, user_idx| {
if (usergroups.len == 0) { if (usergroups.len == 0) {
idx2offset[user_idx] = 0; idx2offset[user_idx] = 0;
continue; continue;
} }
idx2offset[user_idx] = blob.items.len; 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; scratch.len = usergroups.len;
const corpusGids = corpus.groups.items(.gid); const corpusGids = corpus.groups.items(.gid);
for (usergroups) |group_idx, i| for (usergroups) |group_idx, i|