Skip to content

feat(collator): reuse storage cache for consequent transactions #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ weedb = "0.4.1"
zstd-safe = "7.2"
zstd-sys = "2.0"

tycho-executor = { git = "https://github.com/broxus/tycho-vm.git", rev = "c224351e576375430f4a2d66207406998d189946" }
tycho-vm = { git = "https://github.com/broxus/tycho-vm.git", rev = "c224351e576375430f4a2d66207406998d189946" }
tycho-executor = { git = "https://github.com/broxus/tycho-vm.git", rev = "8077daff38aa8887b2d41615dcbb10c6115c39e8" }
tycho-vm = { git = "https://github.com/broxus/tycho-vm.git", rev = "8077daff38aa8887b2d41615dcbb10c6115c39e8" }

# local deps
tycho-block-util = { path = "./block-util", version = "0.2.8" }
Expand Down
52 changes: 23 additions & 29 deletions collator/src/collator/execution_manager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::cmp;
use std::collections::hash_map::Entry;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use anyhow::Result;
use everscale_types::cell::HashBytes;
use everscale_types::models::*;
use humantime::format_duration;
use rayon::prelude::*;
Expand Down Expand Up @@ -113,18 +112,16 @@ impl MessagesExecutor {
let config = self.config.clone();
let params = self.params.clone();

let accounts_cache = Arc::new(&self.accounts_cache);
let accounts_cache = Mutex::new(&mut self.accounts_cache);
let result = msg_group
.into_par_iter()
.map(|(account_id, msgs)| {
Self::execute_subgroup(
account_id,
msgs,
&accounts_cache,
min_next_lt,
&config,
&params,
)
let account_state = accounts_cache
.lock()
.unwrap()
.get_account_stuff(&account_id)?;

Self::execute_messages(account_state, msgs, min_next_lt, &config, &params)
})
.collect_vec_list();

Expand Down Expand Up @@ -193,19 +190,6 @@ impl MessagesExecutor {
})
}

#[allow(clippy::vec_box)]
fn execute_subgroup(
account_id: HashBytes,
msgs: Vec<Box<ParsedMessage>>,
accounts_cache: &AccountsCache,
min_next_lt: u64,
config: &ParsedConfig,
params: &ExecutorParams,
) -> Result<ExecutedTransactions> {
let shard_account_stuff = accounts_cache.get_account_stuff(&account_id)?;
Self::execute_messages(shard_account_stuff, msgs, min_next_lt, config, params)
}

#[allow(clippy::too_many_arguments)]
fn save_subgroup_result(
&mut self,
Expand Down Expand Up @@ -391,9 +375,9 @@ impl AccountsCache {
Ok(None)
}

fn get_account_stuff(&self, account_id: &AccountId) -> Result<Box<ShardAccountStuff>> {
if let Some(account) = self.items.get(account_id) {
Ok(account.clone())
fn get_account_stuff(&mut self, account_id: &AccountId) -> Result<Box<ShardAccountStuff>> {
if let Some(account) = self.items.remove(account_id) {
Ok(account)
} else if let Some((_depth, shard_account)) = self.shard_accounts.get(account_id)? {
ShardAccountStuff::new(self.workchain_id, account_id, shard_account).map(Box::new)
} else {
Expand Down Expand Up @@ -458,7 +442,11 @@ fn execute_ordinary_transaction_impl(

let is_external = matches!(in_message.info, MsgInfo::ExtIn(_));

let mut inspector = ExecutorInspector::default();
let mut storage_cache = std::mem::take(&mut account_stuff.storage_cache);
let mut inspector = ExecutorInspector {
storage_cache: Some(&mut storage_cache),
..Default::default()
};
let uncommited = Executor::new(params, config)
.with_min_lt(min_lt)
.begin_ordinary_ext(
Expand Down Expand Up @@ -490,6 +478,7 @@ fn execute_ordinary_transaction_impl(
output.new_state_meta,
output.transaction.clone(),
inspector.public_libs_diff,
storage_cache,
);

Ok(ExecutedOrdinaryTransaction {
Expand Down Expand Up @@ -520,7 +509,11 @@ fn execute_ticktock_transaction(

let _histogram = HistogramGuard::begin("tycho_collator_execute_ticktock_time");

let mut inspector = ExecutorInspector::default();
let mut storage_cache = std::mem::take(&mut account_stuff.storage_cache);
let mut inspector = ExecutorInspector {
storage_cache: Some(&mut storage_cache),
..Default::default()
};
let uncommited = Executor::new(params, config)
.with_min_lt(min_lt)
.begin_tick_tock_ext(
Expand All @@ -545,6 +538,7 @@ fn execute_ticktock_transaction(
output.new_state_meta,
output.transaction.clone(),
inspector.public_libs_diff,
storage_cache,
);

Ok(Some(ExecutedTransaction {
Expand Down
6 changes: 5 additions & 1 deletion collator/src/collator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ impl AnchorInfo {

pub(super) type AccountId = HashBytes;

#[derive(Clone)]
pub(super) struct ShardAccountStuff {
pub workchain_id: i8,
pub account_addr: AccountId,
Expand All @@ -650,6 +649,7 @@ pub(super) struct ShardAccountStuff {
pub public_libs_diff: AccountPublicLibsDiff,
pub exists: bool,
pub transactions: BTreeMap<u64, (CurrencyCollection, Lazy<Transaction>)>,
pub storage_cache: tycho_executor::StorageCache,
}

pub type AccountPublicLibsDiff = FastHashMap<HashBytes, Option<Cell>>;
Expand Down Expand Up @@ -690,6 +690,7 @@ impl ShardAccountStuff {
public_libs_diff: FastHashMap::new(),
exists,
transactions: Default::default(),
storage_cache: Default::default(),
})
}

Expand All @@ -716,6 +717,7 @@ impl ShardAccountStuff {
public_libs_diff: FastHashMap::new(),
exists: false,
transactions: Default::default(),
storage_cache: Default::default(),
}
}

Expand All @@ -742,6 +744,7 @@ impl ShardAccountStuff {
account_meta: AccountMeta,
tx: Lazy<Transaction>,
mut public_libs_diff: Vec<PublicLibraryChange>,
storage_cache: tycho_executor::StorageCache,
) {
use std::collections::hash_map;

Expand All @@ -754,6 +757,7 @@ impl ShardAccountStuff {
self.transactions.insert(lt, (total_fees.into(), tx));
self.balance = account_meta.balance;
self.exists = account_meta.exists;
self.storage_cache = storage_cache;

if is_masterchain {
// Sort diff in reverse order (sort must be stable here).
Expand Down
Loading