From 7c04f5caa4903449dd6390a24dd77d49552620bb Mon Sep 17 00:00:00 2001 From: Vladimir Petrzhikovskii Date: Mon, 8 Jan 2024 16:49:53 +0100 Subject: [PATCH] chore: replace lru with quick cache --- Cargo.toml | 5 ++--- README.md | 2 +- src/core/dens.rs | 19 +++++++------------ 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0fd54fbda..da55548c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ authors = [ "Ivan Kalinin ", "Stanislav Eliseev " ] -rust-version = "1.62.0" +rust-version = "1.65.0" edition = "2021" [workspace] @@ -35,12 +35,11 @@ getrandom = { version = "0.2.4", optional = true } hex = "0.4" hmac = { version = "0.11.0", optional = true } log = "0.4" -lru = "0.8.0" num-bigint = "0.4" once_cell = "1.12.0" parking_lot = "0.12.0" pbkdf2 = { version = "0.9.0", optional = true } -quick_cache = "0.3.0" +quick_cache = "0.4.1" rand = { version = "0.8", features = ["getrandom"] , optional = true } secstr = { version = "0.5.0", features = ["serde"], optional = true } serde = { version = "1.0", features = ["derive"] } diff --git a/README.md b/README.md index 2e65619da..f22bdc251 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ cargo add nekoton ### Prerequisites -- Rust 1.62+ +- Rust 1.65+ - `wasm-pack` 0.9.1+ (to test build for wasm target) - protoc 3.12.4+ (to generate .rs files from .proto) diff --git a/src/core/dens.rs b/src/core/dens.rs index a1b089ae2..c2d4a54ab 100644 --- a/src/core/dens.rs +++ b/src/core/dens.rs @@ -1,12 +1,11 @@ use std::collections::hash_map::{self, HashMap}; -use std::num::NonZeroUsize; use std::sync::Arc; use anyhow::Result; -use lru::LruCache; use nekoton_contracts::dens; use nekoton_utils::Clock; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; +use quick_cache::sync::Cache; use ton_block::MsgAddressInt; use crate::transport::models::{ExistingContract, RawContractState}; @@ -16,7 +15,7 @@ use crate::transport::Transport; #[derive(Default)] pub struct Dens { tld: RwLock>>, - contract_address_cache: Option>>, + contract_address_cache: Option>, } impl Dens { @@ -48,7 +47,7 @@ impl Dens { } if let Some(contract_address_cache) = &self.contract_address_cache { - if let Some(address) = contract_address_cache.lock().get(path) { + if let Some(address) = contract_address_cache.get(path) { return Ok(Some(address.clone())); } } @@ -60,9 +59,7 @@ impl Dens { if let Some(address) = &address { if let Some(contract_address_cache) = &self.contract_address_cache { - contract_address_cache - .lock() - .push(path.to_owned(), address.clone()); + contract_address_cache.insert(path.to_owned(), address.clone()); } } @@ -79,7 +76,7 @@ impl Dens { pub fn reset_cache(&self) { if let Some(contract_address_cache) = &self.contract_address_cache { - contract_address_cache.lock().clear(); + contract_address_cache.clear(); } } @@ -117,9 +114,7 @@ impl DensBuilder { } pub fn with_contract_address_cache(mut self, capacity: usize) -> Self { - self.dens.contract_address_cache = NonZeroUsize::new(capacity) - .map(LruCache::new) - .map(Mutex::new); + self.dens.contract_address_cache = Some(Cache::new(capacity)); self }