Skip to content

Commit

Permalink
chore: replace lru with quick cache
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef authored and pashinov committed Feb 12, 2024
1 parent f8c669b commit 7c04f5c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"Ivan Kalinin <[email protected]>",
"Stanislav Eliseev <[email protected]>"
]
rust-version = "1.62.0"
rust-version = "1.65.0"
edition = "2021"

[workspace]
Expand Down Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 7 additions & 12 deletions src/core/dens.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,7 +15,7 @@ use crate::transport::Transport;
#[derive(Default)]
pub struct Dens {
tld: RwLock<HashMap<String, Arc<DensTld>>>,
contract_address_cache: Option<Mutex<LruCache<String, MsgAddressInt>>>,
contract_address_cache: Option<Cache<String, MsgAddressInt>>,
}

impl Dens {
Expand Down Expand Up @@ -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()));
}
}
Expand All @@ -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());
}
}

Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 7c04f5c

Please sign in to comment.