Skip to content

Commit

Permalink
change cache size and use replace
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Oct 17, 2024
1 parent e03c10a commit 40ea60e
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions crates/chain-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing.workspace = true
# cache
lazy_static = "1.5.0"
quick_cache = "0.6.6"
criterion = "0.5.1"

# misc
auto_impl.workspace = true
Expand Down Expand Up @@ -66,3 +67,11 @@ test-utils = [
"rand",
"revm"
]

[[bench]]
name = "storage_cache"
harness = false

[[bench]]
name = "account_cache"
harness = false
75 changes: 75 additions & 0 deletions crates/chain-state/benches/account_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#![allow(missing_docs, unreachable_pub)]

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use reth_chain_state::cache;
use reth_primitives::{Account, Address, B256, U256};
use std::io::Read;

pub fn run_account_benchmark(c: &mut Criterion) {
let addrs = setup_account_addr(500000);
setup_account_cache(&addrs);

c.bench_function("account - quick cache access", |b| {
b.iter_batched(
|| random_account_addr(&addrs),
|addr| {
black_box(account_access_quick_cache(addr));
},
criterion::BatchSize::SmallInput,
)
});

c.bench_function("account - quick cache write", |b| {
b.iter_batched(
|| random_account_addr(&addrs),
|addr| {
black_box(account_write_quick_cache(addr));
},
criterion::BatchSize::SmallInput,
)
});
}

fn setup_account_cache(addrs: &Vec<Address>) {
addrs.iter().for_each(|address: &Address| {
cache::plain_state::PLAIN_ACCOUNTS.insert(
*address,
Account { nonce: 1, balance: U256::from(200), bytecode_hash: Some(B256::random()) },
);
});
}

fn setup_account_addr(size: u64) -> Vec<Address> {
let mut lines: Vec<Address> = Vec::new();
for _i in 0..size {
lines.push(Address::random());
}
lines
}

fn random_account_addr(addrs: &Vec<Address>) -> Address {
let max = addrs.len();
let mut rng = rand::thread_rng();
let index = rng.gen_range(0..max);
return addrs[index]
}

fn account_access_quick_cache(addr: Address) {
let _ = cache::plain_state::PLAIN_ACCOUNTS.get(&addr);
}

fn account_write_quick_cache(address: Address) {
let _ = cache::plain_state::PLAIN_ACCOUNTS.replace(
address,
Account { nonce: 1, balance: U256::from(200), bytecode_hash: Some(B256::random()) },
true,
);
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = run_account_benchmark
}
criterion_main!(benches);
72 changes: 72 additions & 0 deletions crates/chain-state/benches/storage_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![allow(missing_docs, unreachable_pub)]

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use reth_chain_state::cache;
use reth_primitives::{Address, B256, U256};
use std::io::Read;

pub fn run_storage_benchmark(c: &mut Criterion) {
let addrs = setup_storage_addr(1000000);
setup_storage_cache(&addrs);

c.bench_function("storage - quick cache access", |b| {
b.iter_batched(
|| random_storage_addr(&addrs),
|addr| {
black_box(storage_access_quick_cache(addr));
},
criterion::BatchSize::SmallInput,
)
});

c.bench_function("storage - quick cache write", |b| {
b.iter_batched(
|| random_storage_addr(&addrs),
|addr| {
black_box(storage_write_quick_cache(addr));
},
criterion::BatchSize::SmallInput,
)
});
}

fn setup_storage_cache(addrs: &Vec<Address>) {
addrs.iter().for_each(|address: &Address| {
cache::plain_state::PLAIN_STORAGES.insert((*address, B256::random()), U256::from(128));
});
}

fn setup_storage_addr(size: u64) -> Vec<Address> {
let mut lines: Vec<Address> = Vec::new();
for _i in 0..size {
lines.push(Address::random());
}
lines
}

fn random_storage_addr(addrs: &Vec<Address>) -> Address {
let max = addrs.len();
let mut rng = rand::thread_rng();
let index = rng.gen_range(0..max);
return addrs[index]
}

