Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
refactor(state): replace GlobalContractCache default with GlobalContr…
Browse files Browse the repository at this point in the history
…actCache new (#1368)
  • Loading branch information
OriStarkware committed Jan 25, 2024
1 parent da50b4b commit 2f7113f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
21 changes: 12 additions & 9 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,13 @@ impl<S: StateReader> CachedState<S> {
}
}

#[cfg(any(feature = "testing", test))]
impl<S: StateReader> From<S> for CachedState<S> {
fn from(state_reader: S) -> Self {
CachedState::new(state_reader, Default::default())
CachedState::new(
state_reader,
GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
)
}
}

Expand Down Expand Up @@ -365,7 +369,9 @@ impl Default for CachedState<crate::test_utils::dict_state_reader::DictStateRead
state: Default::default(),
cache: Default::default(),
class_hash_to_class: Default::default(),
global_class_hash_to_class: Default::default(),
global_class_hash_to_class: GlobalContractCache::new(
GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST,
),
visited_pcs: Default::default(),
}
}
Expand Down Expand Up @@ -726,10 +732,9 @@ type LockedContractClassCache<'a> = MutexGuard<'a, ContractClassLRUCache>;
// `blockifier` compiles as a shared library.
pub struct GlobalContractCache(pub Arc<Mutex<ContractClassLRUCache>>);

impl GlobalContractCache {
// TODO(Arni, 7/1/2024): make this configurable via a CachedState constructor argument.
const CACHE_SIZE: usize = 100;
pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 100;

impl GlobalContractCache {
/// Locks the cache for atomic access. Although conceptually shared, writing to this cache is
/// only possible for one writer at a time.
pub fn lock(&mut self) -> LockedContractClassCache<'_> {
Expand All @@ -739,10 +744,8 @@ impl GlobalContractCache {
pub fn clear(&mut self) {
self.lock().cache_clear();
}
}

impl Default for GlobalContractCache {
fn default() -> Self {
Self(Arc::new(Mutex::new(ContractClassLRUCache::with_size(Self::CACHE_SIZE))))
pub fn new(cache_size: usize) -> Self {
Self(Arc::new(Mutex::new(ContractClassLRUCache::with_size(cache_size))))
}
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/cached_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn test_state_changes_merge() {
fn global_contract_cache_is_used() {
// Initialize the global cache with a single class, and initialize an empty state with this
// cache.
let mut global_cache = GlobalContractCache::default();
let mut global_cache = GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST);
let class_hash = class_hash!(TEST_CLASS_HASH);
let contract_class = get_test_contract_class();
global_cache.lock().cache_set(class_hash, contract_class.clone());
Expand Down
11 changes: 6 additions & 5 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use blockifier::block_context::{BlockContext, BlockInfo, ChainInfo, FeeTokenAddresses, GasPrices};
use blockifier::state::cached_state::GlobalContractCache;
use blockifier::state::cached_state::{GlobalContractCache, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST};
use pyo3::prelude::*;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress};
Expand Down Expand Up @@ -33,10 +33,11 @@ pub struct PyBlockExecutor {
#[pymethods]
impl PyBlockExecutor {
#[new]
#[pyo3(signature = (general_config, max_recursion_depth, target_storage_config))]
#[pyo3(signature = (general_config, max_recursion_depth, global_contract_cache_size, target_storage_config))]
pub fn create(
general_config: PyGeneralConfig,
max_recursion_depth: usize,
global_contract_cache_size: usize,
target_storage_config: StorageConfig,
) -> Self {
log::debug!("Initializing Block Executor...");
Expand All @@ -50,7 +51,7 @@ impl PyBlockExecutor {
max_recursion_depth,
tx_executor,
storage: Box::new(storage),
global_contract_cache: GlobalContractCache::default(),
global_contract_cache: GlobalContractCache::new(global_contract_cache_size),
}
}

Expand Down Expand Up @@ -203,7 +204,7 @@ impl PyBlockExecutor {
general_config,
max_recursion_depth: 50,
tx_executor: None,
global_contract_cache: GlobalContractCache::default(),
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
}
}
}
Expand All @@ -226,7 +227,7 @@ impl PyBlockExecutor {
general_config: PyGeneralConfig::default(),
max_recursion_depth: 50,
tx_executor: None,
global_contract_cache: GlobalContractCache::default(),
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blockifier::execution::call_info::CallInfo;
use blockifier::fee::actual_cost::ActualCost;
use blockifier::fee::fee_checks::PostValidationReport;
use blockifier::state::cached_state::GlobalContractCache;
use blockifier::state::cached_state::{GlobalContractCache, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST};
use blockifier::state::state_api::StateReader;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::objects::{AccountTransactionContext, TransactionExecutionResult};
Expand Down Expand Up @@ -31,20 +31,21 @@ pub struct PyValidator {
#[pymethods]
impl PyValidator {
#[new]
#[pyo3(signature = (general_config, state_reader_proxy, next_block_info, max_recursion_depth, max_nonce_for_validation_skip))]
#[pyo3(signature = (general_config, state_reader_proxy, next_block_info, max_recursion_depth, global_contract_cache_size, max_nonce_for_validation_skip))]
pub fn create(
general_config: PyGeneralConfig,
state_reader_proxy: &PyAny,
next_block_info: PyBlockInfo,
max_recursion_depth: usize,
global_contract_cache_size: usize,
max_nonce_for_validation_skip: PyFelt,
) -> NativeBlockifierResult<Self> {
let tx_executor = TransactionExecutor::new(
PyStateReader::new(state_reader_proxy),
&general_config,
next_block_info,
max_recursion_depth,
GlobalContractCache::default(),
GlobalContractCache::new(global_contract_cache_size),
)?;
let validator = Self {
general_config,
Expand Down Expand Up @@ -115,7 +116,7 @@ impl PyValidator {
&general_config,
next_block_info,
max_recursion_depth,
GlobalContractCache::default(),
GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
)?;
Ok(Self {
general_config,
Expand Down

0 comments on commit 2f7113f

Please sign in to comment.