diff --git a/packages/vm/benches/main.rs b/packages/vm/benches/main.rs index afefe5d3cb..bcad71287b 100644 --- a/packages/vm/benches/main.rs +++ b/packages/vm/benches/main.rs @@ -121,12 +121,12 @@ fn bench_instance(c: &mut Criterion) { fn bench_cache(c: &mut Criterion) { let mut group = c.benchmark_group("Cache"); - let options = CacheOptions { - base_dir: TempDir::new().unwrap().into_path(), - available_capabilities: capabilities_from_csv("iterator,staking"), - memory_cache_size: MEMORY_CACHE_SIZE, - instance_memory_limit: DEFAULT_MEMORY_LIMIT, - }; + let options = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + MEMORY_CACHE_SIZE, + DEFAULT_MEMORY_LIMIT, + ); group.bench_function("save wasm", |b| { let cache: Cache = @@ -150,7 +150,7 @@ fn bench_cache(c: &mut Criterion) { }); group.bench_function("load wasm unchecked", |b| { - let options = CacheOptions { ..options.clone() }; + let options = options.clone(); let mut cache: Cache = unsafe { Cache::new(options).unwrap() }; cache.set_module_unchecked(true); @@ -174,12 +174,12 @@ fn bench_cache(c: &mut Criterion) { }); group.bench_function("instantiate from fs", |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 non_memcache = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + Size(0), + DEFAULT_MEMORY_LIMIT, + ); let cache: Cache = unsafe { Cache::new(non_memcache).unwrap() }; let checksum = cache.save_wasm(CONTRACT).unwrap(); @@ -196,12 +196,12 @@ 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 non_memcache = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + Size(0), + DEFAULT_MEMORY_LIMIT, + ); let mut cache: Cache = unsafe { Cache::new(non_memcache).unwrap() }; cache.set_module_unchecked(true); @@ -263,12 +263,12 @@ fn bench_cache(c: &mut Criterion) { pub fn bench_instance_threads(c: &mut Criterion) { c.bench_function("multi-threaded get_instance", |b| { - let options = CacheOptions { - base_dir: TempDir::new().unwrap().into_path(), - available_capabilities: capabilities_from_csv("iterator,staking"), - memory_cache_size: MEMORY_CACHE_SIZE, - instance_memory_limit: DEFAULT_MEMORY_LIMIT, - }; + let options = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + MEMORY_CACHE_SIZE, + DEFAULT_MEMORY_LIMIT, + ); let cache: Cache = unsafe { Cache::new(options).unwrap() }; diff --git a/packages/vm/examples/multi_threaded_cache.rs b/packages/vm/examples/multi_threaded_cache.rs index fd8c145f25..0b42e0dd9c 100644 --- a/packages/vm/examples/multi_threaded_cache.rs +++ b/packages/vm/examples/multi_threaded_cache.rs @@ -26,12 +26,12 @@ const INSTANTIATION_THREADS: usize = 2048; const THREADS: usize = SAVE_WASM_THREADS + INSTANTIATION_THREADS; pub fn main() { - let options = CacheOptions { - base_dir: TempDir::new().unwrap().into_path(), - available_capabilities: capabilities_from_csv("iterator,staking"), - memory_cache_size: MEMORY_CACHE_SIZE, - instance_memory_limit: DEFAULT_MEMORY_LIMIT, - }; + let options = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + MEMORY_CACHE_SIZE, + DEFAULT_MEMORY_LIMIT, + ); let cache: Cache = unsafe { Cache::new(options).unwrap() }; let cache = Arc::new(cache); diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 7d853affa0..207c00e717 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -52,6 +52,7 @@ pub struct Metrics { } #[derive(Clone, Debug)] +#[non_exhaustive] pub struct CacheOptions { /// The base directory of this cache. /// @@ -65,6 +66,22 @@ pub struct CacheOptions { pub instance_memory_limit: Size, } +impl CacheOptions { + pub fn new( + base_dir: impl Into, + available_capabilities: impl Into>, + memory_cache_size: Size, + instance_memory_limit: Size, + ) -> Self { + Self { + base_dir: base_dir.into(), + available_capabilities: available_capabilities.into(), + memory_cache_size, + instance_memory_limit, + } + } +} + pub struct CacheInner { /// The directory in which the Wasm blobs are stored in the file system. wasm_path: PathBuf,