fn storage_access_quick_cache(addr: Address) {
let _ = cache::plain_state::PLAIN_STORAGES.get(&(addr, B256::random()));
}

fn storage_write_quick_cache(address: Address) {
let _ = cache::plain_state::PLAIN_STORAGES.replace(
(address, B256::random()),
U256::from(128),
true,
);
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = run_storage_benchmark
}
criterion_main!(benches);
33 changes: 16 additions & 17 deletions crates/chain-state/src/cache/plain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ use reth_db_api::transaction::DbTx;
use reth_primitives::{Account, Address, Bytecode, StorageKey, StorageValue, B256, U256};
use reth_revm::db::{BundleState, OriginalValuesKnown};
use std::sync::atomic::AtomicU64;

lazy_static! {
static ref CACHE_SZ: AtomicU64 = AtomicU64::new(0);
}
use tracing::info;

// Cache sizes
const ACCOUNT_CACHE_SIZE: usize = 1000000;
const STORAGE_CACHE_SIZE: usize = ACCOUNT_CACHE_SIZE * 5;
const CONTRACT_CACHE_SIZE: usize = ACCOUNT_CACHE_SIZE / 100;
const ACCOUNT_CACHE_SIZE: usize = 500000;
const STORAGE_CACHE_SIZE: usize = ACCOUNT_CACHE_SIZE * 2;
const CONTRACT_CACHE_SIZE: usize = 10000;

// Type alias for address and storage key tuple
type AddressStorageKey = (Address, StorageKey);

lazy_static! {
/// Account cache
pub(crate) static ref PLAIN_ACCOUNTS: Cache<Address, Account> = Cache::new(ACCOUNT_CACHE_SIZE);
pub static ref PLAIN_ACCOUNTS: Cache<Address, Account> = Cache::new(ACCOUNT_CACHE_SIZE);

/// Storage cache
pub(crate) static ref PLAIN_STORAGES: Cache<AddressStorageKey, StorageValue> = Cache::new(STORAGE_CACHE_SIZE);
pub static ref PLAIN_STORAGES: Cache<AddressStorageKey, StorageValue> = Cache::new(STORAGE_CACHE_SIZE);

/// Contract cache
/// The size of contract is large and the hot contracts should be limited.
Expand All @@ -39,12 +35,6 @@ pub(crate) fn insert_account(k: Address, v: Account) {
/// Insert storage into the cache
pub(crate) fn insert_storage(k: AddressStorageKey, v: U256) {
PLAIN_STORAGES.insert(k, v);

let current = CACHE_SZ.load(std::sync::atomic::Ordering::Relaxed);
CACHE_SZ.store(current + 1, std::sync::atomic::Ordering::Relaxed);
if current % 10000 == 0 {
info!("CACHE_SZ {}", current);
}
}

// Get account from cache
Expand Down Expand Up @@ -85,6 +75,10 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
match cursor {
Ok(mut cursor) => {
for block in blocks {
if block.block.number % 100 == 0 {
info!("CACHE_SZ {}", PLAIN_STORAGES.len());
};
PLAIN_STORAGES.len();
let bundle_state = block.execution_outcome().clone().bundle;
let change_set = bundle_state.into_plain_state(OriginalValuesKnown::Yes);

Expand All @@ -95,13 +89,14 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
PLAIN_ACCOUNTS.remove(address);
}
Some(acc) => {
PLAIN_ACCOUNTS.insert(
let _ = PLAIN_ACCOUNTS.replace(
*address,
Account {
nonce: acc.nonce,
balance: acc.balance,
bytecode_hash: Some(acc.code_hash),
},
true,
);
}
}
Expand All @@ -125,7 +120,11 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
}

for (k, v) in storage.storage.clone() {
insert_storage((storage.address, StorageKey::from(k)), v);
let _ = PLAIN_STORAGES.replace(
(storage.address, StorageKey::from(k)),
v,
true,
);
}
}
}
Expand Down

0 comments on commit 40ea60e

Please sign in to comment.