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 16dce1f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 89 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"]
18 changes: 9 additions & 9 deletions radix-engine/src/wasm/wasm_instrumenter.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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<lru::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 +29,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(lru::LruCache::new(
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));

Self { cache }
}
Expand All @@ -46,7 +46,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 +58,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
15 changes: 8 additions & 7 deletions radix-engine/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::model::InvokeError;
use radix_engine_interface::data::IndexedScryptoValue;
use sbor::rust::num::NonZeroUsize;
use sbor::rust::sync::{Arc, Mutex};
use wasmer::{
imports, Function, HostEnvInitError, Instance, LazyInit, Module, RuntimeError, Store,
Expand Down Expand Up @@ -36,7 +37,7 @@ pub struct WasmerInstanceEnv {

pub struct WasmerEngine {
store: Store,
modules_cache: RefCell<HashMap<Hash, Arc<WasmerModule>>>,
modules_cache: RefCell<lru::LruCache<Hash, Arc<WasmerModule>>>,
}

pub fn send_value(instance: &Instance, value: &[u8]) -> Result<usize, InvokeError<WasmError>> {
Expand Down Expand Up @@ -221,8 +222,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 +234,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(lru::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 +252,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 +266,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
21 changes: 9 additions & 12 deletions radix-engine/src/wasm/wasmi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 +213,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<lru::LruCache<Hash, Arc<WasmiModule>>>,
}

impl Default for WasmiEngine {
Expand All @@ -230,13 +229,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(lru::LruCache::new(
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
Self { modules_cache }
}
}

Expand All @@ -246,7 +243,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 +257,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 16dce1f

Please sign in to comment.