refactor: Delegate to LevelDB for CDBBatch size estimation

Serialized batch size can be queried via the underlying LevelDB implementation calling the native `leveldb::WriteBatch::ApproximateSize()`.

The previous manual calculation was added in e66dbde6d1 as part of https://github.com/bitcoin/bitcoin/pull/10195. At that time (April 2017), the version of LevelDB used by Bitcoin Core (and even the latest source) lacked a native function for this. LevelDB added this capability in 69e2bd224b, merged later that year.

The old manual estimation method (`SizeEstimate()`) is kept temporarily in this commit, and assertions are added in `txdb.cpp` to verify its results against `ApproximateSize()` during batch writes. This ensures the native function behaves as expected before removing the manual calculation in the subsequent commit.
This commit is contained in:
Lőrinc
2025-04-01 14:06:10 +02:00
parent 751077c6e2
commit 8b5e19d8b5
3 changed files with 28 additions and 3 deletions

View File

@@ -200,6 +200,11 @@ void CDBBatch::EraseImpl(std::span<const std::byte> key)
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
}
size_t CDBBatch::ApproximateSize() const
{
return m_impl_batch->batch.ApproximateSize();
}
struct LevelDBContext {
//! custom environment this database is using (may be nullptr in case of default environment)
leveldb::Env* penv;