@@ -4,7 +4,7 @@ use std::io::{Read, Write};
4
4
use std:: marker:: PhantomData ;
5
5
use std:: path:: { Path , PathBuf } ;
6
6
use std:: sync:: Mutex ;
7
- use wasmer:: { Engine , Store } ;
7
+ use wasmer:: { Module , Store } ;
8
8
9
9
use crate :: backend:: { Backend , BackendApi , Querier , Storage } ;
10
10
use crate :: capabilities:: required_capabilities_from_module;
@@ -16,8 +16,13 @@ use crate::instance::{Instance, InstanceOptions};
16
16
use crate :: modules:: { CachedModule , FileSystemCache , InMemoryCache , PinnedMemoryCache } ;
17
17
use crate :: parsed_wasm:: ParsedWasm ;
18
18
use crate :: size:: Size ;
19
+ <<<<<<< HEAD
19
20
use crate :: static_analysis:: has_ibc_entry_points ;
20
21
use crate :: wasm_backend:: { compile , make_compiling_engine , make_runtime_engine } ;
22
+ =======
23
+ use crate :: static_analysis:: { Entrypoint , ExportInfo , REQUIRED_IBC_EXPORTS } ;
24
+ use crate :: wasm_backend:: { compile , make_compiling_engine } ;
25
+ >>>>>>> a34446891 ( Store engine together with Module to fix memory increase issue)
21
26
22
27
const STATE_DIR : & str = "state";
23
28
// Things related to the state of the blockchain.
@@ -88,24 +93,14 @@ pub struct CacheInner {
88
93
memory_cache : InMemoryCache ,
89
94
fs_cache : FileSystemCache ,
90
95
stats : Stats ,
91
- /// A single engine to execute all contracts in this cache instance (usually
92
- /// this means all contracts in the process).
93
- ///
94
- /// This engine is headless, i.e. does not contain a Singlepass compiler.
95
- /// It only executes modules compiled with other engines.
96
- ///
97
- /// The engine has one memory limit set which is the same for all contracts
98
- /// running with it. If different memory limits would be needed for different
99
- /// contracts at some point, we'd need multiple engines. This is because the tunables
100
- /// that control the limit are attached to the engine.
101
- runtime_engine : Engine ,
102
96
}
103
97
104
98
pub struct Cache < A : BackendApi , S : Storage , Q : Querier > {
105
99
/// Available capabilities are immutable for the lifetime of the cache,
106
100
/// i.e. any number of read-only references is allowed to access it concurrently.
107
101
available_capabilities : HashSet < String > ,
108
102
inner : Mutex < CacheInner > ,
103
+ instance_memory_limit : Size ,
109
104
// Those two don't store data but only fix type information
110
105
type_api : PhantomData < A > ,
111
106
type_storage : PhantomData < S > ,
@@ -161,8 +156,8 @@ where
161
156
memory_cache : InMemoryCache :: new ( memory_cache_size) ,
162
157
fs_cache,
163
158
stats : Stats :: default ( ) ,
164
- runtime_engine : make_runtime_engine ( Some ( instance_memory_limit) ) ,
165
159
} ) ,
160
+ instance_memory_limit,
166
161
type_storage : PhantomData :: < S > ,
167
162
type_api : PhantomData :: < A > ,
168
163
type_querier : PhantomData :: < Q > ,
@@ -298,11 +293,12 @@ where
298
293
// for a not-so-relevant use case.
299
294
300
295
// Try to get module from file system cache
301
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
296
+ if let Some ( cached_module) = cache
297
+ . fs_cache
298
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
299
+ {
302
300
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
303
- return cache
304
- . pinned_memory_cache
305
- . store ( checksum, module, module_size) ;
301
+ return cache. pinned_memory_cache . store ( checksum, cached_module) ;
306
302
}
307
303
308
304
// Re-compile from original Wasm bytecode
@@ -317,16 +313,16 @@ where
317
313
}
318
314
319
315
// This time we'll hit the file-system cache.
320
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
316
+ let Some ( cached_module) = cache
317
+ . fs_cache
318
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
321
319
else {
322
320
return Err ( VmError :: generic_err (
323
321
"Can't load module from file system cache after storing it to file system cache (pin)" ,
324
322
) ) ;
325
323
} ;
326
324
327
- cache
328
- . pinned_memory_cache
329
- . store ( checksum, module, module_size)
325
+ cache. pinned_memory_cache . store ( checksum, cached_module)
330
326
}
331
327
332
328
/// Unpins a Module, i.e. removes it from the pinned memory cache.
@@ -350,10 +346,10 @@ where
350
346
backend : Backend < A , S , Q > ,
351
347
options : InstanceOptions ,
352
348
) -> VmResult < Instance < A , S , Q > > {
353
- let ( cached , store) = self . get_module ( checksum) ?;
349
+ let ( module , store) = self . get_module ( checksum) ?;
354
350
let instance = Instance :: from_module (
355
351
store,
356
- & cached . module ,
352
+ & module,
357
353
backend,
358
354
options. gas_limit ,
359
355
options. print_debug ,
@@ -366,36 +362,49 @@ where
366
362
/// Returns a module tied to a previously saved Wasm.
367
363
/// Depending on availability, this is either generated from a memory cache, file system cache or Wasm code.
368
364
/// This is part of `get_instance` but pulled out to reduce the locking time.
369
- fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( CachedModule , Store ) > {
365
+ fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( Module , Store ) > {
370
366
let mut cache = self . inner . lock ( ) . unwrap ( ) ;
371
367
// Try to get module from the pinned memory cache
372
368
if let Some ( element) = cache. pinned_memory_cache . load ( checksum) ? {
373
369
cache. stats . hits_pinned_memory_cache =
374
370
cache. stats . hits_pinned_memory_cache . saturating_add ( 1 ) ;
375
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
376
- return Ok ( ( element, store) ) ;
371
+ let CachedModule {
372
+ module,
373
+ engine,
374
+ size_estimate : _,
375
+ } = element;
376
+ let store = Store :: new ( engine) ;
377
+ return Ok ( ( module, store) ) ;
377
378
}
378
379
379
380
// Get module from memory cache
380
381
if let Some ( element) = cache. memory_cache . load ( checksum) ? {
381
382
cache. stats . hits_memory_cache = cache. stats . hits_memory_cache . saturating_add ( 1 ) ;
382
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
383
- return Ok ( ( element, store) ) ;
383
+ let CachedModule {
384
+ module,
385
+ engine,
386
+ size_estimate : _,
387
+ } = element;
388
+ let store = Store :: new ( engine) ;
389
+ return Ok ( ( module, store) ) ;
384
390
}
385
391
386
392
// Get module from file system cache
387
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
393
+ if let Some ( cached_module) = cache
394
+ . fs_cache
395
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
396
+ {
388
397
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
389
398
390
- cache
391
- . memory_cache
392
- . store ( checksum, module. clone ( ) , module_size) ?;
393
- let cached = CachedModule {
399
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
400
+
401
+ let CachedModule {
394
402
module,
395
- size_estimate : module_size,
396
- } ;
397
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
398
- return Ok ( ( cached, store) ) ;
403
+ engine,
404
+ size_estimate : _,
405
+ } = cached_module;
406
+ let store = Store :: new ( engine) ;
407
+ return Ok ( ( module, store) ) ;
399
408
}
400
409
401
410
// Re-compile module from wasm
@@ -414,21 +423,23 @@ where
414
423
}
415
424
416
425
// This time we'll hit the file-system cache.
417
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
426
+ let Some ( cached_module) = cache
427
+ . fs_cache
428
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
418
429
else {
419
430
return Err ( VmError :: generic_err (
420
431
"Can't load module from file system cache after storing it to file system cache (get_module)" ,
421
432
) ) ;
422
433
} ;
423
- cache
424
- . memory_cache
425
- . store ( checksum, module. clone ( ) , module_size) ?;
426
- let cached = CachedModule {
434
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
435
+
436
+ let CachedModule {
427
437
module,
428
- size_estimate : module_size,
429
- } ;
430
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
431
- Ok ( ( cached, store) )
438
+ engine,
439
+ size_estimate : _,
440
+ } = cached_module;
441
+ let store = Store :: new ( engine) ;
442
+ Ok ( ( module, store) )
432
443
}
433
444
}
434
445
0 commit comments