Skip to content

Commit

Permalink
perf: Persistent executor
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <[email protected]>
  • Loading branch information
dima74 authored and mversic committed Nov 26, 2024
1 parent 8836304 commit 898bec5
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 65 deletions.
13 changes: 8 additions & 5 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 @@ -165,12 +165,15 @@ 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 mut state_block = state.block(unverified_block.header());
let _ = criterion.bench_function("validate", |b| {
b.iter(|| match state_block.validate(transaction.clone()) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
});
b.iter(
|| match state_block.validate(transaction.clone(), &mut wasm_cache) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
},
);
});
state_block.commit();
println!("Success count: {success_count}, Failure count: {failure_count}");
Expand Down
14 changes: 10 additions & 4 deletions crates/iroha_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ mod new {
use std::collections::BTreeMap;

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

/// First stage in the life-cycle of a [`Block`].
///
Expand All @@ -266,14 +266,15 @@ mod new {
impl NewBlock {
/// Categorize transactions of this block to produce a [`ValidBlock`]
pub fn categorize(self, state_block: &mut StateBlock<'_>) -> WithEvents<ValidBlock> {
let mut wasm_cache = WasmCache::new();
let errors = self
.transactions
.iter()
// FIXME: Redundant clone
.cloned()
.enumerate()
.fold(BTreeMap::new(), |mut acc, (idx, tx)| {
if let Err((rejected_tx, error)) = state_block.validate(tx) {
if let Err((rejected_tx, error)) = state_block.validate(tx, &mut wasm_cache) {
iroha_logger::debug!(
block=%self.header.hash(),
tx=%rejected_tx.hash(),
Expand Down Expand Up @@ -338,7 +339,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 @@ -604,6 +607,7 @@ mod valid {
(params.sumeragi().max_clock_drift(), params.transaction)
};

let mut wasm_cache = WasmCache::new();
let errors = block
.transactions()
// FIXME: Redundant clone
Expand All @@ -626,7 +630,9 @@ mod valid {
)
}?;

if let Err((rejected_tx, error)) = state_block.validate(accepted_tx) {
if let Err((rejected_tx, error)) =
state_block.validate(accepted_tx, &mut wasm_cache)
{
iroha_logger::debug!(
tx=%rejected_tx.hash(),
block=%block.hash(),
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 898bec5

Please sign in to comment.