Skip to content

Commit

Permalink
state_machine: reduce memory usage by about 200 MiB
Browse files Browse the repository at this point in the history
This one is tricky! The big picture here is that we have a cache of
objects, which is a normal cache with arbitrary eviction policy.

However, we want to maintain an invariant --- all objects touched by a
bar of events must not be evicted during this bar.

To achieve that, we place a stash below the cache. The job of a stash is
to catch all objects that fall out from the cache inside a single bar
(between bars, the stash is reset).

What's the size of the stash that we need?

The conservative estimate is the number of queries for the cache.
That is, inserts + lookups, and that is, using the old logic,

    @as(u32, ObjectTree.Table.value_count_max) +
        (options.prefetch_entries_max * constants.lsm_batch_multiple)

The insight of this commit is that a lookup and an insert _for the same
key_ are double counted that way.

In other words, what we are interested in is not the amount of queries
to the cache overall, but the amount of _different keys_ the queries
touch.

And for most of operations, we are actually going to update exactly the
keys we've prefetched.

The three exceptions are:

- lookup transfers
- lookup accounts
- fetching dependant transfer for posting/voiding
  • Loading branch information
matklad committed Jan 16, 2024
1 parent fe70ebc commit 13a5fcf
Show file tree
Hide file tree
Showing 3 changed files with 26 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
2 changes: 1 addition & 1 deletion src/testing/state_machine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn StateMachineType(
.{
.things = .{
.cache_entries_max = 2048,
.prefetch_entries_max = 1,
.prefetch_entries_for_read_max = 0,
.tree_options_object = .{},
.tree_options_id = .{},
.tree_options_index = .{ .value = .{} },
Expand Down

0 comments on commit 13a5fcf

Please sign in to comment.