From c34bb62dd85ed66239c39d9275803ed60212b2e1 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin <5773434+shekhirin@users.noreply.github.com> Date: Thu, 18 Dec 2025 13:30:39 +0000 Subject: [PATCH] do not use once_cell --- Cargo.lock | 1 - crates/storage/provider/Cargo.toml | 1 - .../src/providers/database/provider.rs | 5 ++- .../src/providers/state/historical.rs | 32 ++++++++++++------- .../provider/src/providers/state/latest.rs | 18 +++++++---- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc31986135..76de459cd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9984,7 +9984,6 @@ dependencies = [ "itertools 0.14.0", "metrics", "notify", - "once_cell", "parking_lot", "rand 0.9.2", "rayon", diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index 056685f514..22520d82f8 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -54,7 +54,6 @@ parking_lot.workspace = true dashmap = { workspace = true, features = ["inline"] } strum.workspace = true eyre.workspace = true -once_cell.workspace = true # test-utils reth-ethereum-engine-primitives = { workspace = true, optional = true } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 34ba5bb0d1..38cce10743 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -33,7 +33,6 @@ use alloy_primitives::{ Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, }; use itertools::Itertools; -use once_cell::sync::OnceCell; use parking_lot::{Mutex, RwLock}; use rayon::slice::ParallelSliceMut; use reth_chain_state::ExecutedBlock; @@ -94,10 +93,10 @@ pub type DatabaseProviderRO = DatabaseProvider<::TX, N>; /// Cached cursor for plain storage state. pub(crate) type PlainStorageCursor = - Arc::DupCursor>>>; + Arc::DupCursor>>>; /// Cached cursor for storage changeset. pub(crate) type StorageChangesetCursor = - Arc::DupCursor>>>; + Arc::DupCursor>>>; /// A [`DatabaseProvider`] that holds a read-write database transaction. /// diff --git a/crates/storage/provider/src/providers/state/historical.rs b/crates/storage/provider/src/providers/state/historical.rs index d136898236..5eacb1cdcd 100644 --- a/crates/storage/provider/src/providers/state/historical.rs +++ b/crates/storage/provider/src/providers/state/historical.rs @@ -7,7 +7,7 @@ use crate::{ }; use alloy_eips::merge::EPOCH_SLOTS; use alloy_primitives::{Address, BlockNumber, Bytes, StorageKey, StorageValue, B256}; -use parking_lot::Mutex; +use parking_lot::{MappedMutexGuard, MutexGuard}; use reth_db_api::{ cursor::{DbCursorRO, DbDupCursorRO}, models::{storage_sharded_key::StorageShardedKey, ShardedKey}, @@ -125,18 +125,30 @@ impl<'b, Provider: DBProvider + BlockNumReader> HistoricalStateProviderRef<'b, P fn storage_changeset_cursor( &self, - ) -> ProviderResult<&Mutex<::DupCursor>> { - self.storage_changeset_cursor - .get_or_try_init(|| self.provider.tx_ref().cursor_dup_read().map(Mutex::new)) - .map_err(Into::into) + ) -> ProviderResult< + MappedMutexGuard<'_, ::DupCursor>, + > { + MutexGuard::try_map_or_err(self.storage_changeset_cursor.lock(), |cursor| { + ProviderResult::Ok(match cursor { + Some(cursor) => cursor, + None => cursor.insert(self.provider.tx_ref().cursor_dup_read()?), + }) + }) + .map_err(|(_, err)| err) } fn plain_storage_cursor( &self, - ) -> ProviderResult<&Mutex<::DupCursor>> { - self.plain_storage_cursor - .get_or_try_init(|| self.provider.tx_ref().cursor_dup_read().map(Mutex::new)) - .map_err(Into::into) + ) -> ProviderResult< + MappedMutexGuard<'_, ::DupCursor>, + > { + MutexGuard::try_map_or_err(self.plain_storage_cursor.lock(), |cursor| { + ProviderResult::Ok(match cursor { + Some(cursor) => cursor, + None => cursor.insert(self.provider.tx_ref().cursor_dup_read()?), + }) + }) + .map_err(|(_, err)| err) } /// Lookup an account in the `AccountsHistory` table @@ -481,7 +493,6 @@ impl HistoryInfo::InChangeset(changeset_block_number) => { let entry = self .storage_changeset_cursor()? - .lock() .seek_by_key_subkey((changeset_block_number, address).into(), storage_key)? .filter(|entry| entry.key == storage_key); Ok(Some( @@ -497,7 +508,6 @@ impl HistoryInfo::InPlainState | HistoryInfo::MaybeInPlainState => { let entry = self .plain_storage_cursor()? - .lock() .seek_by_key_subkey(address, storage_key)? .filter(|entry| entry.key == storage_key); Ok(entry.map(|e| e.value).or(Some(StorageValue::ZERO))) diff --git a/crates/storage/provider/src/providers/state/latest.rs b/crates/storage/provider/src/providers/state/latest.rs index 21f45189d9..8563fec51e 100644 --- a/crates/storage/provider/src/providers/state/latest.rs +++ b/crates/storage/provider/src/providers/state/latest.rs @@ -3,7 +3,7 @@ use crate::{ AccountReader, BlockHashReader, HashedPostStateProvider, StateProvider, StateRootProvider, }; use alloy_primitives::{Address, BlockNumber, Bytes, StorageKey, StorageValue, B256}; -use parking_lot::Mutex; +use parking_lot::{MappedMutexGuard, MutexGuard}; use reth_db_api::{cursor::DbDupCursorRO, tables, transaction::DbTx}; use reth_primitives_traits::{Account, Bytecode}; use reth_storage_api::{BytecodeReader, DBProvider, StateProofProvider, StorageRootProvider}; @@ -54,10 +54,16 @@ impl<'b, Provider: DBProvider> LatestStateProviderRef<'b, Provider> { fn plain_storage_cursor( &self, - ) -> ProviderResult<&Mutex<::DupCursor>> { - self.plain_storage_cursor - .get_or_try_init(|| self.provider.tx_ref().cursor_dup_read().map(Mutex::new)) - .map_err(Into::into) + ) -> ProviderResult< + MappedMutexGuard<'_, ::DupCursor>, + > { + MutexGuard::try_map_or_err(self.plain_storage_cursor.lock(), |cursor| { + ProviderResult::Ok(match cursor { + Some(cursor) => cursor, + None => cursor.insert(self.provider.tx_ref().cursor_dup_read()?), + }) + }) + .map_err(|(_, err)| err) } fn tx(&self) -> &Provider::Tx { @@ -194,7 +200,7 @@ impl StateProvider storage_key: StorageKey, ) -> ProviderResult> { if let Some(entry) = - self.plain_storage_cursor()?.lock().seek_by_key_subkey(account, storage_key)? && + self.plain_storage_cursor()?.seek_by_key_subkey(account, storage_key)? && entry.key == storage_key { return Ok(Some(entry.value))