Rename and use --max-heap-pages parameter (#1666)

This commit is contained in:
Stanislav Tkach
2019-02-05 13:35:12 +02:00
committed by Gav Wood
parent 888e7bb76f
commit df685c9e53
10 changed files with 45 additions and 34 deletions
+19 -12
View File
@@ -51,6 +51,7 @@ fn fetch_cached_runtime_version<'a, E: Externalities<Blake2Hasher>>(
wasm_executor: &WasmExecutor,
cache: &'a mut RefMut<CacheType>,
ext: &mut E,
default_heap_pages: Option<u64>,
) -> Result<(&'a WasmModuleInstanceRef, &'a Option<RuntimeVersion>)> {
let code_hash = match ext.storage_hash(well_known_keys::CODE) {
@@ -63,10 +64,10 @@ fn fetch_cached_runtime_version<'a, E: Externalities<Blake2Hasher>>(
Some(code) => code,
None => return RuntimePreproc::InvalidCode,
};
let heap_pages = match ext.storage(well_known_keys::HEAP_PAGES) {
Some(pages) => u64::decode(&mut &pages[..]).unwrap_or(DEFAULT_HEAP_PAGES),
None => DEFAULT_HEAP_PAGES,
};
let heap_pages = ext.storage(well_known_keys::HEAP_PAGES)
.and_then(|pages| u64::decode(&mut &pages[..]))
.or(default_heap_pages)
.unwrap_or(DEFAULT_HEAP_PAGES);
match WasmModule::from_buffer(code)
.map_err(|_| ErrorKind::InvalidCode(vec![]).into())
.and_then(|module| wasm_executor.prepare_module(ext, heap_pages as usize, &module))
@@ -125,7 +126,7 @@ pub trait NativeExecutionDispatch: Send + Sync {
fn native_version() -> NativeVersion;
/// Construct corresponding `NativeExecutor`
fn new() -> NativeExecutor<Self> where Self: Sized;
fn new(default_heap_pages: Option<u64>) -> NativeExecutor<Self> where Self: Sized;
}
/// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence
@@ -138,15 +139,18 @@ pub struct NativeExecutor<D: NativeExecutionDispatch> {
fallback: WasmExecutor,
/// Native runtime version info.
native_version: NativeVersion,
/// The default number of 64KB pages to allocate for Wasm execution.
default_heap_pages: Option<u64>,
}
impl<D: NativeExecutionDispatch> NativeExecutor<D> {
/// Create new instance.
pub fn new() -> Self {
pub fn new(default_heap_pages: Option<u64>) -> Self {
NativeExecutor {
_dummy: Default::default(),
fallback: WasmExecutor::new(),
native_version: D::native_version(),
default_heap_pages,
}
}
}
@@ -157,6 +161,7 @@ impl<D: NativeExecutionDispatch> Clone for NativeExecutor<D> {
_dummy: Default::default(),
fallback: self.fallback.clone(),
native_version: D::native_version(),
default_heap_pages: self.default_heap_pages,
}
}
}
@@ -171,7 +176,8 @@ impl<D: NativeExecutionDispatch> RuntimeInfo for NativeExecutor<D> {
ext: &mut E,
) -> Option<RuntimeVersion> {
RUNTIMES_CACHE.with(|c|
fetch_cached_runtime_version(&self.fallback, &mut c.borrow_mut(), ext).ok()?.1.clone()
fetch_cached_runtime_version(&self.fallback, &mut c.borrow_mut(), ext, self.default_heap_pages)
.ok()?.1.clone()
)
}
}
@@ -194,9 +200,10 @@ impl<D: NativeExecutionDispatch> CodeExecutor<Blake2Hasher> for NativeExecutor<D
) -> (Result<NativeOrEncoded<R>>, bool) {
RUNTIMES_CACHE.with(|c| {
let mut c = c.borrow_mut();
let (module, onchain_version) = match fetch_cached_runtime_version(&self.fallback, &mut c, ext) {
Ok((module, onchain_version)) => (module, onchain_version),
Err(e) => return (Err(e), false),
let (module, onchain_version) = match fetch_cached_runtime_version(
&self.fallback, &mut c, ext, self.default_heap_pages) {
Ok((module, onchain_version)) => (module, onchain_version),
Err(e) => return (Err(e), false),
};
match (
use_native,
@@ -281,8 +288,8 @@ macro_rules! native_executor_instance {
$version()
}
fn new() -> $crate::NativeExecutor<$name> {
$crate::NativeExecutor::new()
fn new(default_heap_pages: Option<u64>) -> $crate::NativeExecutor<$name> {
$crate::NativeExecutor::new(default_heap_pages)
}
}
}