Skip to content

Commit

Permalink
perf: don't recreate ReadOptions when querying the database
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Apr 22, 2024
1 parent 43abe1b commit 5d04eea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ledger/store/src/helpers/rocksdb/internal/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> DataMap<K
Q: Serialize + ?Sized,
{
let raw_key = self.create_prefixed_key(key)?;
match self.database.get_pinned(&raw_key)? {
match self.database.get_pinned_opt(&raw_key, &self.database.default_readopts)? {
Some(data) => Ok(Some(data)),
None => Ok(None),
}
Expand Down
19 changes: 18 additions & 1 deletion ledger/store/src/helpers/rocksdb/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ pub trait Database {
}

/// An instance of a RocksDB database.
#[derive(Clone)]
pub struct RocksDB {
/// The RocksDB instance.
rocksdb: Arc<rocksdb::DB>,
Expand All @@ -91,6 +90,22 @@ pub struct RocksDB {
pub(super) atomic_depth: Arc<AtomicUsize>,
/// A flag indicating whether the atomic writes are currently paused.
pub(super) atomic_writes_paused: Arc<AtomicBool>,
/// This is an optimization that avoids some allocations when querying the database.
pub(super) default_readopts: rocksdb::ReadOptions,
}

impl Clone for RocksDB {
fn clone(&self) -> Self {
Self {
rocksdb: self.rocksdb.clone(),
network_id: self.network_id,
storage_mode: self.storage_mode.clone(),
atomic_batch: self.atomic_batch.clone(),
atomic_depth: self.atomic_depth.clone(),
atomic_writes_paused: self.atomic_writes_paused.clone(),
default_readopts: Default::default(),
}
}
}

impl Deref for RocksDB {
Expand Down Expand Up @@ -136,6 +151,7 @@ impl Database for RocksDB {
atomic_batch: Default::default(),
atomic_depth: Default::default(),
atomic_writes_paused: Default::default(),
default_readopts: Default::default(),
})
})?
.clone();
Expand Down Expand Up @@ -309,6 +325,7 @@ impl RocksDB {
atomic_batch: Default::default(),
atomic_depth: Default::default(),
atomic_writes_paused: Default::default(),
default_readopts: Default::default(),
})
}?;

Expand Down
2 changes: 1 addition & 1 deletion ledger/store/src/helpers/rocksdb/internal/nested_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<M: Serialize + DeserializeOwned, K: Serialize + DeserializeOwned, V: Serial
#[inline]
fn get_map_key_raw(&self, map: &M, key: &K) -> Result<Option<rocksdb::DBPinnableSlice>> {
let raw_map_key = self.create_prefixed_map_key(map, key)?;
match self.database.get_pinned(&raw_map_key)? {
match self.database.get_pinned_opt(&raw_map_key, &self.database.default_readopts)? {
Some(data) => Ok(Some(data)),
None => Ok(None),
}
Expand Down

0 comments on commit 5d04eea

Please sign in to comment.