This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Refactor WASM module instantiation #10480
Merged
paritytech-processbot
merged 22 commits into
paritytech:master
from
koute:master_wasmtime_pooling
Mar 19, 2022
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f4d9282
Refactor WASM module instantiation; enable WASM instance pooling
koute 6b9b777
Disable the `uffd` feature on `wasmtime`
koute d982eeb
Merge branch 'master' into master_wasmtime_pooling
koute 92cbf66
Restore the original behavior regarding the initial WASM memory size
koute 719aadd
Adjust error message
koute d9e055b
Remove unnecessary import in the benchmarks
koute 2f53ad6
Preinstantiate the WASM runtime for a slight speedup
koute 19daf56
Delete the asserts in `convert_memory_import_into_export`
koute ae988e2
`return` -> `break`
koute 44c5086
Revert WASM instance pooling for now
koute afd355b
Have `convert_memory_import_into_export` return an error instead of p…
koute 3e8932f
Update the warning when an import is missing
koute 0a61182
Merge branch 'master' into master_wasmtime_pooling
koute 7370f41
Rustfmt and clippy fix
koute 539347e
Fix executor benchmarks' compilation without `wasmtime` being enabled
koute 7f0d17a
rustfmt again
koute 780b0ec
Align to review comments
koute 72aa655
Extend tests so that both imported and exported memories are tested
koute 3a15f68
Increase the number of heap pages for exported memories too
koute 3cb916e
Merge branch 'master' into master_wasmtime_pooling
koute 3b4ca22
Fix `decommit_works` test
koute e3be4e7
Merge branch 'master' into master_wasmtime_pooling
koute File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.