Skip to content

Commit

Permalink
Merge pull request #228 from OffchainLabs/simplify-init-cache
Browse files Browse the repository at this point in the history
Simplify Init Cache
  • Loading branch information
rachel-bousfield authored Apr 16, 2024
2 parents edfe58c + b41bbdf commit 0c44124
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
38 changes: 6 additions & 32 deletions arbitrator/stylus/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use lazy_static::lazy_static;
use lru::LruCache;
use parking_lot::Mutex;
use prover::programs::config::CompileConfig;
use rand::Rng;
use std::{
collections::{hash_map::Entry, HashMap},
num::NonZeroUsize,
};
use std::{collections::HashMap, num::NonZeroUsize};
use wasmer::{Engine, Module, Store};

lazy_static! {
Expand Down Expand Up @@ -50,20 +46,11 @@ impl CacheKey {
struct CacheItem {
module: Module,
engine: Engine,
/// Represents the number of uses this item has left.
/// This is done to mitigate any potential memory leaks in wasmer, though likely isn't necessary.
/// TODO: remove after gaining confidence in wasmer.
uses: usize,
}

impl CacheItem {
fn new(module: Module, engine: Engine) -> Self {
let uses = rand::thread_rng().gen_range(8..16);
Self {
module,
engine,
uses,
}
Self { module, engine }
}

fn data(&self) -> (Module, Store) {
Expand All @@ -85,26 +72,13 @@ impl InitCache {
let key = CacheKey::new(module_hash, version, debug);

// See if the item is in the long term cache
if let Entry::Occupied(mut entry) = cache.arbos.entry(key) {
let item = entry.get_mut();
item.uses = item.uses.saturating_sub(1);

let data = item.data();
if item.uses == 0 {
entry.remove();
}
return Some(data);
if let Some(item) = cache.arbos.get(&key) {
return Some(item.data());
}

// See if the item is in the LRU cache, promoting if so
if let Some(item) = cache.lru.get_mut(&key) {
item.uses = item.uses.saturating_sub(1);

let data = item.data();
if item.uses == 0 {
cache.lru.pop(&key);
}
return Some(data);
if let Some(item) = cache.lru.get(&key) {
return Some(item.data());
}
None
}
Expand Down
2 changes: 1 addition & 1 deletion contracts

0 comments on commit 0c44124

Please sign in to comment.