@@ -5,7 +5,7 @@ use std::marker::PhantomData;
55use std:: path:: { Path , PathBuf } ;
66use std:: str:: FromStr ;
77use std:: sync:: Mutex ;
8- use wasmer:: { Engine , Store } ;
8+ use wasmer:: { Module , Store } ;
99
1010use cosmwasm_std:: Checksum ;
1111
@@ -19,7 +19,7 @@ use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryC
1919use crate :: parsed_wasm:: ParsedWasm ;
2020use crate :: size:: Size ;
2121use crate :: static_analysis:: { Entrypoint , ExportInfo , REQUIRED_IBC_EXPORTS } ;
22- use crate :: wasm_backend:: { compile, make_compiling_engine, make_runtime_engine } ;
22+ use crate :: wasm_backend:: { compile, make_compiling_engine} ;
2323
2424const STATE_DIR : & str = "state" ;
2525// Things related to the state of the blockchain.
@@ -91,24 +91,14 @@ pub struct CacheInner {
9191 memory_cache : InMemoryCache ,
9292 fs_cache : FileSystemCache ,
9393 stats : Stats ,
94- /// A single engine to execute all contracts in this cache instance (usually
95- /// this means all contracts in the process).
96- ///
97- /// This engine is headless, i.e. does not contain a Singlepass compiler.
98- /// It only executes modules compiled with other engines.
99- ///
100- /// The engine has one memory limit set which is the same for all contracts
101- /// running with it. If different memory limits would be needed for different
102- /// contracts at some point, we'd need multiple engines. This is because the tunables
103- /// that control the limit are attached to the engine.
104- runtime_engine : Engine ,
10594}
10695
10796pub struct Cache < A : BackendApi , S : Storage , Q : Querier > {
10897 /// Available capabilities are immutable for the lifetime of the cache,
10998 /// i.e. any number of read-only references is allowed to access it concurrently.
11099 available_capabilities : HashSet < String > ,
111100 inner : Mutex < CacheInner > ,
101+ instance_memory_limit : Size ,
112102 // Those two don't store data but only fix type information
113103 type_api : PhantomData < A > ,
114104 type_storage : PhantomData < S > ,
@@ -169,8 +159,8 @@ where
169159 memory_cache : InMemoryCache :: new ( memory_cache_size) ,
170160 fs_cache,
171161 stats : Stats :: default ( ) ,
172- runtime_engine : make_runtime_engine ( Some ( instance_memory_limit) ) ,
173162 } ) ,
163+ instance_memory_limit,
174164 type_storage : PhantomData :: < S > ,
175165 type_api : PhantomData :: < A > ,
176166 type_querier : PhantomData :: < Q > ,
@@ -318,11 +308,12 @@ where
318308 // for a not-so-relevant use case.
319309
320310 // Try to get module from file system cache
321- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
311+ if let Some ( cached_module) = cache
312+ . fs_cache
313+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
314+ {
322315 cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
323- return cache
324- . pinned_memory_cache
325- . store ( checksum, module, module_size) ;
316+ return cache. pinned_memory_cache . store ( checksum, cached_module) ;
326317 }
327318
328319 // Re-compile from original Wasm bytecode
@@ -337,16 +328,16 @@ where
337328 }
338329
339330 // This time we'll hit the file-system cache.
340- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
331+ let Some ( cached_module) = cache
332+ . fs_cache
333+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
341334 else {
342335 return Err ( VmError :: generic_err (
343336 "Can't load module from file system cache after storing it to file system cache (pin)" ,
344337 ) ) ;
345338 } ;
346339
347- cache
348- . pinned_memory_cache
349- . store ( checksum, module, module_size)
340+ cache. pinned_memory_cache . store ( checksum, cached_module)
350341 }
351342
352343 /// Unpins a Module, i.e. removes it from the pinned memory cache.
@@ -370,10 +361,10 @@ where
370361 backend : Backend < A , S , Q > ,
371362 options : InstanceOptions ,
372363 ) -> VmResult < Instance < A , S , Q > > {
373- let ( cached , store) = self . get_module ( checksum) ?;
364+ let ( module , store) = self . get_module ( checksum) ?;
374365 let instance = Instance :: from_module (
375366 store,
376- & cached . module ,
367+ & module,
377368 backend,
378369 options. gas_limit ,
379370 None ,
@@ -385,36 +376,49 @@ where
385376 /// Returns a module tied to a previously saved Wasm.
386377 /// Depending on availability, this is either generated from a memory cache, file system cache or Wasm code.
387378 /// This is part of `get_instance` but pulled out to reduce the locking time.
388- fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( CachedModule , Store ) > {
379+ fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( Module , Store ) > {
389380 let mut cache = self . inner . lock ( ) . unwrap ( ) ;
390381 // Try to get module from the pinned memory cache
391382 if let Some ( element) = cache. pinned_memory_cache . load ( checksum) ? {
392383 cache. stats . hits_pinned_memory_cache =
393384 cache. stats . hits_pinned_memory_cache . saturating_add ( 1 ) ;
394- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
395- return Ok ( ( element, store) ) ;
385+ let CachedModule {
386+ module,
387+ engine,
388+ size_estimate : _,
389+ } = element;
390+ let store = Store :: new ( engine) ;
391+ return Ok ( ( module, store) ) ;
396392 }
397393
398394 // Get module from memory cache
399395 if let Some ( element) = cache. memory_cache . load ( checksum) ? {
400396 cache. stats . hits_memory_cache = cache. stats . hits_memory_cache . saturating_add ( 1 ) ;
401- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
402- return Ok ( ( element, store) ) ;
397+ let CachedModule {
398+ module,
399+ engine,
400+ size_estimate : _,
401+ } = element;
402+ let store = Store :: new ( engine) ;
403+ return Ok ( ( module, store) ) ;
403404 }
404405
405406 // Get module from file system cache
406- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
407+ if let Some ( cached_module) = cache
408+ . fs_cache
409+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
410+ {
407411 cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
408412
409- cache
410- . memory_cache
411- . store ( checksum, module. clone ( ) , module_size) ?;
412- let cached = CachedModule {
413+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
414+
415+ let CachedModule {
413416 module,
414- size_estimate : module_size,
415- } ;
416- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
417- return Ok ( ( cached, store) ) ;
417+ engine,
418+ size_estimate : _,
419+ } = cached_module;
420+ let store = Store :: new ( engine) ;
421+ return Ok ( ( module, store) ) ;
418422 }
419423
420424 // Re-compile module from wasm
@@ -433,21 +437,23 @@ where
433437 }
434438
435439 // This time we'll hit the file-system cache.
436- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
440+ let Some ( cached_module) = cache
441+ . fs_cache
442+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
437443 else {
438444 return Err ( VmError :: generic_err (
439445 "Can't load module from file system cache after storing it to file system cache (get_module)" ,
440446 ) ) ;
441447 } ;
442- cache
443- . memory_cache
444- . store ( checksum, module. clone ( ) , module_size) ?;
445- let cached = CachedModule {
448+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
449+
450+ let CachedModule {
446451 module,
447- size_estimate : module_size,
448- } ;
449- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
450- Ok ( ( cached, store) )
452+ engine,
453+ size_estimate : _,
454+ } = cached_module;
455+ let store = Store :: new ( engine) ;
456+ Ok ( ( module, store) )
451457 }
452458}
453459
0 commit comments