Skip to content

Commit

Permalink
feat: enable in-memory cache for wasmer1
Browse files Browse the repository at this point in the history
This PR ports wasmer0 implementation of in-memory cache to wasme1 as
well.

The goal here is to keep the behavior between two wasmers as close as
possible. We absolutely do want to get rid of the in-memory cache, but
it's safer not to do that while also doing a major wasmer upgrade!

See near#4076 (comment)
for context
  • Loading branch information
matklad committed May 13, 2021
1 parent 473e71b commit 28cb68f
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions runtime/near-vm-runner/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl fmt::Debug for MockCompiledContractCache {
}
}

#[cfg(not(feature = "no_cache"))]
const CACHE_SIZE: usize = 128;

#[cfg(feature = "wasmer0_vm")]
pub mod wasmer0_cache {
use super::*;
Expand Down Expand Up @@ -177,9 +180,6 @@ pub mod wasmer0_cache {
}
}

#[cfg(not(feature = "no_cache"))]
const CACHE_SIZE: usize = 128;

#[cfg(not(feature = "no_cache"))]
cached_key! {
MODULES: SizedCache<CryptoHash, Result<wasmer_runtime::Module, VMError>>
Expand Down Expand Up @@ -215,15 +215,6 @@ pub mod wasmer1_cache {
use near_primitives::contract::ContractCode;

use super::*;
pub(crate) fn compile_module_cached_wasmer1(
code: &ContractCode,
config: &VMConfig,
cache: Option<&dyn CompiledContractCache>,
store: &wasmer::Store,
) -> Result<wasmer::Module, VMError> {
let key = get_key(code, VMKind::Wasmer1, config);
return compile_module_cached_wasmer1_impl(key, &code.code, config, cache, store);
}

fn compile_module_wasmer1(
code: &[u8],
Expand Down Expand Up @@ -291,6 +282,37 @@ pub mod wasmer1_cache {
Err(_) => Err(VMError::CacheError(ReadError)),
}
}

#[cfg(not(feature = "no_cache"))]
cached_key! {
MODULES: SizedCache<CryptoHash, Result<wasmer::Module, VMError>>
= SizedCache::with_size(CACHE_SIZE);
Key = {
key
};

fn memcache_compile_module_cached_wasmer1(
key: CryptoHash,
wasm_code: &[u8],
config: &VMConfig,
cache: Option<&dyn CompiledContractCache>,
store: &wasmer::Store) -> Result<wasmer::Module, VMError> = {
compile_module_cached_wasmer1_impl(key, wasm_code, config, cache, store)
}
}

pub(crate) fn compile_module_cached_wasmer1(
code: &ContractCode,
config: &VMConfig,
cache: Option<&dyn CompiledContractCache>,
store: &wasmer::Store,
) -> Result<wasmer::Module, VMError> {
let key = get_key(code, VMKind::Wasmer1, config);
#[cfg(not(feature = "no_cache"))]
return memcache_compile_module_cached_wasmer1(key, &code.code, config, cache, store);
#[cfg(feature = "no_cache")]
return compile_module_cached_wasmer1_impl(key, &code.code, config, cache, store);
}
}

pub(crate) fn precompile_contract_impl(
Expand Down

0 comments on commit 28cb68f

Please sign in to comment.