Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Make CacheOptions non-exhaustive #1898

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ and this project adheres to
([#1890])
- cosmwasm-std: `Uint{64,128}::full_mul` now take `Into<Self>` as an argument.
([#1874])
- cosmwasm-vm: Make `CacheOptions` non-exhaustive and add a constructor.
([#1898])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879
[#1890]: https://github.com/CosmWasm/cosmwasm/pull/1890
[#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898

## [1.5.0-rc.0]

Expand Down
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
Loading