Skip to content
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
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ kube = "0.88.1"
linera-kywasmtime = "0.1.0"
linked-hash-map = "0.5.6"
log = "0.4.21"
lru = "0.12.3"
lru = "0.15.0"
mini-moka = "0.10.3"
nonzero_lit = "0.1.2"
num-bigint = "0.4.3"
Expand Down
32 changes: 8 additions & 24 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions linera-execution/src/wasm/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ impl<Module> Default for ModuleCache<Module> {
}
}

impl<Module> ModuleCache<Module> {
#[cfg(test)]
fn with_max_size(max_size: u64) -> Self {
ModuleCache {
modules: LruCache::unbounded(),
total_size: 0,
max_size,
}
}
}

impl<Module: Clone> ModuleCache<Module> {
/// Returns a `Module` for the requested `bytecode`, creating it with `module_builder` and
/// adding it to the cache if it doesn't already exist in the cache.
Expand All @@ -58,11 +69,20 @@ impl<Module: Clone> ModuleCache<Module> {
pub fn insert(&mut self, bytecode: Bytecode, module: Module) {
let bytecode_size = bytecode.as_ref().len() as u64;

if bytecode_size > self.max_size {
return;
}

if self.modules.promote(&bytecode) {
return;
}

if self.total_size + bytecode_size > self.max_size {
self.reduce_size_to(self.max_size - bytecode_size);
}

self.modules.put(bytecode, module);
self.total_size += bytecode_size;
}

/// Evicts entries from the cache so that the total size of cached bytecode files is less than
Expand All @@ -79,3 +99,71 @@ impl<Module: Clone> ModuleCache<Module> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn bytecode(size: usize) -> Bytecode {
Bytecode::new(vec![0u8; size])
}

fn distinct_bytecode(size: usize, discriminant: u8) -> Bytecode {
let mut bytes = vec![0u8; size];
bytes[0] = discriminant;
Bytecode::new(bytes)
}

#[test]
fn total_size_tracks_insertions() {
let mut cache = ModuleCache::<u32>::with_max_size(1000);
cache.insert(bytecode(100), 1);
assert_eq!(cache.total_size, 100);
cache.insert(distinct_bytecode(200, 1), 2);
assert_eq!(cache.total_size, 300);
}

#[test]
fn eviction_triggers_when_full() {
let mut cache = ModuleCache::<u32>::with_max_size(250);
cache.insert(bytecode(100), 1);
cache.insert(distinct_bytecode(100, 1), 2);
assert_eq!(cache.total_size, 200);
assert_eq!(cache.modules.len(), 2);

cache.insert(distinct_bytecode(100, 2), 3);
assert_eq!(cache.modules.len(), 2);
assert!(cache.total_size <= 250);
}

#[test]
fn oversized_bytecode_is_rejected() {
let mut cache = ModuleCache::<u32>::with_max_size(50);
cache.insert(bytecode(100), 1);
assert_eq!(cache.total_size, 0);
assert_eq!(cache.modules.len(), 0);
}

#[test]
fn reinserting_same_key_does_not_double_count() {
let mut cache = ModuleCache::<u32>::with_max_size(1000);
let bc = bytecode(100);
cache.insert(bc.clone(), 1);
assert_eq!(cache.total_size, 100);
cache.insert(bc, 2);
assert_eq!(cache.total_size, 100);
assert_eq!(cache.modules.len(), 1);
}

#[test]
fn reinserting_existing_key_does_not_evict() {
let mut cache = ModuleCache::<u32>::with_max_size(200);
cache.insert(bytecode(100), 1);
cache.insert(distinct_bytecode(100, 1), 2);
assert_eq!(cache.modules.len(), 2);

cache.insert(bytecode(100), 3);
assert_eq!(cache.modules.len(), 2);
assert_eq!(cache.total_size, 200);
}
}
4 changes: 2 additions & 2 deletions linera-sdk/tests/fixtures/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading