Skip to content

Commit

Permalink
remove module store
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Oct 16, 2023
1 parent 2fdce5d commit c47fcde
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 131 deletions.
70 changes: 0 additions & 70 deletions crates/host/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,76 +204,6 @@ impl SerializedModuleCache {
}
}

/// Caches wasmer modules that can be used to build wasmer instances. This is the
/// output of building from wasm or deserializing the items in the serialized module cache.
#[derive(Default)]
pub struct ModuleCache {
plru: MicroCache,
key_map: PlruKeyMap,
cache: BTreeMap<CacheKey, Arc<ModuleWithStore>>,
}

pub static MODULE_CACHE: Lazy<RwLock<ModuleCache>> =
Lazy::new(|| RwLock::new(ModuleCache::default()));

impl ModuleCache {
/// Wraps the serialized module cache to build modules as needed and also cache
/// the module itself in the module cache.
fn get_with_build_cache(
&mut self,
key: CacheKey,
wasm: &[u8],
) -> Result<Arc<ModuleWithStore>, wasmer::RuntimeError> {
let module_with_store = match SERIALIZED_MODULE_CACHE.get() {
Some(serialized_module_cache) => serialized_module_cache.write().get(key, wasm)?,
None => {
return Err(wasmer::RuntimeError::user(Box::new(wasm_error!(
WasmErrorInner::UninitializedSerializedModuleCache
))))
}
};
Ok(self.put_item(key, module_with_store))
}

/// Attempts to retrieve a module ready to build instances from. Builds a new
/// module from the provided wasm and caches both the module and a serialized
/// copy of the module if there is a miss.
pub fn get(
&mut self,
key: CacheKey,
wasm: &[u8],
) -> Result<Arc<ModuleWithStore>, wasmer::RuntimeError> {
match self.get_item(&key) {
Some(item) => Ok(item),
None => self.get_with_build_cache(key, wasm),
}
}
}

impl PlruCache for ModuleCache {
type Item = ModuleWithStore;

fn plru_mut(&mut self) -> &mut MicroCache {
&mut self.plru
}

fn key_map_mut(&mut self) -> &mut PlruKeyMap {
&mut self.key_map
}

fn key_map(&self) -> &PlruKeyMap {
&self.key_map
}

fn cache(&self) -> &BTreeMap<CacheKey, Arc<Self::Item>> {
&self.cache
}

fn cache_mut(&mut self) -> &mut BTreeMap<CacheKey, Arc<Self::Item>> {
&mut self.cache
}
}

/// Caches wasm instances. Reusing wasm instances allows maximum speed in function
/// calls but also introduces the possibility of memory corruption or other bad
/// state that is inappropriate to persist/reuse/access across calls. It is the
Expand Down
1 change: 0 additions & 1 deletion crates/host/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub use crate::env::Env;
pub use crate::guest;
pub use crate::module::MODULE_CACHE;
pub use holochain_serialized_bytes::prelude::*;
pub use holochain_wasmer_common::result::WasmError;
pub use holochain_wasmer_common::*;
Expand Down
65 changes: 5 additions & 60 deletions test/src/wasms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use wasmer::Cranelift;
use wasmer::FunctionEnv;
use wasmer::Imports;
use wasmer::Instance;
use wasmer_middlewares::Metering;
use wasmer::Store;
use wasmer::Module;

use wasmer::Store;
use wasmer_middlewares::Metering;

use std::sync::atomic::{AtomicUsize, Ordering};
static INSTANCE_COUNTER: AtomicUsize = AtomicUsize::new(0);
Expand Down Expand Up @@ -80,11 +79,9 @@ impl TestWasm {
})
}

pub fn serial_cache_module(&self, metered: bool) -> Arc<ModuleWithStore> {
pub fn module(&self, metered: bool) -> Arc<ModuleWithStore> {
match SERIALIZED_MODULE_CACHE.get() {
Some(cache) => {
cache.write().get(self.key(metered), self.bytes()).unwrap()
},
Some(cache) => cache.write().get(self.key(metered), self.bytes()).unwrap(),
None => {
let cranelift_fn = || {
let cost_function = |_operator: &Operator| -> u64 { 1 };
Expand Down Expand Up @@ -119,61 +116,9 @@ impl TestWasm {
}
}

pub fn module(&self, metered: bool) -> Arc<ModuleWithStore> {
match MODULE_CACHE.write().get(self.key(metered), self.bytes()) {
Ok(v) => {
// println!("using cached module for {}", self.name());
v
},
Err(runtime_error) => match runtime_error.downcast::<WasmError>() {
Ok(WasmError {
error: WasmErrorInner::UninitializedSerializedModuleCache,
..
}) => {
{
let cranelift_fn = || {
let cost_function = |_operator: &Operator| -> u64 { 1 };
let metering = Arc::new(Metering::new(10000000000, cost_function));
let mut cranelift = Cranelift::default();
cranelift.canonicalize_nans(true).push_middleware(metering);
cranelift
};

let cranelift_fn_unmetered = || {
let mut cranelift = Cranelift::default();
cranelift.canonicalize_nans(true);
cranelift
};

assert!(SERIALIZED_MODULE_CACHE
.set(parking_lot::RwLock::new(
SerializedModuleCache::default_with_cranelift(if metered {
cranelift_fn
} else {
cranelift_fn_unmetered
})
))
.is_ok());
}
SERIALIZED_MODULE_CACHE
.get()
.unwrap()
.write()
.get(self.key(metered), self.bytes())
.unwrap()
}

_ => unreachable!(),
},
}
}

pub fn _instance(&self, metered: bool) -> InstanceWithStore {
let instance_count = INSTANCE_COUNTER.fetch_add(1, Ordering::SeqCst);
// println!("creating instance for {} {}", self.name(), instance_count);
// let module_with_store = self.module(metered);
// let module_with_store = self.uncached_module();
let module_with_store = self.serial_cache_module(metered);
let module_with_store = self.module(metered);
let function_env;
let instance;
{
Expand Down

0 comments on commit c47fcde

Please sign in to comment.