Skip to content

Commit

Permalink
Add fn to use unchecked loading from fs cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Jun 1, 2023
1 parent e66c583 commit 1899888
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/vm/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ fn bench_cache(c: &mut Criterion) {
});
});

group.bench_function("load wasm unchecked", |b| {
let options = CacheOptions { ..options.clone() };
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
cache.set_module_unchecked(true);
let checksum = cache.save_wasm(CONTRACT).unwrap();

b.iter(|| {
let result = cache.load_wasm(&checksum);
assert!(result.is_ok());
});
});

group.bench_function("analyze", |b| {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
Expand Down Expand Up @@ -182,6 +195,29 @@ fn bench_cache(c: &mut Criterion) {
});
});

group.bench_function("instantiate from fs unchecked", |b| {
let non_memcache = CacheOptions {
base_dir: TempDir::new().unwrap().into_path(),
available_capabilities: capabilities_from_csv("iterator,staking"),
memory_cache_size: Size(0),
instance_memory_limit: DEFAULT_MEMORY_LIMIT,
};
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
cache.set_module_unchecked(true);
let checksum = cache.save_wasm(CONTRACT).unwrap();

b.iter(|| {
let _ = cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_fs_cache >= 1);
assert_eq!(cache.stats().misses, 0);
});
});

group.bench_function("instantiate from memory", |b| {
let checksum = Checksum::generate(CONTRACT);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
Expand Down
10 changes: 10 additions & 0 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ where
})
}

/// If `unchecked` is true, the filesystem cache will use the `*_unchecked` wasmer functions for
/// loading modules from disk.
pub fn set_module_unchecked(&mut self, unchecked: bool) {
self.inner
.lock()
.unwrap()
.fs_cache
.set_module_unchecked(unchecked);
}

pub fn stats(&self) -> Stats {
self.inner.lock().unwrap().stats
}
Expand Down
6 changes: 6 additions & 0 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ impl FileSystemCache {
})
}

/// If `unchecked` is true, the cache will use the `*_unchecked` wasmer functions for
/// loading modules from disk.
pub fn set_module_unchecked(&mut self, unchecked: bool) {
self.unchecked_modules = unchecked;
}

/// Loads a serialized module from the file system and returns a module (i.e. artifact + store),
/// along with the size of the serialized module.
pub fn load(
Expand Down

0 comments on commit 1899888

Please sign in to comment.