Skip to content

Commit 898bec5

Browse files
dima74mversic
authored andcommitted
perf: Persistent executor
Signed-off-by: Dmitry Murzin <[email protected]>
1 parent 8836304 commit 898bec5

File tree

6 files changed

+275
-65
lines changed

6 files changed

+275
-65
lines changed

crates/iroha_core/benches/validation.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use iroha_core::{
66
block::*,
77
prelude::*,
88
query::store::LiveQueryStore,
9-
smartcontracts::{isi::Registrable as _, Execute},
9+
smartcontracts::{isi::Registrable as _, wasm::cache::WasmCache, Execute},
1010
state::{State, World},
1111
};
1212
use iroha_data_model::{
@@ -165,12 +165,15 @@ fn validate_transaction(criterion: &mut Criterion) {
165165
.expect("Failed to accept transaction.");
166166
let mut success_count = 0;
167167
let mut failure_count = 0;
168+
let mut wasm_cache = WasmCache::new();
168169
let mut state_block = state.block(unverified_block.header());
169170
let _ = criterion.bench_function("validate", |b| {
170-
b.iter(|| match state_block.validate(transaction.clone()) {
171-
Ok(_) => success_count += 1,
172-
Err(_) => failure_count += 1,
173-
});
171+
b.iter(
172+
|| match state_block.validate(transaction.clone(), &mut wasm_cache) {
173+
Ok(_) => success_count += 1,
174+
Err(_) => failure_count += 1,
175+
},
176+
);
174177
});
175178
state_block.commit();
176179
println!("Success count: {success_count}, Failure count: {failure_count}");

crates/iroha_core/src/block.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ mod new {
251251
use std::collections::BTreeMap;
252252

253253
use super::*;
254-
use crate::state::StateBlock;
254+
use crate::{smartcontracts::wasm::cache::WasmCache, state::StateBlock};
255255

256256
/// First stage in the life-cycle of a [`Block`].
257257
///
@@ -266,14 +266,15 @@ mod new {
266266
impl NewBlock {
267267
/// Categorize transactions of this block to produce a [`ValidBlock`]
268268
pub fn categorize(self, state_block: &mut StateBlock<'_>) -> WithEvents<ValidBlock> {
269+
let mut wasm_cache = WasmCache::new();
269270
let errors = self
270271
.transactions
271272
.iter()
272273
// FIXME: Redundant clone
273274
.cloned()
274275
.enumerate()
275276
.fold(BTreeMap::new(), |mut acc, (idx, tx)| {
276-
if let Err((rejected_tx, error)) = state_block.validate(tx) {
277+
if let Err((rejected_tx, error)) = state_block.validate(tx, &mut wasm_cache) {
277278
iroha_logger::debug!(
278279
block=%self.header.hash(),
279280
tx=%rejected_tx.hash(),
@@ -338,7 +339,9 @@ mod valid {
338339
use mv::storage::StorageReadOnly;
339340

340341
use super::*;
341-
use crate::{state::StateBlock, sumeragi::network_topology::Role};
342+
use crate::{
343+
smartcontracts::wasm::cache::WasmCache, state::StateBlock, sumeragi::network_topology::Role,
344+
};
342345

343346
/// Block that was validated and accepted
344347
#[derive(Debug, Clone)]
@@ -604,6 +607,7 @@ mod valid {
604607
(params.sumeragi().max_clock_drift(), params.transaction)
605608
};
606609

610+
let mut wasm_cache = WasmCache::new();
607611
let errors = block
608612
.transactions()
609613
// FIXME: Redundant clone
@@ -626,7 +630,9 @@ mod valid {
626630
)
627631
}?;
628632

629-
if let Err((rejected_tx, error)) = state_block.validate(accepted_tx) {
633+
if let Err((rejected_tx, error)) =
634+
state_block.validate(accepted_tx, &mut wasm_cache)
635+
{
630636
iroha_logger::debug!(
631637
tx=%rejected_tx.hash(),
632638
block=%block.hash(),

crates/iroha_core/src/executor.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use serde::{
1818
};
1919

2020
use crate::{
21-
smartcontracts::{wasm, Execute as _},
21+
smartcontracts::{wasm, wasm::cache::WasmCache, Execute as _},
2222
state::{deserialize::WasmSeed, StateReadOnly, StateTransaction},
2323
WorldReadOnly as _,
2424
};
@@ -122,6 +122,7 @@ impl Executor {
122122
state_transaction: &mut StateTransaction<'_, '_>,
123123
authority: &AccountId,
124124
transaction: SignedTransaction,
125+
wasm_cache: &mut WasmCache<'_, '_, '_>,
125126
) -> Result<(), ValidationFail> {
126127
trace!("Running transaction execution");
127128

@@ -140,18 +141,16 @@ impl Executor {
140141
Ok(())
141142
}
142143
Self::UserProvided(loaded_executor) => {
143-
let runtime =
144-
wasm::RuntimeBuilder::<wasm::state::executor::ExecuteTransaction>::new()
145-
.with_engine(state_transaction.engine.clone()) // Cloning engine is cheap, see [`wasmtime::Engine`] docs
146-
.with_config(state_transaction.world.parameters().executor)
147-
.build()?;
148-
149-
runtime.execute_executor_execute_transaction(
144+
let wasm_cache = WasmCache::change_lifetime(wasm_cache);
145+
let mut runtime = wasm_cache
146+
.take_or_create_cached_runtime(state_transaction, &loaded_executor.module)?;
147+
let result = runtime.execute_executor_execute_transaction(
150148
state_transaction,
151149
authority,
152-
&loaded_executor.module,
153150
transaction,
154-
)?
151+
)?;
152+
wasm_cache.put_cached_runtime(runtime);
153+
result
155154
}
156155
}
157156
}

0 commit comments

Comments
 (0)