Skip to content

Commit 34fbece

Browse files
committed
Set array capacity and use appendAssumeCapacity
1 parent d69c8a1 commit 34fbece

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/decoder.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub const Decoder = struct {
191191
const Value = std.meta.FieldType(DecodedType.KV, .value);
192192
var map = DecodedType.init(allocator);
193193
const map_len = field.size;
194-
try map.ensureTotalCapacity(@intCast(map_len));
194+
try map.ensureTotalCapacity(map_len);
195195

196196
for (0..map_len) |_| {
197197
const key = try self.decodeValue(allocator, Key);
@@ -209,11 +209,12 @@ pub const Decoder = struct {
209209
}
210210

211211
const Value = std.meta.Child(DecodedType.Slice);
212-
var array = std.ArrayList(Value).init(allocator);
213212
const array_len = field.size;
213+
var array = try std.ArrayList(Value).initCapacity(allocator, array_len);
214+
214215
for (0..array_len) |_| {
215216
const value = try self.decodeValue(allocator, Value);
216-
try array.append(value);
217+
array.appendAssumeCapacity(value);
217218
}
218219

219220
return array;

src/maxminddb.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test {
1717
}
1818

1919
fn expectEqualMaps(
20-
map: std.hash_map.StringHashMap([]const u8),
20+
map: std.StringArrayHashMap([]const u8),
2121
keys: []const []const u8,
2222
values: []const []const u8,
2323
) !void {

src/reader.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub const Reader = struct {
175175
// start with the node we traversed to as our to be processed stack.
176176
// Else the stack will be empty and we'll be returning an iterator that visits nothing.
177177
if (node < node_count) {
178-
try stack.append(WithinNode{
178+
stack.appendAssumeCapacity(WithinNode{
179179
.node = node,
180180
.ip_bytes = ip_bytes,
181181
.prefix_len = prefix_len,
@@ -360,15 +360,15 @@ fn Iterator(comptime T: type) type {
360360
}
361361
}
362362

363-
try self.stack.append(WithinNode{
363+
self.stack.appendAssumeCapacity(WithinNode{
364364
.node = node,
365365
.ip_bytes = right_ip_bytes,
366366
.prefix_len = current.prefix_len + 1,
367367
});
368368

369369
// In order traversal of the children on the left (0-bit).
370370
node = try reader.readNode(current.node, 0);
371-
try self.stack.append(WithinNode{
371+
self.stack.appendAssumeCapacity(WithinNode{
372372
.node = node,
373373
.ip_bytes = current.ip_bytes,
374374
.prefix_len = current.prefix_len + 1,

0 commit comments

Comments
 (0)