forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor WASM module instantiation (paritytech#10480)
* Refactor WASM module instantiation; enable WASM instance pooling * Disable the `uffd` feature on `wasmtime` * Restore the original behavior regarding the initial WASM memory size * Adjust error message * Remove unnecessary import in the benchmarks * Preinstantiate the WASM runtime for a slight speedup * Delete the asserts in `convert_memory_import_into_export` * `return` -> `break` * Revert WASM instance pooling for now * Have `convert_memory_import_into_export` return an error instead of panic * Update the warning when an import is missing * Rustfmt and clippy fix * Fix executor benchmarks' compilation without `wasmtime` being enabled * rustfmt again * Align to review comments * Extend tests so that both imported and exported memories are tested * Increase the number of heap pages for exported memories too * Fix `decommit_works` test
- Loading branch information
1 parent
32ee913
commit 98ec2a0
Showing
9 changed files
with
468 additions
and
323 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2021 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
|
||
use sc_executor_common::{runtime_blob::RuntimeBlob, wasm_runtime::WasmModule}; | ||
use sc_runtime_test::wasm_binary_unwrap as test_runtime; | ||
use sp_wasm_interface::HostFunctions as _; | ||
use std::sync::Arc; | ||
|
||
enum Method { | ||
Interpreted, | ||
#[cfg(feature = "wasmtime")] | ||
Compiled { | ||
fast_instance_reuse: bool, | ||
}, | ||
} | ||
|
||
// This is just a bog-standard Kusama runtime with the extra `test_empty_return` | ||
// function copy-pasted from the test runtime. | ||
fn kusama_runtime() -> &'static [u8] { | ||
include_bytes!("kusama_runtime.wasm") | ||
} | ||
|
||
fn initialize(runtime: &[u8], method: Method) -> Arc<dyn WasmModule> { | ||
let blob = RuntimeBlob::uncompress_if_needed(runtime).unwrap(); | ||
let host_functions = sp_io::SubstrateHostFunctions::host_functions(); | ||
let heap_pages = 2048; | ||
let allow_missing_func_imports = true; | ||
|
||
match method { | ||
Method::Interpreted => sc_executor_wasmi::create_runtime( | ||
blob, | ||
heap_pages, | ||
host_functions, | ||
allow_missing_func_imports, | ||
) | ||
.map(|runtime| -> Arc<dyn WasmModule> { Arc::new(runtime) }), | ||
#[cfg(feature = "wasmtime")] | ||
Method::Compiled { fast_instance_reuse } => | ||
sc_executor_wasmtime::create_runtime::<sp_io::SubstrateHostFunctions>( | ||
blob, | ||
sc_executor_wasmtime::Config { | ||
heap_pages, | ||
max_memory_size: None, | ||
allow_missing_func_imports, | ||
cache_path: None, | ||
semantics: sc_executor_wasmtime::Semantics { | ||
fast_instance_reuse, | ||
deterministic_stack_limit: None, | ||
canonicalize_nans: false, | ||
parallel_compilation: true, | ||
}, | ||
}, | ||
) | ||
.map(|runtime| -> Arc<dyn WasmModule> { Arc::new(runtime) }), | ||
} | ||
.unwrap() | ||
} | ||
|
||
fn bench_call_instance(c: &mut Criterion) { | ||
let _ = env_logger::try_init(); | ||
|
||
#[cfg(feature = "wasmtime")] | ||
{ | ||
let runtime = initialize(test_runtime(), Method::Compiled { fast_instance_reuse: true }); | ||
c.bench_function("call_instance_test_runtime_with_fast_instance_reuse", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()) | ||
}); | ||
} | ||
|
||
#[cfg(feature = "wasmtime")] | ||
{ | ||
let runtime = initialize(test_runtime(), Method::Compiled { fast_instance_reuse: false }); | ||
c.bench_function("call_instance_test_runtime_without_fast_instance_reuse", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()); | ||
}); | ||
} | ||
|
||
#[cfg(feature = "wasmtime")] | ||
{ | ||
let runtime = initialize(kusama_runtime(), Method::Compiled { fast_instance_reuse: true }); | ||
c.bench_function("call_instance_kusama_runtime_with_fast_instance_reuse", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()) | ||
}); | ||
} | ||
|
||
#[cfg(feature = "wasmtime")] | ||
{ | ||
let runtime = initialize(kusama_runtime(), Method::Compiled { fast_instance_reuse: false }); | ||
c.bench_function("call_instance_kusama_runtime_without_fast_instance_reuse", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()); | ||
}); | ||
} | ||
|
||
{ | ||
let runtime = initialize(test_runtime(), Method::Interpreted); | ||
c.bench_function("call_instance_test_runtime_interpreted", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()) | ||
}); | ||
} | ||
|
||
{ | ||
let runtime = initialize(kusama_runtime(), Method::Interpreted); | ||
c.bench_function("call_instance_kusama_runtime_interpreted", |b| { | ||
let mut instance = runtime.new_instance().unwrap(); | ||
b.iter(|| instance.call_export("test_empty_return", &[0]).unwrap()) | ||
}); | ||
} | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = bench_call_instance | ||
} | ||
criterion_main!(benches); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.