Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert data structures to *Unmanaged #36

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
More unmanaged conversions
  • Loading branch information
lalinsky committed Dec 2, 2024
commit 049b4e0c469947415ff95a36633ea599e9a6871f
9 changes: 4 additions & 5 deletions src/FileSegment.zig
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ dir: std.fs.Dir,
info: SegmentInfo = .{},
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
docs: std.AutoHashMapUnmanaged(u32, bool) = .{},
index: std.ArrayList(u32),
index: std.ArrayListUnmanaged(u32) = .{},
block_size: usize = 0,
blocks: []const u8,
merged: u32 = 0,
@@ -35,15 +35,14 @@ pub fn init(allocator: std.mem.Allocator, options: Options) Self {
return Self{
.allocator = allocator,
.dir = options.dir,
.index = std.ArrayList(u32).init(allocator),
.blocks = undefined,
};
}

pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
self.attributes.deinit(self.allocator);
self.docs.deinit(self.allocator);
self.index.deinit();
self.index.deinit(self.allocator);

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

var source_reader = source.reader();
defer source_reader.close();
11 changes: 5 additions & 6 deletions src/MemorySegment.zig
Original file line number Diff line number Diff line change
@@ -21,14 +21,13 @@ allocator: std.mem.Allocator,
info: SegmentInfo = .{},
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
docs: std.AutoHashMapUnmanaged(u32, bool) = .{},
items: std.ArrayList(Item),
items: std.ArrayListUnmanaged(Item) = .{},
frozen: bool = false,

pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
_ = opts;
return .{
.allocator = allocator,
.items = std.ArrayList(Item).init(allocator),
};
}

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

self.attributes.deinit(self.allocator);
self.docs.deinit(self.allocator);
self.items.deinit();
self.items.deinit(self.allocator);
}

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 {

try self.attributes.ensureTotalCapacity(self.allocator, num_attributes);
try self.docs.ensureTotalCapacity(self.allocator, num_docs);
try self.items.ensureTotalCapacity(num_items);
try self.items.ensureTotalCapacity(self.allocator, num_items);

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

self.items.clearRetainingCapacity();
try self.items.ensureTotalCapacity(merger.estimated_size);
try self.items.ensureTotalCapacity(self.allocator, merger.estimated_size);
while (true) {
const item = try merger.read() orelse break;
try self.items.append(item);
try self.items.append(self.allocator, item);
merger.advance();
}
}
17 changes: 9 additions & 8 deletions src/filefmt.zig
Original file line number Diff line number Diff line change
@@ -200,11 +200,12 @@ test "writeBlock/readBlock/readFirstItemFromBlock" {
var segment = MemorySegment.init(std.testing.allocator, .{});
defer segment.deinit(.delete);

try segment.items.append(.{ .hash = 1, .id = 1 });
try segment.items.append(.{ .hash = 2, .id = 1 });
try segment.items.append(.{ .hash = 3, .id = 1 });
try segment.items.append(.{ .hash = 3, .id = 2 });
try segment.items.append(.{ .hash = 4, .id = 1 });
try segment.items.ensureTotalCapacity(std.testing.allocator, 5);
segment.items.appendAssumeCapacity(.{ .hash = 1, .id = 1 });
segment.items.appendAssumeCapacity(.{ .hash = 2, .id = 1 });
segment.items.appendAssumeCapacity(.{ .hash = 3, .id = 1 });
segment.items.appendAssumeCapacity(.{ .hash = 3, .id = 2 });
segment.items.appendAssumeCapacity(.{ .hash = 4, .id = 1 });

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

const blocks_data_start = fixed_buffer_stream.pos;

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

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