From 99b17245527ae551617c95f6608ae91820b3d4f5 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 8 Jan 2021 10:59:53 +0100 Subject: [PATCH] Integrate PinnedMemoryCache to Cache --- packages/vm/src/cache.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 7a7b13276e..8f86f227dc 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -9,7 +9,7 @@ use crate::checksum::Checksum; use crate::compatibility::check_wasm; use crate::errors::{VmError, VmResult}; use crate::instance::{Instance, InstanceOptions}; -use crate::modules::{FileSystemCache, InMemoryCache}; +use crate::modules::{FileSystemCache, InMemoryCache, PinnedMemoryCache}; use crate::size::Size; use crate::wasm_backend::{compile_and_use, compile_only, make_runtime_store}; @@ -18,6 +18,7 @@ const MODULES_DIR: &str = "modules"; #[derive(Debug, Default, Clone, Copy)] pub struct Stats { + pub hits_pinned_memory_cache: u32, pub hits_memory_cache: u32, pub hits_fs_cache: u32, pub misses: u32, @@ -33,6 +34,7 @@ pub struct CacheOptions { pub struct Cache { wasm_path: PathBuf, supported_features: HashSet, + pinned_memory_cache: PinnedMemoryCache, memory_cache: InMemoryCache, fs_cache: FileSystemCache, stats: Stats, @@ -72,6 +74,7 @@ where Ok(Cache { wasm_path, supported_features, + pinned_memory_cache: PinnedMemoryCache::new(), memory_cache: InMemoryCache::new(memory_cache_size), fs_cache, stats: Stats::default(), @@ -117,6 +120,14 @@ where options: InstanceOptions, ) -> VmResult> { let store = make_runtime_store(options.memory_limit); + // Try to get module from the pinned memory cache + if let Some(module) = self.pinned_memory_cache.load(checksum, &store)? { + self.stats.hits_pinned_memory_cache += 1; + let instance = + Instance::from_module(&module, backend, options.gas_limit, options.print_debug)?; + return Ok(instance); + } + // Get module from memory cache if let Some(module) = self.memory_cache.load(checksum, &store)? { self.stats.hits_memory_cache += 1;