Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/storage/libmdbx-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ reth-mdbx-sys.workspace = true
bitflags.workspace = true
byteorder.workspace = true
derive_more.workspace = true
indexmap.workspace = true
parking_lot.workspace = true
smallvec.workspace = true
thiserror.workspace = true
Expand Down
2 changes: 0 additions & 2 deletions crates/storage/libmdbx-rs/benches/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ fn bench_put_rand(c: &mut Criterion) {

let txn = env.begin_ro_txn().unwrap();
let db = txn.open_db(None).unwrap();
txn.prime_for_permaopen(db);
let db = txn.commit_and_rebind_open_dbs().unwrap().2.remove(0);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only used in a benchmark, which is a no-op anyway (db is the same after these 2 lines with the same DBI and environment pointer).


let mut items: Vec<(String, String)> = (0..n).map(|n| (get_key(n), get_data(n))).collect();
items.shuffle(&mut StdRng::from_seed(Default::default()));
Expand Down
61 changes: 16 additions & 45 deletions crates/storage/libmdbx-rs/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
Cursor, Error, Stat, TableObject,
};
use ffi::{MDBX_txn_flags_t, MDBX_TXN_RDONLY, MDBX_TXN_READWRITE};
use indexmap::IndexSet;
use parking_lot::{Mutex, MutexGuard};
use std::{
ffi::{c_uint, c_void},
Expand Down Expand Up @@ -94,7 +93,6 @@ where

let inner = TransactionInner {
txn,
primed_dbis: Mutex::new(IndexSet::new()),
committed: AtomicBool::new(false),
env,
_marker: Default::default(),
Expand Down Expand Up @@ -173,50 +171,25 @@ where
///
/// Any pending operations will be saved.
pub fn commit(self) -> Result<(bool, CommitLatency)> {
self.commit_and_rebind_open_dbs().map(|v| (v.0, v.1))
}

pub fn prime_for_permaopen(&self, db: Database) {
self.inner.primed_dbis.lock().insert(db.dbi());
}
let result = self.txn_execute(|txn| {
if K::IS_READ_ONLY {
#[cfg(feature = "read-tx-timeouts")]
self.env().txn_manager().remove_active_read_transaction(txn);

/// Commits the transaction and returns table handles permanently open until dropped.
pub fn commit_and_rebind_open_dbs(self) -> Result<(bool, CommitLatency, Vec<Database>)> {
let result = {
let result = self.txn_execute(|txn| {
if K::IS_READ_ONLY {
#[cfg(feature = "read-tx-timeouts")]
self.env().txn_manager().remove_active_read_transaction(txn);

let mut latency = CommitLatency::new();
mdbx_result(unsafe {
ffi::mdbx_txn_commit_ex(txn, latency.mdb_commit_latency())
})
let mut latency = CommitLatency::new();
mdbx_result(unsafe { ffi::mdbx_txn_commit_ex(txn, latency.mdb_commit_latency()) })
.map(|v| (v, latency))
} else {
let (sender, rx) = sync_channel(0);
self.env()
.txn_manager()
.send_message(TxnManagerMessage::Commit { tx: TxnPtr(txn), sender });
rx.recv().unwrap()
}
})?;
} else {
let (sender, rx) = sync_channel(0);
self.env()
.txn_manager()
.send_message(TxnManagerMessage::Commit { tx: TxnPtr(txn), sender });
rx.recv().unwrap()
}
})?;

self.inner.set_committed();
result
};
result.map(|(v, latency)| {
(
v,
latency,
self.inner
.primed_dbis
.lock()
.iter()
.map(|&dbi| Database::new_from_ptr(dbi, self.env().clone()))
.collect(),
)
})
self.inner.set_committed();
result
}

/// Opens a handle to an MDBX database.
Expand Down Expand Up @@ -308,8 +281,6 @@ where
{
/// The transaction pointer itself.
txn: TransactionPtr,
/// A set of database handles that are primed for permaopen.
Copy link
Copy Markdown
Contributor Author

@hai-rise hai-rise Sep 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very questionable description: Tables are "pemaopen" by default, and storing a DBI here doesn't have any effect on that. In fact, it's still possible to close the table handles elsewhere.

Also, instead of arbitrarily calling prime_for_permaopen in a lockful way at runtime (without a key to look up anyway), DatabaseEnv should create tables and cache all DBIs at startup time, before passing onto transactions to read in a lockless way.

primed_dbis: Mutex<IndexSet<ffi::MDBX_dbi>>,
/// Whether the transaction has committed.
committed: AtomicBool,
env: Environment,
Expand Down
Loading