Skip to content

Commit 049b4e0

Browse files
committed
More unmanaged conversions
1 parent 06d12bf commit 049b4e0

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/FileSegment.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dir: std.fs.Dir,
2222
info: SegmentInfo = .{},
2323
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
2424
docs: std.AutoHashMapUnmanaged(u32, bool) = .{},
25-
index: std.ArrayList(u32),
25+
index: std.ArrayListUnmanaged(u32) = .{},
2626
block_size: usize = 0,
2727
blocks: []const u8,
2828
merged: u32 = 0,
@@ -35,15 +35,14 @@ pub fn init(allocator: std.mem.Allocator, options: Options) Self {
3535
return Self{
3636
.allocator = allocator,
3737
.dir = options.dir,
38-
.index = std.ArrayList(u32).init(allocator),
3938
.blocks = undefined,
4039
};
4140
}
4241

4342
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
4443
self.attributes.deinit(self.allocator);
4544
self.docs.deinit(self.allocator);
46-
self.index.deinit();
45+
self.index.deinit(self.allocator);
4746

4847
if (self.raw_data) |data| {
4948
std.posix.munmap(data);
@@ -138,8 +137,8 @@ test "build" {
138137
source.info = .{ .version = 1 };
139138
source.frozen = true;
140139
try source.docs.put(source.allocator, 1, true);
141-
try source.items.append(.{ .id = 1, .hash = 1 });
142-
try source.items.append(.{ .id = 1, .hash = 2 });
140+
try source.items.append(source.allocator, .{ .id = 1, .hash = 1 });
141+
try source.items.append(source.allocator, .{ .id = 1, .hash = 2 });
143142

144143
var source_reader = source.reader();
145144
defer source_reader.close();

src/MemorySegment.zig

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ allocator: std.mem.Allocator,
2121
info: SegmentInfo = .{},
2222
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
2323
docs: std.AutoHashMapUnmanaged(u32, bool) = .{},
24-
items: std.ArrayList(Item),
24+
items: std.ArrayListUnmanaged(Item) = .{},
2525
frozen: bool = false,
2626

2727
pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
2828
_ = opts;
2929
return .{
3030
.allocator = allocator,
31-
.items = std.ArrayList(Item).init(allocator),
3231
};
3332
}
3433

@@ -37,7 +36,7 @@ pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
3736

3837
self.attributes.deinit(self.allocator);
3938
self.docs.deinit(self.allocator);
40-
self.items.deinit();
39+
self.items.deinit(self.allocator);
4140
}
4241

4342
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !void {
@@ -76,7 +75,7 @@ pub fn build(self: *Self, changes: []const Change) !void {
7675

7776
try self.attributes.ensureTotalCapacity(self.allocator, num_attributes);
7877
try self.docs.ensureTotalCapacity(self.allocator, num_docs);
79-
try self.items.ensureTotalCapacity(num_items);
78+
try self.items.ensureTotalCapacity(self.allocator, num_items);
8079

8180
var i = changes.len;
8281
while (i > 0) {
@@ -127,10 +126,10 @@ pub fn merge(self: *Self, merger: *SegmentMerger(Self)) !void {
127126
self.docs = merger.segment.docs.move();
128127

129128
self.items.clearRetainingCapacity();
130-
try self.items.ensureTotalCapacity(merger.estimated_size);
129+
try self.items.ensureTotalCapacity(self.allocator, merger.estimated_size);
131130
while (true) {
132131
const item = try merger.read() orelse break;
133-
try self.items.append(item);
132+
try self.items.append(self.allocator, item);
134133
merger.advance();
135134
}
136135
}

src/filefmt.zig

+9-8
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ test "writeBlock/readBlock/readFirstItemFromBlock" {
200200
var segment = MemorySegment.init(std.testing.allocator, .{});
201201
defer segment.deinit(.delete);
202202

203-
try segment.items.append(.{ .hash = 1, .id = 1 });
204-
try segment.items.append(.{ .hash = 2, .id = 1 });
205-
try segment.items.append(.{ .hash = 3, .id = 1 });
206-
try segment.items.append(.{ .hash = 3, .id = 2 });
207-
try segment.items.append(.{ .hash = 4, .id = 1 });
203+
try segment.items.ensureTotalCapacity(std.testing.allocator, 5);
204+
segment.items.appendAssumeCapacity(.{ .hash = 1, .id = 1 });
205+
segment.items.appendAssumeCapacity(.{ .hash = 2, .id = 1 });
206+
segment.items.appendAssumeCapacity(.{ .hash = 3, .id = 1 });
207+
segment.items.appendAssumeCapacity(.{ .hash = 3, .id = 2 });
208+
segment.items.appendAssumeCapacity(.{ .hash = 4, .id = 1 });
208209

209210
const block_size = 1024;
210211
var block_data: [block_size]u8 = undefined;
@@ -442,8 +443,8 @@ pub fn readSegmentFile(dir: fs.Dir, info: SegmentInfo, segment: *FileSegment) !v
442443

443444
const blocks_data_start = fixed_buffer_stream.pos;
444445

445-
const estimated_block_count = (raw_data.len - fixed_buffer_stream.pos) / block_size;
446-
try segment.index.ensureTotalCapacity(estimated_block_count);
446+
const max_possible_block_count = (raw_data.len - fixed_buffer_stream.pos) / block_size;
447+
try segment.index.ensureTotalCapacity(segment.allocator, max_possible_block_count);
447448

448449
var num_items: u32 = 0;
449450
var num_blocks: u32 = 0;
@@ -457,7 +458,7 @@ pub fn readSegmentFile(dir: fs.Dir, info: SegmentInfo, segment: *FileSegment) !v
457458
if (block_header.num_items == 0) {
458459
break;
459460
}
460-
try segment.index.append(block_header.first_item.hash);
461+
segment.index.appendAssumeCapacity(block_header.first_item.hash);
461462
num_items += block_header.num_items;
462463
num_blocks += 1;
463464
crc.update(block_data[0..]);

0 commit comments

Comments
 (0)