aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-07-12 18:21:19 -0400
committerGitHub <noreply@github.com>2021-07-12 18:21:19 -0400
commit9b79ce1cd302a196bbb49e5fa89657bd840f4066 (patch)
treef717633b02bdd9cf1c7bc724908e8d19ef097981 /lib
parent3063f0a5ed373947badd0af056db310283c76e37 (diff)
parent28dd9d478d24190ab5c8c4b892d7dfc16c380ae0 (diff)
downloadzig-9b79ce1cd302a196bbb49e5fa89657bd840f4066.tar.gz
zig-9b79ce1cd302a196bbb49e5fa89657bd840f4066.zip
Merge pull request #9365 from ifreund/hash-map-fix
std/hash_map: fix ensureUnusedCapacity() over-allocating
Diffstat (limited to 'lib')
-rw-r--r--lib/std/hash_map.zig15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
index d6762c9d3b..ef97e7d7fa 100644
--- a/lib/std/hash_map.zig
+++ b/lib/std/hash_map.zig
@@ -848,7 +848,7 @@ pub fn HashMapUnmanaged(
return ensureUnusedCapacityContext(self, allocator, additional_size, undefined);
}
pub fn ensureUnusedCapacityContext(self: *Self, allocator: *Allocator, additional_size: Size, ctx: Context) !void {
- return ensureTotalCapacityContext(self, allocator, self.capacity() + additional_size, ctx);
+ return ensureTotalCapacityContext(self, allocator, self.count() + additional_size, ctx);
}
pub fn clearRetainingCapacity(self: *Self) void {
@@ -1956,6 +1956,19 @@ test "std.hash_map getOrPutAdapted" {
}
}
+test "std.hash_map ensureUnusedCapacity" {
+ var map = AutoHashMap(u64, u64).init(testing.allocator);
+ defer map.deinit();
+
+ try map.ensureUnusedCapacity(32);
+ const capacity = map.capacity();
+ try map.ensureUnusedCapacity(32);
+
+ // Repeated ensureUnusedCapacity() calls with no insertions between
+ // should not change the capacity.
+ try testing.expectEqual(capacity, map.capacity());
+}
+
test "compile everything" {
std.testing.refAllDecls(AutoHashMap(i32, i32));
std.testing.refAllDecls(StringHashMap([]const u8));