Skip to content

Commit 3221552

Browse files
committed
Avoid memcpy-ing all segment blocks during load
1 parent 32520fa commit 3221552

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/filefmt.zig

+9-8
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub fn readSegmentFile(dir: fs.Dir, info: SegmentInfo, segment: *FileSegment) !v
451451

452452
const block_size = header.block_size;
453453
const padding_size = block_size - fixed_buffer_stream.pos % block_size;
454-
try reader.skipBytes(padding_size, .{});
454+
try fixed_buffer_stream.seekBy(@intCast(padding_size));
455455

456456
const blocks_data_start = fixed_buffer_stream.pos;
457457

@@ -462,23 +462,24 @@ pub fn readSegmentFile(dir: fs.Dir, info: SegmentInfo, segment: *FileSegment) !v
462462
var num_blocks: u32 = 0;
463463
var crc = std.hash.crc.Crc64Xz.init();
464464

465-
var block_data_buffer: [max_block_size]u8 = undefined;
466-
var block_data = block_data_buffer[0..block_size];
467-
while (true) {
468-
try reader.readNoEof(block_data);
465+
var ptr = blocks_data_start;
466+
while (ptr + block_size <= raw_data.len) {
467+
const block_data = raw_data[ptr .. ptr + block_size];
468+
ptr += block_size;
469469
const block_header = try decodeBlockHeader(block_data, segment.min_doc_id);
470470
if (block_header.num_items == 0) {
471471
break;
472472
}
473473
segment.index.appendAssumeCapacity(block_header.first_item.hash);
474474
num_items += block_header.num_items;
475475
num_blocks += 1;
476-
crc.update(block_data[0..]);
476+
crc.update(block_data);
477477
}
478-
479-
const blocks_data_end = fixed_buffer_stream.pos;
478+
const blocks_data_end = ptr;
480479
segment.blocks = raw_data[blocks_data_start..blocks_data_end];
481480

481+
try fixed_buffer_stream.seekBy(@intCast(segment.blocks.len));
482+
482483
const footer = try unpacker.read(SegmentFileFooter);
483484
if (footer.magic != segment_file_footer_magic_v1) {
484485
return error.InvalidSegment;

0 commit comments

Comments
 (0)