Skip to content

Commit

Permalink
Replace RefCell with Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Nov 21, 2022
1 parent 92966d0 commit d7b6cdc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions radix-engine/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use sbor::rust::rc::Rc;
pub use sbor::rust::str::FromStr;
pub use sbor::rust::string::String;
pub use sbor::rust::string::ToString;
pub use sbor::rust::sync::Mutex;
pub use sbor::rust::vec;
pub use sbor::rust::vec::Vec;
pub use sbor::{Decode, DecodeError, Encode, SborPath, SborPathBuf, SborTypeId, SborValue, TypeId};
Expand Down
10 changes: 6 additions & 4 deletions radix-engine/src/wasm/wasm_instrumenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::wasm::{WasmMeteringConfig, WasmModule};

pub struct WasmInstrumenter {
#[cfg(not(feature = "moka"))]
cache: RefCell<lru::LruCache<(Hash, Hash), Arc<Vec<u8>>>>,
cache: Mutex<lru::LruCache<(Hash, Hash), Arc<Vec<u8>>>>,
#[cfg(feature = "moka")]
cache: moka::sync::Cache<(Hash, Hash), Arc<Vec<u8>>>,
}
Expand All @@ -32,7 +32,8 @@ pub struct InstrumentedCode {
impl WasmInstrumenter {
pub fn new(options: InstrumenterOptions) -> Self {
#[cfg(not(feature = "moka"))]
let cache = RefCell::new(lru::LruCache::new(
let cache = Mutex::new(lru::LruCache::new(
// TODO: bespoke LruCache implementation with custom size strategy
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
#[cfg(feature = "moka")]
Expand Down Expand Up @@ -60,7 +61,7 @@ impl WasmInstrumenter {

#[cfg(not(feature = "moka"))]
{
if let Some(cached) = self.cache.borrow_mut().get(&cache_key) {
if let Some(cached) = self.cache.lock().unwrap().get(&cache_key) {
return InstrumentedCode {
code: cached.clone(),
code_hash,
Expand All @@ -79,7 +80,8 @@ impl WasmInstrumenter {

#[cfg(not(feature = "moka"))]
self.cache
.borrow_mut()
.lock()
.unwrap()
.put(cache_key, instrumented_ref.clone());
#[cfg(feature = "moka")]
self.cache.insert(cache_key, instrumented_ref.clone());
Expand Down
10 changes: 6 additions & 4 deletions radix-engine/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct WasmerInstanceEnv {
pub struct WasmerEngine {
store: Store,
#[cfg(not(feature = "moka"))]
modules_cache: RefCell<lru::LruCache<Hash, Arc<WasmerModule>>>,
modules_cache: Mutex<lru::LruCache<Hash, Arc<WasmerModule>>>,
#[cfg(feature = "moka")]
modules_cache: moka::sync::Cache<Hash, Arc<WasmerModule>>,
}
Expand Down Expand Up @@ -239,7 +239,8 @@ impl WasmerEngine {
pub fn new(options: EngineOptions) -> Self {
let compiler = Singlepass::new();
#[cfg(not(feature = "moka"))]
let modules_cache = RefCell::new(lru::LruCache::new(
let modules_cache = Mutex::new(lru::LruCache::new(
// TODO: bespoke LruCache implementation with custom size strategy
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
#[cfg(feature = "moka")]
Expand All @@ -264,7 +265,7 @@ impl WasmEngine for WasmerEngine {
let code_hash = &instrumented_code.code_hash;
#[cfg(not(feature = "moka"))]
{
if let Some(cached_module) = self.modules_cache.borrow_mut().get(code_hash) {
if let Some(cached_module) = self.modules_cache.lock().unwrap().get(code_hash) {
return cached_module.instantiate();
}
}
Expand All @@ -282,7 +283,8 @@ impl WasmEngine for WasmerEngine {

#[cfg(not(feature = "moka"))]
self.modules_cache
.borrow_mut()
.lock()
.unwrap()
.put(*code_hash, new_module.clone());
#[cfg(feature = "moka")]
self.modules_cache.insert(*code_hash, new_module.clone());
Expand Down
10 changes: 6 additions & 4 deletions radix-engine/src/wasm/wasmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub struct EngineOptions {

pub struct WasmiEngine {
#[cfg(not(feature = "moka"))]
modules_cache: RefCell<lru::LruCache<Hash, Arc<WasmiModule>>>,
modules_cache: Mutex<lru::LruCache<Hash, Arc<WasmiModule>>>,
#[cfg(feature = "moka")]
modules_cache: moka::sync::Cache<Hash, Arc<WasmiModule>>,
}
Expand All @@ -236,7 +236,8 @@ impl Default for WasmiEngine {
impl WasmiEngine {
pub fn new(options: EngineOptions) -> Self {
#[cfg(not(feature = "moka"))]
let modules_cache = RefCell::new(lru::LruCache::new(
let modules_cache = Mutex::new(lru::LruCache::new(
// TODO: bespoke LruCache implementation with custom size strategy
NonZeroUsize::new(options.max_cache_size_bytes / (1024 * 1024)).unwrap(),
));
#[cfg(feature = "moka")]
Expand All @@ -259,7 +260,7 @@ impl WasmEngine for WasmiEngine {

#[cfg(not(feature = "moka"))]
{
if let Some(cached_module) = self.modules_cache.borrow_mut().get(code_hash) {
if let Some(cached_module) = self.modules_cache.lock().unwrap().get(code_hash) {
return cached_module.instantiate();
}
}
Expand All @@ -277,7 +278,8 @@ impl WasmEngine for WasmiEngine {

#[cfg(not(feature = "moka"))]
self.modules_cache
.borrow_mut()
.lock()
.unwrap()
.put(*code_hash, new_module.clone());
#[cfg(feature = "moka")]
self.modules_cache.insert(*code_hash, new_module.clone());
Expand Down

0 comments on commit d7b6cdc

Please sign in to comment.