WasmExecutor takes a cache directory (#8057)

That is useful for executors like wasmtime which produces compiled code
and can actually benefit from caching under some circumstances
This commit is contained in:
Sergei Shulepov
2021-02-09 17:48:29 +01:00
committed by GitHub
parent ef6615ba65
commit eb7226bb4a
6 changed files with 104 additions and 8 deletions
@@ -26,6 +26,7 @@ use std::{
panic::{UnwindSafe, AssertUnwindSafe},
result,
sync::{Arc, atomic::{AtomicU64, Ordering}, mpsc},
path::PathBuf,
};
use sp_version::{NativeVersion, RuntimeVersion};
@@ -102,6 +103,9 @@ pub struct WasmExecutor {
cache: Arc<RuntimeCache>,
/// The size of the instances cache.
max_runtime_instances: usize,
/// The path to a directory which the executor can leverage for a file cache, e.g. put there
/// compiled artifacts.
cache_path: Option<PathBuf>,
}
impl WasmExecutor {
@@ -112,19 +116,30 @@ impl WasmExecutor {
/// `method` - Method used to execute Wasm code.
///
/// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution.
/// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided.
/// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided.
///
/// `host_functions` - The set of host functions to be available for import provided by this
/// executor.
///
/// `max_runtime_instances` - The number of runtime instances to keep in memory ready for reuse.
///
/// `cache_path` - A path to a directory where the executor can place its files for purposes of
/// caching. This may be important in cases when there are many different modules with the
/// compiled execution method is used.
pub fn new(
method: WasmExecutionMethod,
default_heap_pages: Option<u64>,
host_functions: Vec<&'static dyn Function>,
max_runtime_instances: usize,
cache_path: Option<PathBuf>,
) -> Self {
WasmExecutor {
method,
default_heap_pages: default_heap_pages.unwrap_or(DEFAULT_HEAP_PAGES),
host_functions: Arc::new(host_functions),
cache: Arc::new(RuntimeCache::new(max_runtime_instances)),
cache: Arc::new(RuntimeCache::new(max_runtime_instances, cache_path.clone())),
max_runtime_instances,
cache_path,
}
}
@@ -210,6 +225,7 @@ impl sp_core::traits::CallInWasm for WasmExecutor {
&wasm_code,
self.host_functions.to_vec(),
allow_missing_host_functions,
self.cache_path.as_deref(),
)
.map_err(|e| format!("Failed to create module: {:?}", e))?;
@@ -267,6 +283,7 @@ impl<D: NativeExecutionDispatch> NativeExecutor<D> {
default_heap_pages,
host_functions,
max_runtime_instances,
None,
);
NativeExecutor {