less memory thrashing

This commit is contained in:
Motiejus Jakštys 2022-03-05 05:33:31 +02:00 committed by Motiejus Jakštys
parent 87c424aab9
commit 869f3f3a65
3 changed files with 15 additions and 13 deletions

View File

@ -28,6 +28,7 @@ pub fn pack(allocator: Allocator, input: [][*:0]const u8) Error![]const u8 {
const size = c.cmph_packed_size(hash); const size = c.cmph_packed_size(hash);
var buf = try allocator.alloc(u8, size); var buf = try allocator.alloc(u8, size);
errdefer allocator.free(buf);
c.cmph_pack(hash, &buf[0]); c.cmph_pack(hash, &buf[0]);
c.cmph_destroy(hash); c.cmph_destroy(hash);
return buf[4..]; return buf[4..];

View File

@ -139,6 +139,7 @@ pub fn someMembers(
members: []const []const u8, members: []const []const u8,
) Allocator.Error!BufSet { ) Allocator.Error!BufSet {
var bufset = BufSet.init(allocator); var bufset = BufSet.init(allocator);
errdefer bufset.deinit();
for (members) |member| { for (members) |member| {
try bufset.insert(member); try bufset.insert(member);
} }

View File

@ -53,6 +53,7 @@ const Corpus = struct {
) initErr!Corpus { ) initErr!Corpus {
var arena = std.heap.ArenaAllocator.init(baseAllocator); var arena = std.heap.ArenaAllocator.init(baseAllocator);
var allocator = arena.allocator(); var allocator = arena.allocator();
errdefer arena.deinit();
var users = try allocator.alloc(User, usersConst.len); var users = try allocator.alloc(User, usersConst.len);
var groups = try allocator.alloc(Group, groupsConst.len); var groups = try allocator.alloc(Group, groupsConst.len);
@ -108,6 +109,7 @@ const Corpus = struct {
var username2groups = StringHashMap( var username2groups = StringHashMap(
ArrayListUnmanaged(*const Group), ArrayListUnmanaged(*const Group),
).init(baseAllocator); ).init(baseAllocator);
defer username2groups.deinit();
for (groups) |*group| { for (groups) |*group| {
var members = try allocator.alloc(*const User, group.members.count()); var members = try allocator.alloc(*const User, group.members.count());
@ -150,7 +152,6 @@ const Corpus = struct {
const usergroups = elem.value_ptr.*.toOwnedSlice(allocator); const usergroups = elem.value_ptr.*.toOwnedSlice(allocator);
try username2groups_final.put(username, usergroups); try username2groups_final.put(username, usergroups);
} }
username2groups.deinit();
return Corpus{ return Corpus{
.arena = arena, .arena = arena,
@ -243,11 +244,13 @@ pub const UserGids = struct {
const userGidsErr = Allocator.Error || error{Overflow}; const userGidsErr = Allocator.Error || error{Overflow};
pub fn userGids(allocator: Allocator, corpus: *const Corpus) userGidsErr!UserGids { pub fn userGids(allocator: Allocator, corpus: *const Corpus) userGidsErr!UserGids {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var blob = ArrayList(u8).init(allocator); var blob = ArrayList(u8).init(allocator);
errdefer blob.deinit();
var name2offset = StringHashMap(u32).init(allocator); var name2offset = StringHashMap(u32).init(allocator);
errdefer name2offset.deinit();
var scratch = try allocator.alloc(u32, 256);
defer allocator.free(scratch);
for (corpus.users) |user| { for (corpus.users) |user| {
const usergroups_maybe = corpus.username2groups.get(user.name); const usergroups_maybe = corpus.username2groups.get(user.name);
if (usergroups_maybe == null) if (usergroups_maybe == null)
@ -255,19 +258,16 @@ pub fn userGids(allocator: Allocator, corpus: *const Corpus) userGidsErr!UserGid
const usergroups = usergroups_maybe.?; const usergroups = usergroups_maybe.?;
try name2offset.putNoClobber(user.name, try math.cast(u32, blob.items.len)); try name2offset.putNoClobber(user.name, try math.cast(u32, blob.items.len));
var deltaCompressedGids = try arena.allocator().alloc(u32, usergroups.len); scratch = try allocator.realloc(scratch, usergroups.len);
scratch.len = usergroups.len;
deltaCompressedGids.len = usergroups.len; for (usergroups) |group, i|
for (usergroups) |group, i| { scratch[i] = group.gid;
deltaCompressedGids[i] = group.gid; compress.deltaCompress(u32, scratch) catch |err| switch (err) {
}
compress.deltaCompress(u32, deltaCompressedGids) catch |err| switch (err) {
error.NotSorted => unreachable, error.NotSorted => unreachable,
}; };
try compress.appendUvarint(&blob, usergroups.len); try compress.appendUvarint(&blob, usergroups.len);
for (deltaCompressedGids) |gid| { for (scratch) |gid|
try compress.appendUvarint(&blob, gid); try compress.appendUvarint(&blob, gid);
}
} }
return UserGids{ return UserGids{