Skip to content

Commit

Permalink
Merge pull request #1429 from tigerbeetle/matklad/thinner-cache-map
Browse files Browse the repository at this point in the history
state_machine: reduce memory usage by about 200 MiB
  • Loading branch information
matklad committed Jan 19, 2024
2 parents f96a0a4 + a3eb0a7 commit f7cf98e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/lsm/groove.zig
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,10 @@ pub fn GrooveType(
scan_builder: ScanBuilder,

pub const Options = struct {
/// The maximum number of objects that might be prefetched by a batch.
prefetch_entries_max: u32,
/// The maximum number of objects that might be prefetched and not modified by a batch.
prefetch_entries_for_read_max: u32,
/// The maximum number of objects that might be prefetched and then modified by a batch.
prefetch_entries_for_update_max: u32,
cache_entries_max: u32,

tree_options_object: ObjectTree.Options,
Expand All @@ -491,9 +493,10 @@ pub fn GrooveType(

// In the worst case, each Map must be able to store the value_count_max (to
// contain either TableMutable or TableImmutable) as well as the maximum number of
// prefetches a bar may perform.
// prefetches a bar may perform, excluding prefetches already accounted for by
// value_count_max.
.map_value_count_max = @as(u32, ObjectTree.Table.value_count_max) +
(options.prefetch_entries_max * constants.lsm_batch_multiple),
(options.prefetch_entries_for_read_max * constants.lsm_batch_multiple),

// Scopes are limited to a single beat, so the maximum number of entries in a
// single scope is value_count_max / constants.lsm_batch_multiple.
Expand Down Expand Up @@ -554,7 +557,10 @@ pub fn GrooveType(
}

var prefetch_keys = PrefetchKeys{};
try prefetch_keys.ensureTotalCapacity(allocator, options.prefetch_entries_max);
try prefetch_keys.ensureTotalCapacity(
allocator,
options.prefetch_entries_for_read_max + options.prefetch_entries_for_update_max,
);
errdefer prefetch_keys.deinit(allocator);

var scan_builder = if (has_scan) try ScanBuilder.init(allocator) else {};
Expand Down
23 changes: 14 additions & 9 deletions src/state_machine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1315,12 +1315,11 @@ pub fn StateMachineType(

return .{
.accounts = .{
.prefetch_entries_max = @max(
// create_account()/lookup_account() looks up 1 AccountImmutable per item.
batch_accounts_max,
// create_transfer()/post_or_void_pending_transfer() looks up 2
// AccountImmutables for every transfer.
2 * batch_transfers_max,
// lookup_account() looks up 1 AccountImmutable per item.
.prefetch_entries_for_read_max = batch_accounts_max,
.prefetch_entries_for_update_max = @max(
batch_accounts_max, // create_account()
2 * batch_transfers_max, // create_transfer(), debit and credit accounts
),
.cache_entries_max = options.cache_entries_accounts,
.tree_options_object = .{},
Expand All @@ -1338,8 +1337,11 @@ pub fn StateMachineType(
},
},
.transfers = .{
// *2 to fetch pending and post/void transfer.
.prefetch_entries_max = 2 * batch_transfers_max,
// lookup_transfer() looks up 1 Transfer.
// create_transfer() looks up at most 1 Transfer for posting/voiding.
.prefetch_entries_for_read_max = batch_transfers_max,
// create_transfer() updates a single Transfer.
.prefetch_entries_for_update_max = batch_transfers_max,
.cache_entries_max = options.cache_entries_transfers,
.tree_options_object = .{},
.tree_options_id = .{},
Expand All @@ -1357,7 +1359,10 @@ pub fn StateMachineType(
},
},
.posted = .{
.prefetch_entries_max = batch_transfers_max,
// Nothing lookups posted groove.
.prefetch_entries_for_read_max = 0,
// create_transfer() posts/voids at most one transfer.
.prefetch_entries_for_update_max = batch_transfers_max,
.cache_entries_max = options.cache_entries_posted,
.tree_options_object = .{},
.tree_options_id = {},
Expand Down
3 changes: 2 additions & 1 deletion src/testing/state_machine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ pub fn StateMachineType(
.{
.things = .{
.cache_entries_max = 2048,
.prefetch_entries_max = 1,
.prefetch_entries_for_read_max = 0,
.prefetch_entries_for_update_max = 1,
.tree_options_object = .{},
.tree_options_id = .{},
.tree_options_index = .{ .value = .{} },
Expand Down

0 comments on commit f7cf98e

Please sign in to comment.