From 5d04eea691808038edb51015abb72c2874447030 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Mon, 22 Apr 2024 18:26:38 +0200 Subject: [PATCH] perf: don't recreate ReadOptions when querying the database Signed-off-by: ljedrz --- .../store/src/helpers/rocksdb/internal/map.rs | 2 +- .../store/src/helpers/rocksdb/internal/mod.rs | 19 ++++++++++++++++++- .../helpers/rocksdb/internal/nested_map.rs | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ledger/store/src/helpers/rocksdb/internal/map.rs b/ledger/store/src/helpers/rocksdb/internal/map.rs index f2f86fff93..58c97f3827 100644 --- a/ledger/store/src/helpers/rocksdb/internal/map.rs +++ b/ledger/store/src/helpers/rocksdb/internal/map.rs @@ -535,7 +535,7 @@ impl DataMap Ok(Some(data)), None => Ok(None), } diff --git a/ledger/store/src/helpers/rocksdb/internal/mod.rs b/ledger/store/src/helpers/rocksdb/internal/mod.rs index 20222de928..a30b06d71b 100644 --- a/ledger/store/src/helpers/rocksdb/internal/mod.rs +++ b/ledger/store/src/helpers/rocksdb/internal/mod.rs @@ -75,7 +75,6 @@ pub trait Database { } /// An instance of a RocksDB database. -#[derive(Clone)] pub struct RocksDB { /// The RocksDB instance. rocksdb: Arc, @@ -91,6 +90,22 @@ pub struct RocksDB { pub(super) atomic_depth: Arc, /// A flag indicating whether the atomic writes are currently paused. pub(super) atomic_writes_paused: Arc, + /// 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 { @@ -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(); @@ -309,6 +325,7 @@ impl RocksDB { atomic_batch: Default::default(), atomic_depth: Default::default(), atomic_writes_paused: Default::default(), + default_readopts: Default::default(), }) }?; diff --git a/ledger/store/src/helpers/rocksdb/internal/nested_map.rs b/ledger/store/src/helpers/rocksdb/internal/nested_map.rs index a1c6dda8a6..6fc86c4ea9 100644 --- a/ledger/store/src/helpers/rocksdb/internal/nested_map.rs +++ b/ledger/store/src/helpers/rocksdb/internal/nested_map.rs @@ -75,7 +75,7 @@ impl Result> { 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), }