Skip to content

Commit

Permalink
Make CacheOptions non-exhaustive
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Oct 18, 2023
1 parent b1c031a commit 3011a8d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
50 changes: 25 additions & 25 deletions packages/vm/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MockApi, MockStorage, MockQuerier> =
Expand All @@ -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<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
cache.set_module_unchecked(true);
Expand All @@ -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<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
let checksum = cache.save_wasm(CONTRACT).unwrap();
Expand All @@ -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<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
cache.set_module_unchecked(true);
Expand Down Expand Up @@ -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<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
Expand Down
12 changes: 6 additions & 6 deletions packages/vm/examples/multi_threaded_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MockApi, MockStorage, MockQuerier> = unsafe { Cache::new(options).unwrap() };
let cache = Arc::new(cache);
Expand Down
17 changes: 17 additions & 0 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct Metrics {
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct CacheOptions {
/// The base directory of this cache.
///
Expand All @@ -65,6 +66,22 @@ pub struct CacheOptions {
pub instance_memory_limit: Size,
}

impl CacheOptions {
pub fn new(
base_dir: impl Into<PathBuf>,
available_capabilities: impl Into<HashSet<String>>,
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,
Expand Down

0 comments on commit 3011a8d

Please sign in to comment.