From 16dce1f15900f91a797403baf3bdd4ff4df61b06 Mon Sep 17 00:00:00 2001 From: Yulong Wu Date: Mon, 21 Nov 2022 11:17:32 +0000 Subject: [PATCH] Add LruCache --- radix-engine/Cargo.toml | 5 +- radix-engine/src/wasm/wasm_instrumenter.rs | 18 ++-- radix-engine/src/wasm/wasmer.rs | 15 +-- radix-engine/src/wasm/wasmi.rs | 21 ++-- sbor/src/rust.rs | 4 + simulator/Cargo.lock | 117 ++++++++++----------- 6 files changed, 91 insertions(+), 89 deletions(-) diff --git a/radix-engine/Cargo.toml b/radix-engine/Cargo.toml index 4cc7d9d06b1..f90758e820a 100644 --- a/radix-engine/Cargo.toml +++ b/radix-engine/Cargo.toml @@ -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" } @@ -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"] diff --git a/radix-engine/src/wasm/wasm_instrumenter.rs b/radix-engine/src/wasm/wasm_instrumenter.rs index 1e3b88aaf43..b7248c93a1b 100644 --- a/radix-engine/src/wasm/wasm_instrumenter.rs +++ b/radix-engine/src/wasm/wasm_instrumenter.rs @@ -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>>>, + cache: RefCell>>>, } #[derive(Debug, Clone)] pub struct InstrumenterOptions { - #[allow(dead_code)] - max_cache_size_bytes: u64, + max_cache_size_bytes: usize, } impl Default for WasmInstrumenter { @@ -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 } } @@ -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, @@ -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, diff --git a/radix-engine/src/wasm/wasmer.rs b/radix-engine/src/wasm/wasmer.rs index 145eaaf1e8c..fe019e980f5 100644 --- a/radix-engine/src/wasm/wasmer.rs +++ b/radix-engine/src/wasm/wasmer.rs @@ -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, @@ -36,7 +37,7 @@ pub struct WasmerInstanceEnv { pub struct WasmerEngine { store: Store, - modules_cache: RefCell>>, + modules_cache: RefCell>>, } pub fn send_value(instance: &Instance, value: &[u8]) -> Result> { @@ -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 { @@ -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, @@ -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(); } } @@ -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() } diff --git a/radix-engine/src/wasm/wasmi.rs b/radix-engine/src/wasm/wasmi.rs index 5f4889974aa..ac0912ae560 100644 --- a/radix-engine/src/wasm/wasmi.rs +++ b/radix-engine/src/wasm/wasmi.rs @@ -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::*; @@ -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>>, + modules_cache: RefCell>>, } impl Default for WasmiEngine { @@ -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 } } } @@ -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(); } } @@ -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() } diff --git a/sbor/src/rust.rs b/sbor/src/rust.rs index b321bcf990b..2b41cd42345 100644 --- a/sbor/src/rust.rs +++ b/sbor/src/rust.rs @@ -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; @@ -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; diff --git a/simulator/Cargo.lock b/simulator/Cargo.lock index 3fc215b5177..723da250fe2 100644 --- a/simulator/Cargo.lock +++ b/simulator/Cargo.lock @@ -128,9 +128,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.73" +version = "1.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" +checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" dependencies = [ "jobserver", ] @@ -152,9 +152,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" dependencies = [ "iana-time-zone", "num-integer", @@ -274,9 +274,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.80" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b7d4e43b25d3c994662706a1d4fcfc32aaa6afd287502c111b237093bb23f3a" +checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453" dependencies = [ "cc", "cxxbridge-flags", @@ -286,9 +286,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.80" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84f8829ddc213e2c1368e51a2564c552b65a8cb6a28f31e576270ac81d5e5827" +checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0" dependencies = [ "cc", "codespan-reporting", @@ -301,15 +301,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.80" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e72537424b474af1460806647c41d4b6d35d09ef7fe031c5c2fa5766047cc56a" +checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71" [[package]] name = "cxxbridge-macro" -version = "1.0.80" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "309e4fb93eed90e1e14bea0da16b209f81813ba9fc7830c20ed151dd7bc0a4d7" +checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470" dependencies = [ "proc-macro2", "quote", @@ -362,9 +362,9 @@ dependencies = [ [[package]] name = "digest" -version = "0.10.5" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer 0.10.3", "crypto-common", @@ -501,9 +501,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.51" +version = "0.1.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5a6ef98976b22b3b7f2f3a806f858cb862044cfa66805aa3ad84cb3d3b785ed" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -531,9 +531,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "indexmap" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown", @@ -566,9 +566,12 @@ dependencies = [ [[package]] name = "keccak" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9b7d56ba4a8344d6be9729995e6b06f928af29998cdf79fe390cbf6b1fee838" +checksum = "3afef3b6eff9ce9d8ff9b3601125eec7f0c8cbac7abd14f355d053fa56c98768" +dependencies = [ + "cpufeatures", +] [[package]] name = "lazy_static" @@ -590,9 +593,9 @@ checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" [[package]] name = "libloading" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ "cfg-if", "winapi", @@ -600,9 +603,9 @@ dependencies = [ [[package]] name = "libm" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "292a948cd991e376cf75541fe5b97a1081d713c618b4f1b9500f8844e49eb565" +checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" [[package]] name = "librocksdb-sys" @@ -648,6 +651,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "lru" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6e8aaa3f231bb4bd57b84b2d5dc3ae7f350265df8aa96492e0bc394a1571909" + [[package]] name = "memchr" version = "2.5.0" @@ -718,20 +727,11 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_threads" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" -dependencies = [ - "libc", -] - [[package]] name = "once_cell" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" [[package]] name = "opaque-debug" @@ -741,9 +741,9 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "os_str_bytes" -version = "6.3.0" +version = "6.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" [[package]] name = "parity-wasm" @@ -771,9 +771,9 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" [[package]] name = "ppv-lite86" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro-error" @@ -825,6 +825,7 @@ dependencies = [ "colored", "hex", "indexmap", + "lru", "parity-wasm", "radix-engine-constants", "radix-engine-interface", @@ -975,9 +976,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" dependencies = [ "aho-corasick", "memchr", @@ -986,9 +987,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.27" +version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "rocksdb" @@ -1127,9 +1128,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" +checksum = "8e8b3801309262e8184d9687fb697586833e939767aea0dda89f5a8e650e8bd7" dependencies = [ "itoa", "ryu", @@ -1138,9 +1139,9 @@ dependencies = [ [[package]] name = "serde_with" -version = "2.0.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368f2d60d049ea019a84dcd6687b0d1e0030fe663ae105039bdf967ed5e6a9a7" +checksum = "25bf4a5a814902cd1014dbccfa4d4560fb8432c779471e96e035602519f82eef" dependencies = [ "base64", "chrono", @@ -1154,9 +1155,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "2.0.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ccadfacf6cf10faad22bbadf55986bdd0856edfb5d9210aa1dcf1f516e84e93" +checksum = "e3452b4c0f6c1e357f73fdb87cd1efabaa12acf328c7a528e252893baeb3f4aa" dependencies = [ "darling", "proc-macro2", @@ -1185,7 +1186,7 @@ checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.10.5", + "digest 0.10.6", ] [[package]] @@ -1194,7 +1195,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdf0c33fae925bdc080598b84bc15c55e7b9a4a43b3c704da051f977469691c9" dependencies = [ - "digest 0.10.5", + "digest 0.10.6", "keccak", ] @@ -1338,13 +1339,11 @@ dependencies = [ [[package]] name = "time" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fab5c8b9980850e06d92ddbe3ab839c062c801f3927c0fb8abd6fc8e918fbca" +checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" dependencies = [ "itoa", - "libc", - "num_threads", "serde", "time-core", "time-macros", @@ -1358,9 +1357,9 @@ checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" [[package]] name = "time-macros" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65bb801831d812c562ae7d2bfb531f26e66e4e1f6b17307ba4149c5064710e5b" +checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2" dependencies = [ "time-core", ] @@ -1422,9 +1421,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83" +checksum = "422ee0de9031b5b948b97a8fc04e3aa35230001a722ddd27943e0be31564ce4c" dependencies = [ "getrandom 0.2.8", ]