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 2, 2024
1 parent 4ed4632 commit cdbbf17
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 61 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
14 changes: 9 additions & 5 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 @@ -217,9 +217,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 @@ -286,7 +287,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 @@ -595,6 +598,7 @@ mod valid {
(params.sumeragi().max_clock_drift(), params.transaction)
};

let mut wasm_cache = WasmCache::new();
block
.transactions()
// TODO: Unnecessary clone?
Expand All @@ -617,13 +621,13 @@ mod valid {
}?;

if error.is_some() {
match state_block.validate(tx) {
match state_block.validate(tx, &mut wasm_cache) {
Err(rejected_transaction) => Ok(rejected_transaction),
Ok(_) => Err(TransactionValidationError::RejectedIsValid),
}?;
} else {
state_block
.validate(tx)
.validate(tx, &mut wasm_cache)
.map_err(|(_tx, error)| TransactionValidationError::NotValid(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 validation");

Expand All @@ -140,18 +141,16 @@ impl Executor {
Ok(())
}
Self::UserProvided(loaded_executor) => {
let runtime =
wasm::RuntimeBuilder::<wasm::state::executor::ValidateTransaction>::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_validate_transaction(
let wasm_cache = WasmCache::change_lifetime(wasm_cache);
let mut runtime =
wasm_cache.create_runtime_cached(state_transaction, &loaded_executor.module)?;
let result = runtime.execute_executor_validate_transaction(
state_transaction,
authority,
&loaded_executor.module,
transaction,
)?
)?;
wasm_cache.save_cached_runtime(runtime);
result
}
}
}
Expand Down
Loading

0 comments on commit cdbbf17

Please sign in to comment.