Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Switch to pooling copy-on-write instantiation strategy for WASM #11232

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sc_consensus::{
use sc_service::{
config::{
DatabaseSource, KeepBlocks, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
PruningMode, WasmExecutionMethod,
PruningMode, WasmExecutionMethod, WasmInstantiationStrategy,
},
BasePath, Configuration, Role,
};
Expand Down Expand Up @@ -77,7 +77,9 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
state_pruning: PruningMode::ArchiveAll,
keep_blocks: KeepBlocks::All,
chain_spec: spec,
wasm_method: WasmExecutionMethod::Compiled,
wasm_method: WasmExecutionMethod::Compiled {
instantiation_strategy: WasmInstantiationStrategy::PoolingCopyOnWrite,
},
execution_strategies: ExecutionStrategies {
syncing: execution_strategy,
importing: execution_strategy,
Expand Down
6 changes: 5 additions & 1 deletion bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use node_runtime::{
UncheckedExtrinsic,
};
use node_testing::keyring::*;
#[cfg(feature = "wasmtime")]
use sc_executor::WasmInstantiationStrategy;
use sc_executor::{Externalities, NativeElseWasmExecutor, RuntimeVersionOf, WasmExecutionMethod};
use sp_core::{
storage::well_known_keys,
Expand Down Expand Up @@ -183,7 +185,9 @@ fn bench_execute_block(c: &mut Criterion) {
ExecutionMethod::Native,
ExecutionMethod::Wasm(WasmExecutionMethod::Interpreted),
#[cfg(feature = "wasmtime")]
ExecutionMethod::Wasm(WasmExecutionMethod::Compiled),
ExecutionMethod::Wasm(WasmExecutionMethod::Compiled {
instantiation_strategy: WasmInstantiationStrategy::PoolingCopyOnWrite,
}),
];

for strategy in execution_methods {
Expand Down
11 changes: 9 additions & 2 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use sc_client_api::{
};
use sc_client_db::PruningMode;
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult, ImportedAux};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod, WasmInstantiationStrategy};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_consensus::BlockOrigin;
Expand Down Expand Up @@ -399,7 +399,14 @@ impl BenchDb {
let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
let client = sc_service::new_client(
backend.clone(),
NativeElseWasmExecutor::new(WasmExecutionMethod::Compiled, None, 8, 2),
NativeElseWasmExecutor::new(
WasmExecutionMethod::Compiled {
instantiation_strategy: WasmInstantiationStrategy::PoolingCopyOnWrite,
},
None,
8,
2,
),
&keyring.generate_genesis(),
None,
None,
Expand Down
66 changes: 54 additions & 12 deletions client/cli/src/arg_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@

use clap::ArgEnum;

/// The instantiation strategy to use in compiled mode.
#[derive(Debug, Clone, Copy, ArgEnum)]
#[clap(rename_all = "kebab-case")]
pub enum WasmInstantiationStrategy {
/// Pool the instances to avoid initializing everything from scratch
/// on each instantiation. Use copy-on-write memory when possible.
PoolingCopyOnWrite,

/// Recreate the instance from scratch on every instantiation.
/// Use copy-on-write memory when possible.
RecreateInstanceCopyOnWrite,

/// Pool the instances to avoid initializing everything from scratch
/// on each instantiation.
Pooling,

/// Recreate the instance from scratch on every instantiation. Very slow.
RecreateInstance,

/// Legacy instance reuse mechanism. DEPRECATED. Will be removed. Do not use.
LegacyInstanceReuse,
}
koute marked this conversation as resolved.
Show resolved Hide resolved

/// The default [`WasmInstantiationStrategy`].
pub const DEFAULT_WASM_INSTANTIATION_STRATEGY: WasmInstantiationStrategy =
WasmInstantiationStrategy::PoolingCopyOnWrite;

/// How to execute Wasm runtime code.
#[derive(Debug, Clone, Copy)]
pub enum WasmExecutionMethod {
Expand Down Expand Up @@ -71,18 +98,33 @@ impl WasmExecutionMethod {
}
}

impl Into<sc_service::config::WasmExecutionMethod> for WasmExecutionMethod {
fn into(self) -> sc_service::config::WasmExecutionMethod {
match self {
WasmExecutionMethod::Interpreted =>
sc_service::config::WasmExecutionMethod::Interpreted,
#[cfg(feature = "wasmtime")]
WasmExecutionMethod::Compiled => sc_service::config::WasmExecutionMethod::Compiled,
#[cfg(not(feature = "wasmtime"))]
WasmExecutionMethod::Compiled => panic!(
"Substrate must be compiled with \"wasmtime\" feature for compiled Wasm execution"
),
}
/// Converts the execution method and instantiation strategy command line arguments
/// into an execution method which can be used internally.
pub fn execution_method_from_cli(
execution_method: WasmExecutionMethod,
_instantiation_strategy: WasmInstantiationStrategy,
) -> sc_service::config::WasmExecutionMethod {
match execution_method {
WasmExecutionMethod::Interpreted => sc_service::config::WasmExecutionMethod::Interpreted,
#[cfg(feature = "wasmtime")]
WasmExecutionMethod::Compiled => sc_service::config::WasmExecutionMethod::Compiled {
instantiation_strategy: match _instantiation_strategy {
WasmInstantiationStrategy::PoolingCopyOnWrite =>
sc_service::config::WasmInstantiationStrategy::PoolingCopyOnWrite,
WasmInstantiationStrategy::RecreateInstanceCopyOnWrite =>
sc_service::config::WasmInstantiationStrategy::RecreateInstanceCopyOnWrite,
WasmInstantiationStrategy::Pooling =>
sc_service::config::WasmInstantiationStrategy::Pooling,
WasmInstantiationStrategy::RecreateInstance =>
sc_service::config::WasmInstantiationStrategy::RecreateInstance,
WasmInstantiationStrategy::LegacyInstanceReuse =>
sc_service::config::WasmInstantiationStrategy::LegacyInstanceReuse,
},
},
#[cfg(not(feature = "wasmtime"))]
WasmExecutionMethod::Compiled => panic!(
"Substrate must be compiled with \"wasmtime\" feature for compiled Wasm execution"
),
}
}

Expand Down
22 changes: 17 additions & 5 deletions client/cli/src/params/import_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

use crate::{
arg_enums::{
ExecutionStrategy, WasmExecutionMethod, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION,
DEFAULT_EXECUTION_IMPORT_BLOCK, DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR,
DEFAULT_EXECUTION_OFFCHAIN_WORKER, DEFAULT_EXECUTION_OTHER, DEFAULT_EXECUTION_SYNCING,
DEFAULT_WASM_EXECUTION_METHOD,
ExecutionStrategy, WasmExecutionMethod, WasmInstantiationStrategy,
DEFAULT_EXECUTION_BLOCK_CONSTRUCTION, DEFAULT_EXECUTION_IMPORT_BLOCK,
DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR, DEFAULT_EXECUTION_OFFCHAIN_WORKER,
DEFAULT_EXECUTION_OTHER, DEFAULT_EXECUTION_SYNCING, DEFAULT_WASM_EXECUTION_METHOD,
DEFAULT_WASM_INSTANTIATION_STRATEGY,
},
params::{DatabaseParams, PruningParams},
};
Expand Down Expand Up @@ -58,6 +59,17 @@ pub struct ImportParams {
)]
pub wasm_method: WasmExecutionMethod,

/// The WASM instantiation method to use.
///
/// Only has an effect when `wasm-execution` is set to `compiled`.
#[clap(
long,
value_name = "STRATEGY",
default_value_t = DEFAULT_WASM_INSTANTIATION_STRATEGY,
arg_enum,
)]
pub wasm_instantiation_strategy: WasmInstantiationStrategy,

/// Specify the path where local WASM runtimes are stored.
///
/// These runtimes will override on-chain runtimes when the version matches.
Expand All @@ -81,7 +93,7 @@ impl ImportParams {

/// Get the WASM execution method from the parameters
pub fn wasm_method(&self) -> sc_service::config::WasmExecutionMethod {
self.wasm_method.into()
crate::execution_method_from_cli(self.wasm_method, self.wasm_instantiation_strategy)
}

/// Enable overriding on-chain WASM with locally-stored WASM
Expand Down
2 changes: 2 additions & 0 deletions client/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ paste = "1.0"
regex = "1.5.5"
criterion = "0.3"
env_logger = "0.9"
num_cpus = "1.13.1"
tempfile = "3.3.0"

[[bench]]
name = "bench"
Expand Down
Loading