From a3eb0a7969a7a85e4e64d11c4346188095600885 Mon Sep 17 00:00:00 2001 From: Alex Kladov Date: Mon, 15 Jan 2024 15:08:48 +0000 Subject: [PATCH] state_machine: reduce memory usage by about 200 MiB 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 --- src/lsm/groove.zig | 16 +++++++++++----- src/state_machine.zig | 23 ++++++++++++++--------- src/testing/state_machine.zig | 3 ++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/lsm/groove.zig b/src/lsm/groove.zig index 855ca4f59f..59dc602209 100644 --- a/src/lsm/groove.zig +++ b/src/lsm/groove.zig @@ -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, @@ -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. @@ -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 {}; diff --git a/src/state_machine.zig b/src/state_machine.zig index 0ff5820c80..cc0b64f776 100644 --- a/src/state_machine.zig +++ b/src/state_machine.zig @@ -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 = .{}, @@ -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 = .{}, @@ -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 = {}, diff --git a/src/testing/state_machine.zig b/src/testing/state_machine.zig index 684c7378e1..3d1c5ae771 100644 --- a/src/testing/state_machine.zig +++ b/src/testing/state_machine.zig @@ -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 = .{} },