Skip to content

Commit

Permalink
feat(allocators): batch allocator (and use in hashmapdb)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnut committed Nov 26, 2024
1 parent 5fe52a0 commit 766d0ca
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/ledger/database/hashmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const RwLock = std.Thread.RwLock;
const BytesRef = database.interface.BytesRef;
const ColumnFamily = database.interface.ColumnFamily;
const DiskMemoryAllocator = sig.utils.allocators.DiskMemoryAllocator;
const BatchAllocator = sig.utils.allocators.BatchAllocator;
const IteratorDirection = database.interface.IteratorDirection;
const Logger = sig.trace.Logger;
const SortedMap = sig.utils.collections.SortedMap;
Expand All @@ -20,6 +21,7 @@ pub fn SharedHashMapDB(comptime column_families: []const ColumnFamily) type {
return struct {
ram_allocator: Allocator,
disk_allocator: Allocator,
batch_allocator_state: *BatchAllocator,
disk_allocator_state: *DiskMemoryAllocator,
maps: []SharedHashMap,
/// shared lock is required to call locking map methods.
Expand All @@ -37,12 +39,19 @@ pub fn SharedHashMapDB(comptime column_families: []const ColumnFamily) type {
logger.info().log("Initializing SharedHashMapDB");
const actual_path = try std.fmt.allocPrint(allocator, "{s}/hashmapdb", .{path});
defer allocator.free(actual_path);

const disk_allocator = try allocator.create(DiskMemoryAllocator);
disk_allocator.* = DiskMemoryAllocator{
.dir = try std.fs.cwd().makeOpenPath(actual_path, .{}),
.logger = logger.withScope(@typeName(DiskMemoryAllocator)),
.mmap_ratio = 8,
};
const batch_allocator = try allocator.create(BatchAllocator);
batch_allocator.* = BatchAllocator{
.backing_allocator = std.testing.allocator,
.batch_size = 1 << 30,
};

var maps = try allocator.alloc(SharedHashMap, column_families.len);
const lock = try allocator.create(RwLock);
lock.* = .{};
Expand All @@ -55,8 +64,9 @@ pub fn SharedHashMapDB(comptime column_families: []const ColumnFamily) type {
}
return .{
.ram_allocator = allocator,
.disk_allocator = disk_allocator.allocator(),
.disk_allocator = batch_allocator.allocator(),
.disk_allocator_state = disk_allocator,
.batch_allocator_state = batch_allocator,
.maps = maps,
.transaction_lock = lock,
};
Expand Down
Loading

0 comments on commit 766d0ca

Please sign in to comment.