Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enha: use faster library for storage cache #1895

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ humantime = "=2.1.0"
indexmap = { version = "=2.2.6", features = ["serde"] }
itertools = "=0.13.0"
lazy_static = "=1.4.0"
lru = "=0.12.5"
nanoid = "=0.4.0"
nonempty = { version = "=0.10.0", features = ["serialize"] }
once_cell = "=1.19.0"
Expand All @@ -41,6 +40,7 @@ rustc-hash = "=2.0.0"
smallvec = "=1.13.2"
static_assertions = "=1.1.0"
strum = "=0.26.2"
quick_cache = "=0.6.9"
sugars = "=3.0.1"
thiserror = "=1.0.61"
uuid = { version = "=1.10.0", features = ["v7"]}
Expand Down
48 changes: 15 additions & 33 deletions src/eth/storage/cache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::num::NonZeroUsize;

use lru::LruCache;
use parking_lot::Mutex;
use quick_cache::sync::Cache;
use quick_cache::sync::DefaultLifecycle;
use quick_cache::UnitWeighter;
use rustc_hash::FxBuildHasher;
use smallvec::SmallVec;

use super::AccountWithSlots;
use crate::eth::primitives::Account;
Expand All @@ -14,41 +12,38 @@ use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::SlotValue;

pub struct StorageCache {
slot_cache: Mutex<LruCache<(Address, SlotIndex), SlotValue, FxBuildHasher>>,
account_cache: Mutex<LruCache<Address, Account, FxBuildHasher>>,
slot_cache: Cache<(Address, SlotIndex), SlotValue, UnitWeighter, FxBuildHasher>,
account_cache: Cache<Address, Account, UnitWeighter, FxBuildHasher>,
}

impl Default for StorageCache {
fn default() -> Self {
Self {
slot_cache: Mutex::new(LruCache::with_hasher(NonZeroUsize::new(100_000).unwrap(), FxBuildHasher)),
account_cache: Mutex::new(LruCache::with_hasher(NonZeroUsize::new(20_000).unwrap(), FxBuildHasher)),
slot_cache: Cache::with(100_000, 100_000, UnitWeighter, FxBuildHasher, DefaultLifecycle::default()),
account_cache: Cache::with(20_000, 20_000, UnitWeighter, FxBuildHasher, DefaultLifecycle::default()),
}
}
}

impl StorageCache {
pub fn clear(&self) {
self.slot_cache.lock().clear();
self.account_cache.lock().clear();
self.slot_cache.clear();
self.account_cache.clear();
}

pub fn cache_slot(&self, address: Address, slot: Slot) {
self.slot_cache.lock().put((address, slot.index), slot.value);
self.slot_cache.insert((address, slot.index), slot.value);
}

pub fn cache_account(&self, account: Account) {
self.account_cache.lock().put(account.address, account);
self.account_cache.insert(account.address, account);
}

pub fn cache_account_and_slots_from_changes(&self, changes: ExecutionChanges) {
let mut slot_batch = SmallVec::<[_; 16]>::new();
let mut account_batch = SmallVec::<[_; 8]>::new();

for change in changes.into_values() {
// cache slots
for slot in change.slots.into_values().flat_map(|slot| slot.take()) {
slot_batch.push(((change.address, slot.index), slot.value));
self.slot_cache.insert((change.address, slot.index), slot.value);
}

// cache account
Expand All @@ -62,28 +57,15 @@ impl StorageCache {
if let Some(Some(bytecode)) = change.bytecode.take_ref() {
account.info.bytecode = Some(bytecode.clone());
}
account_batch.push((change.address, account.info));
}

{
let mut slot_lock = self.slot_cache.lock();
for (key, value) in slot_batch {
slot_lock.push(key, value);
}
}
{
let mut account_lock = self.account_cache.lock();
for (key, value) in account_batch {
account_lock.push(key, value);
}
self.account_cache.insert(change.address, account.info);
}
}

pub fn get_slot(&self, address: Address, index: SlotIndex) -> Option<Slot> {
self.slot_cache.lock().get(&(address, index)).map(|&value| Slot { value, index })
self.slot_cache.get(&(address, index)).map(|value| Slot { value, index })
}

pub fn get_account(&self, address: Address) -> Option<Account> {
self.account_cache.lock().get(&address).cloned()
self.account_cache.get(&address)
}
}
Loading