Moved to File.MemoryMap instead of direct file I/O
This commit is contained in:
+21
-18
@@ -5,18 +5,17 @@ const Decompressor = @import("util/decompressor.zig");
|
||||
const MetadataReader = @import("util/metadata.zig");
|
||||
const OffsetFile = @import("util/offset_file.zig");
|
||||
|
||||
pub fn lookupValue(comptime T: anytype, alloc: std.mem.Allocator, io: Io, decomp: *const Decompressor, file: OffsetFile, table_start: u64, idx: u32) !T {
|
||||
pub fn lookupValue(comptime T: anytype, alloc: std.mem.Allocator, decomp: *const Decompressor, file: OffsetFile, table_start: u64, idx: u32) !T {
|
||||
const T_PER_BLOCK: u16 = 8192 / @sizeOf(T);
|
||||
|
||||
const block = idx / T_PER_BLOCK;
|
||||
const block_offset = idx % T_PER_BLOCK;
|
||||
|
||||
var rdr = try file.readerAt(io, table_start + (8 * block), &[0]u8{});
|
||||
var offset: u64 = undefined;
|
||||
try rdr.interface.readSliceEndian(u64, @ptrCast(&offset), .little);
|
||||
const offset_pos = table_start + (8 * block);
|
||||
const offset: u64 = std.mem.readInt(u64, file.map.memory[offset_pos .. offset_pos + 8], .little);
|
||||
|
||||
rdr = try file.readerAt(io, offset, &[0]u8{});
|
||||
var meta: MetadataReader = .init(alloc, &rdr.interface, decomp);
|
||||
var rdr = file.readerAt(offset);
|
||||
var meta: MetadataReader = .init(alloc, &rdr, decomp);
|
||||
|
||||
try meta.interface.discardAll(@sizeOf(T) * block_offset);
|
||||
var out: T = undefined;
|
||||
@@ -41,7 +40,7 @@ pub fn CachedTable(comptime T: anytype) type {
|
||||
|
||||
table: std.AutoHashMap(u32, []T),
|
||||
|
||||
mut: Io.Mutex = .init,
|
||||
mut: Io.RwLock = .init,
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator, fil: OffsetFile, decomp: *const Decompressor, offset: u64, total_num: u32) Table {
|
||||
return .{
|
||||
@@ -72,16 +71,15 @@ pub fn CachedTable(comptime T: anytype) type {
|
||||
num_blocks += 1;
|
||||
|
||||
for (0..num_blocks) |block| {
|
||||
var rdr = try self.fil.readerAt(io, self.table_start + (8 * block), &[0]u8{});
|
||||
var offset: u64 = undefined;
|
||||
try rdr.interface.readSliceEndian(u64, @ptrCast(&offset), .little);
|
||||
const offset_pos = self.table_start + (8 * block);
|
||||
const offset: u64 = std.mem.readInt(u64, self.fil.map.memory[offset_pos .. offset_pos + 8], .little);
|
||||
|
||||
const len: u16 = if (self.total_num % T_PER_BLOCK != 0 and block == (self.total_num - 1) / T_PER_BLOCK)
|
||||
@truncate(self.total_num % T_PER_BLOCK)
|
||||
else
|
||||
T_PER_BLOCK;
|
||||
|
||||
rdr = try self.fil.readerAt(io, offset, &[0]u8{});
|
||||
var rdr = self.fil.readerAt(offset);
|
||||
var meta: MetadataReader = .init(self.alloc, &rdr.interface, self.decomp);
|
||||
|
||||
const slice = try meta.interface.readSliceEndianAlloc(self.alloc, T, len, .little);
|
||||
@@ -92,8 +90,14 @@ pub fn CachedTable(comptime T: anytype) type {
|
||||
pub fn get(self: *Table, io: Io, idx: u32) Error!T {
|
||||
const block = idx / T_PER_BLOCK;
|
||||
const block_offset = idx % T_PER_BLOCK;
|
||||
if (self.table.contains(block))
|
||||
return self.table.get(block).?[block_offset];
|
||||
|
||||
{
|
||||
try self.mut.lockShared(io);
|
||||
defer self.mut.unlockShared(io);
|
||||
|
||||
if (self.table.contains(block))
|
||||
return self.table.get(block).?[block_offset];
|
||||
}
|
||||
|
||||
try self.mut.lock(io);
|
||||
defer self.mut.unlock(io);
|
||||
@@ -101,20 +105,19 @@ pub fn CachedTable(comptime T: anytype) type {
|
||||
if (self.table.contains(block))
|
||||
return self.table.get(block).?[block_offset];
|
||||
|
||||
var rdr = try self.fil.readerAt(io, self.table_start + (8 * block), &[0]u8{});
|
||||
var offset: u64 = undefined;
|
||||
try rdr.interface.readSliceEndian(u64, @ptrCast(&offset), .little);
|
||||
const offset_pos = self.table_start + (8 * block);
|
||||
const offset: u64 = std.mem.readInt(u64, self.fil.map.memory[offset_pos .. offset_pos + 8], .little);
|
||||
|
||||
const len: u16 = if (self.total_num % T_PER_BLOCK != 0 and block == (self.total_num - 1) / T_PER_BLOCK)
|
||||
@truncate(self.total_num % T_PER_BLOCK)
|
||||
else
|
||||
T_PER_BLOCK;
|
||||
|
||||
rdr = try self.fil.readerAt(io, offset, &[0]u8{});
|
||||
var rdr = self.fil.readerAt(offset);
|
||||
var meta: MetadataReader = .init(self.alloc, &rdr.interface, self.decomp);
|
||||
|
||||
const slice = try meta.interface.readSliceEndianAlloc(self.alloc, T, len, .little);
|
||||
try self.table.put(block, slice);
|
||||
try self.table.put(@truncate(block), slice);
|
||||
|
||||
return slice[block_offset];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user