Skip to content

Commit

Permalink
Fix wasmer build
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Nov 21, 2022
1 parent 9041616 commit 40a2f3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
24 changes: 12 additions & 12 deletions radix-engine/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::model::InvokeError;
use moka::sync::Cache;
use radix_engine_interface::data::IndexedScryptoValue;
use sbor::rust::sync::{Arc, Mutex};
use wasmer::{
Expand All @@ -17,6 +16,7 @@ use super::InstrumentedCode;

pub struct WasmerModule {
module: Module,
#[allow(dead_code)]
code_size_bytes: usize,
}

Expand All @@ -36,7 +36,7 @@ pub struct WasmerInstanceEnv {

pub struct WasmerEngine {
store: Store,
modules_cache: Cache<Hash, Arc<WasmerModule>>,
modules_cache: RefCell<HashMap<Hash, Arc<WasmerModule>>>,
}

pub fn send_value(instance: &Instance, value: &[u8]) -> Result<usize, InvokeError<WasmError>> {
Expand Down Expand Up @@ -221,6 +221,7 @@ impl WasmInstance for WasmerInstance {

#[derive(Debug, Clone)]
pub struct EngineOptions {
#[allow(dead_code)]
max_cache_size_bytes: u64,
}

Expand All @@ -233,15 +234,10 @@ impl Default for WasmerEngine {
}

impl WasmerEngine {
#[allow(unused_variables)]
pub fn new(options: EngineOptions) -> Self {
let compiler = Singlepass::new();
let modules_cache = Cache::builder()
.weigher(|_key: &Hash, value: &Arc<WasmerModule>| -> u32 {
// Approximate the module entry size by the code size
value.code_size_bytes.try_into().unwrap_or(u32::MAX)
})
.max_capacity(options.max_cache_size_bytes)
.build();
let modules_cache = RefCell::new(HashMap::new());
Self {
store: Store::new(&Universal::new(compiler).engine()),
modules_cache,
Expand All @@ -254,8 +250,10 @@ impl WasmEngine for WasmerEngine {

fn instantiate(&self, instrumented_code: &InstrumentedCode) -> WasmerInstance {
let code_hash = &instrumented_code.code_hash;
if let Some(cached_module) = self.modules_cache.get(code_hash) {
return cached_module.instantiate();
{
if let Some(cached_module) = self.modules_cache.borrow().get(code_hash) {
return cached_module.instantiate();
}
}

let code = instrumented_code.code.as_ref();
Expand All @@ -265,7 +263,9 @@ impl WasmEngine for WasmerEngine {
code_size_bytes: code.len(),
});

self.modules_cache.insert(*code_hash, new_module.clone());
self.modules_cache
.borrow_mut()
.insert(*code_hash, new_module.clone());

new_module.instantiate()
}
Expand Down
3 changes: 2 additions & 1 deletion radix-engine/src/wasm/wasmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ impl Default for WasmiEngine {
}

impl WasmiEngine {
pub fn new(_options: EngineOptions) -> Self {
#[allow(unused_variables)]
pub fn new(options: EngineOptions) -> Self {
// TODO: limit size
let cache = RefCell::new(HashMap::new());
Self {
Expand Down

0 comments on commit 40a2f3f

Please sign in to comment.