Skip to content

Commit

Permalink
Add LruCache
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Nov 21, 2022
1 parent 40a2f3f commit 84ef28b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 88 deletions.
5 changes: 3 additions & 2 deletions radix-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ utils = { path = "../utils", default-features = false }
radix-engine-constants = { path = "../radix-engine-constants" }
colored = { version = "2.0.0", default-features = false }
hex = { version = "0.4.3", default-features = false }
bitflags = "1.3"
bitflags = { version = "1.3" }
indexmap = { version = "1.8.1" }
lru = { version = "0.8.1", default-features = false }

# WASM de-/serialization
parity-wasm = { version = "0.42.2" }
Expand Down Expand Up @@ -55,7 +56,7 @@ harness = false
# You should enable either `std` or `alloc`
default = ["std"]
std = ["sbor/std", "scrypto/std", "scrypto-unit/std", "wasmi/std", "transaction/std", "radix-engine-interface/std", "utils/std"]
alloc = ["sbor/alloc", "scrypto/alloc", "scrypto-unit/alloc", "transaction/alloc", "radix-engine-interface/alloc", "utils/alloc"]
alloc = ["sbor/alloc", "scrypto/alloc", "scrypto-unit/alloc", "transaction/alloc", "radix-engine-interface/alloc", "utils/alloc", "lru/hashbrown"]

# Use `wasmer` as WASM engine, otherwise `wasmi`
wasmer = ["dep:wasmer", "dep:wasmer-compiler-singlepass"]
19 changes: 10 additions & 9 deletions radix-engine/src/wasm/wasm_instrumenter.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use lru::LruCache;
use radix_engine_interface::crypto::hash;
use sbor::rust::cell::RefCell;
use sbor::rust::collections::HashMap;
use sbor::rust::num::NonZeroUsize;
use sbor::rust::sync::Arc;

use crate::types::*;
use crate::wasm::{WasmMeteringConfig, WasmModule};

pub struct WasmInstrumenter {
cache: RefCell<HashMap<(Hash, Hash), Arc<Vec<u8>>>>,
cache: RefCell<LruCache<(Hash, Hash), Arc<Vec<u8>>>>,
}

#[derive(Debug, Clone)]
pub struct InstrumenterOptions {
#[allow(dead_code)]
max_cache_size_bytes: u64,
max_cache_size_bytes: usize,
}

