Skip to content

Commit 36670c1

Browse files
committed
Explain the use of Option<...> in InMemoryCache and avoid unwrap
1 parent e940753 commit 36670c1

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/vm/src/modules/in_memory_cache.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ impl WeightScale<Checksum, CachedModule> for SizeScale {
2828

2929
/// An in-memory module cache
3030
pub struct InMemoryCache {
31+
/// This is where the cached data is stored.
32+
///
33+
/// `Some` means the cache is active (i.e. size > 0) and
34+
/// `None` means it is inactive (size is 0). This is only needed because
35+
/// the currently used `CLruCache` does not support zero capacity construction.
36+
/// It can likely be simplified if the underlying implementation supports zero.
3137
modules: Option<CLruCache<Checksum, CachedModule, RandomState, SizeScale>>,
3238
}
3339

@@ -37,16 +43,15 @@ impl InMemoryCache {
3743
pub fn new(size: Size) -> Self {
3844
let preallocated_entries = size.0 / MINIMUM_MODULE_SIZE.0;
3945

46+
let size = NonZeroUsize::new(size.0);
4047
InMemoryCache {
41-
modules: if size.0 > 0 {
42-
Some(CLruCache::with_config(
43-
CLruCacheConfig::new(NonZeroUsize::new(size.0).unwrap())
48+
modules: size.map(|non_zero_size| {
49+
CLruCache::with_config(
50+
CLruCacheConfig::new(non_zero_size)
4451
.with_memory(preallocated_entries)
4552
.with_scale(SizeScale),
46-
))
47-
} else {
48-
None
49-
},
53+
)
54+
}),
5055
}
5156
}
5257

0 commit comments

Comments
 (0)