Skip to content

Commit

Permalink
feat: prefetch accounts and access keys (near#7590)
Browse files Browse the repository at this point in the history
Introduces the concept of prefetching data from the DB while applying chunks.

This is non-speculative prefetching only for now. In other words, the future is not speculatively predicted, only data is fetched that is guaranteed to be useful. More details on that inside `runtime/runtime/src/prefetch.rs`.

No protocol change is needed for this. In general, prefetching how it has been implemented is (supposed to be) invisible in all possible ways, other than trie storage latency.

Performance wise, this is going to make the worst-case assumption for all action receipts better. The worst case is that two accounts and one access keys have to be fetched from disk for every receipt. This IO cost dominates the gas cost for action receipt creation.

Prefetching this data opens the door to potentially reducing this cost. This could affect all actions but is particularly relevant for redistributing gas costs around function call actions, see also near#6992.

----

Tests check that the prefetcher loads the trie nodes that are expected into the staging area and that they are removed from it afterwards.
  • Loading branch information
jakmeier committed Sep 15, 2022
1 parent 9a3a2ff commit 3277435
Show file tree
Hide file tree
Showing 15 changed files with 905 additions and 22 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ thiserror = "1"
lru = "0.7.2"
once_cell = "1.5.2"
rlimit = "0.7"
crossbeam = "0.8"

near-crypto = { path = "../crypto" }
near-o11y = { path = "../o11y" }
Expand Down
3 changes: 3 additions & 0 deletions core/store/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct StoreConfig {
/// We're still experimenting with this parameter and it seems decreasing its value can improve
/// the performance of the storage
pub trie_cache_capacities: Vec<(ShardUId, u64)>,
/// Enable fetching account and access key data ahead of time to avoid IO latency.
pub enable_receipt_prefetching: bool,
}

/// Mode in which to open the storage.
Expand Down Expand Up @@ -103,6 +105,7 @@ impl Default for StoreConfig {
block_size: bytesize::ByteSize::kib(16),

trie_cache_capacities: vec![(ShardUId { version: 1, shard_id: 3 }, 45_000_000)],
enable_receipt_prefetching: false,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ use crate::db::{
pub use crate::trie::iterator::TrieIterator;
pub use crate::trie::update::{TrieUpdate, TrieUpdateIterator, TrieUpdateValuePtr};
pub use crate::trie::{
estimator, split_state, ApplyStatePartResult, KeyForStateChanges, PartialStorage, ShardTries,
Trie, TrieCache, TrieCachingStorage, TrieChanges, TrieConfig, TrieStorage, WrappedTrieChanges,
estimator, split_state, ApplyStatePartResult, KeyForStateChanges, PartialStorage, PrefetchApi,
ShardTries, Trie, TrieCache, TrieCachingStorage, TrieChanges, TrieConfig, TrieStorage,
WrappedTrieChanges,
};

mod columns;
Expand Down
1 change: 1 addition & 0 deletions core/store/src/trie/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const TRIE_LIMIT_CACHED_VALUE_SIZE: usize = 1000;
pub struct TrieConfig {
pub shard_cache_config: ShardCacheConfig,
pub view_shard_cache_config: ShardCacheConfig,
pub enable_receipt_prefetching: bool,
}

pub struct ShardCacheConfig {
Expand Down
2 changes: 2 additions & 0 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use crate::trie::config::TrieConfig;
use crate::trie::insert_delete::NodesStorage;
use crate::trie::iterator::TrieIterator;
use crate::trie::nibble_slice::NibbleSlice;
pub use crate::trie::prefetching_trie_storage::PrefetchApi;
pub use crate::trie::shard_tries::{KeyForStateChanges, ShardTries, WrappedTrieChanges};
pub use crate::trie::trie_storage::{TrieCache, TrieCachingStorage, TrieStorage};
use crate::trie::trie_storage::{TrieMemoryPartialStorage, TrieRecordingStorage};
Expand All @@ -28,6 +29,7 @@ mod config;
mod insert_delete;
pub mod iterator;
mod nibble_slice;
mod prefetching_trie_storage;
mod shard_tries;
pub mod split_state;
mod state_parts;
Expand Down
Loading

0 comments on commit 3277435

Please sign in to comment.