impl Default for WasmInstrumenter {
Expand All @@ -30,9 +30,10 @@ pub struct InstrumentedCode {
}

impl WasmInstrumenter {
pub fn new(_options: InstrumenterOptions) -> Self {
// TODO: limit size
let cache = RefCell::new(HashMap::new());
pub fn new(options: InstrumenterOptions) -> Self {
let cache = RefCell::new(LruCache::new(
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));

Self { cache }
}
Expand All @@ -46,7 +47,7 @@ impl WasmInstrumenter {
let cache_key = (code_hash, *wasm_metering_config.identifier());

{
if let Some(cached) = self.cache.borrow().get(&cache_key) {
if let Some(cached) = self.cache.borrow_mut().get(&cache_key) {
return InstrumentedCode {
code: cached.clone(),
code_hash,
Expand All @@ -58,7 +59,7 @@ impl WasmInstrumenter {

self.cache
.borrow_mut()
.insert(cache_key, instrumented_ref.clone());
.put(cache_key, instrumented_ref.clone());

InstrumentedCode {
code: instrumented_ref,
Expand Down
12 changes: 6 additions & 6 deletions radix-engine/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ impl WasmInstance for WasmerInstance {

#[derive(Debug, Clone)]
pub struct EngineOptions {
#[allow(dead_code)]
max_cache_size_bytes: u64,
max_cache_size_bytes: usize,
}

impl Default for WasmerEngine {
Expand All @@ -234,10 +233,11 @@ impl Default for WasmerEngine {
}

impl WasmerEngine {
#[allow(unused_variables)]
pub fn new(options: EngineOptions) -> Self {
let compiler = Singlepass::new();
let modules_cache = RefCell::new(HashMap::new());
let modules_cache = RefCell::new(LruCache::new(
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
Self {
store: Store::new(&Universal::new(compiler).engine()),
modules_cache,
Expand All @@ -251,7 +251,7 @@ impl WasmEngine for WasmerEngine {
fn instantiate(&self, instrumented_code: &InstrumentedCode) -> WasmerInstance {
let code_hash = &instrumented_code.code_hash;
{
if let Some(cached_module) = self.modules_cache.borrow().get(code_hash) {
if let Some(cached_module) = self.modules_cache.borrow_mut().get(code_hash) {
return cached_module.instantiate();
}
}
Expand All @@ -265,7 +265,7 @@ impl WasmEngine for WasmerEngine {

self.modules_cache
.borrow_mut()
.insert(*code_hash, new_module.clone());
.put(*code_hash, new_module.clone());

new_module.instantiate()
}
Expand Down
22 changes: 10 additions & 12 deletions radix-engine/src/wasm/wasmi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use lru::LruCache;
use radix_engine_interface::crypto::Hash;
use radix_engine_interface::data::IndexedScryptoValue;
use sbor::rust::cell::RefCell;
use sbor::rust::collections::HashMap;
use sbor::rust::num::NonZeroUsize;
use sbor::rust::sync::Arc;
use wasmi::*;

Expand Down Expand Up @@ -213,12 +214,11 @@ impl WasmInstance for WasmiInstance {

#[derive(Debug, Clone)]
pub struct EngineOptions {
#[allow(dead_code)]
max_cache_size_bytes: u64,
max_cache_size_bytes: usize,
}

pub struct WasmiEngine {
modules_cache: RefCell<HashMap<Hash, Arc<WasmiModule>>>,
modules_cache: RefCell<LruCache<Hash, Arc<WasmiModule>>>,
}

impl Default for WasmiEngine {
Expand All @@ -230,13 +230,11 @@ impl Default for WasmiEngine {
}

impl WasmiEngine {
#[allow(unused_variables)]
pub fn new(options: EngineOptions) -> Self {
// TODO: limit size
let cache = RefCell::new(HashMap::new());
Self {
modules_cache: cache,
}
let modules_cache = RefCell::new(LruCache::new(
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
Self { modules_cache }
}
}

Expand All @@ -246,7 +244,7 @@ impl WasmEngine for WasmiEngine {
fn instantiate(&self, instrumented_code: &InstrumentedCode) -> WasmiInstance {
let code_hash = &instrumented_code.code_hash;
{
if let Some(cached_module) = self.modules_cache.borrow().get(code_hash) {
if let Some(cached_module) = self.modules_cache.borrow_mut().get(code_hash) {
return cached_module.instantiate();
}
}
Expand All @@ -260,7 +258,7 @@ impl WasmEngine for WasmiEngine {

self.modules_cache
.borrow_mut()
.insert(*code_hash, new_module.clone());
.put(*code_hash, new_module.clone());

new_module.instantiate()
}
Expand Down
4 changes: 4 additions & 0 deletions sbor/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub use core::marker;
#[cfg(feature = "alloc")]
pub use core::mem;
#[cfg(feature = "alloc")]
pub use core::num;
#[cfg(feature = "alloc")]
pub use core::ops;
#[cfg(feature = "alloc")]
pub use core::ptr;
Expand Down Expand Up @@ -62,6 +64,8 @@ pub use std::marker;
#[cfg(not(feature = "alloc"))]
pub use std::mem;
#[cfg(not(feature = "alloc"))]
pub use std::num;
#[cfg(not(feature = "alloc"))]
pub use std::ops;
#[cfg(not(feature = "alloc"))]
pub use std::ptr;
Expand Down
Loading

0 comments on commit 84ef28b

Please sign in to comment.