Skip to content

Commit

Permalink
perf: Persistent executor
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <diralik@yandex.ru>
  • Loading branch information
dima74 committed Oct 10, 2024
1 parent a5794ef commit 3be87fd
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 62 deletions.
5 changes: 3 additions & 2 deletions crates/iroha_core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iroha_core::{
block::*,
prelude::*,
query::store::LiveQueryStore,
smartcontracts::{isi::Registrable as _, Execute},
smartcontracts::{isi::Registrable as _, wasm::cache::WasmCache, Execute},
state::{State, World},
};
use iroha_data_model::{
Expand Down Expand Up @@ -132,10 +132,11 @@ fn validate_transaction(criterion: &mut Criterion) {
.expect("Failed to accept transaction.");
let mut success_count = 0;
let mut failure_count = 0;
let mut wasm_cache = WasmCache::new();
let _ = criterion.bench_function("validate", move |b| {
b.iter(|| {
let mut state_block = state.block();
match state_block.validate(transaction.clone()) {
match state_block.validate(transaction.clone(), &mut wasm_cache) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
}
Expand Down
12 changes: 8 additions & 4 deletions crates/iroha_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mod pending {
use nonzero_ext::nonzero;

use super::*;
use crate::state::StateBlock;
use crate::{smartcontracts::wasm::cache::WasmCache, state::StateBlock};

/// First stage in the life-cycle of a [`Block`].
/// In the beginning the block is assumed to be verified and to contain only accepted transactions.
Expand Down Expand Up @@ -231,9 +231,10 @@ mod pending {
transactions: Vec<AcceptedTransaction>,
state_block: &mut StateBlock<'_>,
) -> Vec<CommittedTransaction> {
let mut wasm_cache = WasmCache::new();
transactions
.into_iter()
.map(|tx| match state_block.validate(tx) {
.map(|tx| match state_block.validate(tx, &mut wasm_cache) {
Ok(tx) => CommittedTransaction {
value: tx,
error: None,
Expand Down Expand Up @@ -299,7 +300,9 @@ mod valid {
use mv::storage::StorageReadOnly;

use super::*;
use crate::{state::StateBlock, sumeragi::network_topology::Role};
use crate::{
smartcontracts::wasm::cache::WasmCache, state::StateBlock, sumeragi::network_topology::Role,
};

/// Block that was validated and accepted
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -612,6 +615,7 @@ mod valid {
(params.sumeragi().max_clock_drift(), params.transaction)
};

let mut wasm_cache = WasmCache::new();
for CommittedTransaction { value, error } in block.transactions_mut() {
let tx = if is_genesis {
AcceptedTransaction::accept_genesis(
Expand All @@ -629,7 +633,7 @@ mod valid {
)
}?;

*error = match state_block.validate(tx) {
*error = match state_block.validate(tx, &mut wasm_cache) {
Ok(_) => None,
Err((_tx, error)) => Some(Box::new(error)),
};
Expand Down
19 changes: 9 additions & 10 deletions crates/iroha_core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde::{
};

use crate::{
smartcontracts::{wasm, Execute as _},
smartcontracts::{wasm, wasm::cache::WasmCache, Execute as _},
state::{deserialize::WasmSeed, StateReadOnly, StateTransaction},
WorldReadOnly as _,
};
Expand Down Expand Up @@ -122,6 +122,7 @@ impl Executor {
state_transaction: &mut StateTransaction<'_, '_>,
authority: &AccountId,
transaction: SignedTransaction,
wasm_cache: &mut WasmCache<'_, '_, '_>,
) -> Result<(), ValidationFail> {
trace!("Running transaction execution");

Expand All @@ -140,18 +141,16 @@ impl Executor {
Ok(())
}
Self::UserProvided(loaded_executor) => {
let runtime =
wasm::RuntimeBuilder::<wasm::state::executor::ExecuteTransaction>::new()
.with_engine(state_transaction.engine.clone()) // Cloning engine is cheap, see [`wasmtime::Engine`] docs
.with_config(state_transaction.world.parameters().executor)
.build()?;

runtime.execute_executor_execute_transaction(
let wasm_cache = WasmCache::change_lifetime(wasm_cache);
let mut runtime = wasm_cache
.take_or_create_cached_runtime(state_transaction, &loaded_executor.module)?;
let result = runtime.execute_executor_execute_transaction(
state_transaction,
authority,
&loaded_executor.module,
transaction,
)?
)?;
wasm_cache.put_cached_runtime(runtime);
result
}
}
}
Expand Down
Loading

0 comments on commit 3be87fd

Please sign in to comment.