Skip to content

Commit e419b0e

Browse files
l0rinclaanwj
andcommitted
refactor: Remove manual CDBBatch size estimation
Remove the manual batch size estimation logic (`SizeEstimate()` method and `size_estimate` member) from `CDBBatch`. Size is now determined solely by the `ApproximateSize()` method introduced in the previous commit, which delegates to the native LevelDB function. The manual calculation is no longer necessary as LevelDB now provides this functionality directly, and the previous commit verified that the native function's results matched the manual estimation. Assertions comparing the two methods are removed from `txdb.cpp`. Co-authored-by: Wladimir J. van der Laan <laanwj@protonmail.com>
1 parent 8b5e19d commit e419b0e

File tree

3 files changed

+3
-40
lines changed

3 files changed

+3
-40
lines changed

src/dbwrapper.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ CDBBatch::~CDBBatch() = default;
168168
void CDBBatch::Clear()
169169
{
170170
m_impl_batch->batch.Clear();
171-
assert(m_impl_batch->batch.ApproximateSize() == kHeader);
172-
size_estimate = kHeader; // TODO remove
173171
}
174172

175173
void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
@@ -178,26 +176,12 @@ void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
178176
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
179177
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
180178
m_impl_batch->batch.Put(slKey, slValue);
181-
// LevelDB serializes writes as:
182-
// - byte: header
183-
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
184-
// - byte[]: key
185-
// - varint: value length
186-
// - byte[]: value
187-
// The formula below assumes the key and value are both less than 16k.
188-
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
189179
}
190180

191181
void CDBBatch::EraseImpl(std::span<const std::byte> key)
192182
{
193183
leveldb::Slice slKey(CharCast(key.data()), key.size());
194184
m_impl_batch->batch.Delete(slKey);
195-
// LevelDB serializes erases as:
196-
// - byte: header
197-
// - varint: key length
198-
// - byte[]: key
199-
// The formula below assumes the key is less than 16kB.
200-
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
201185
}
202186

203187
size_t CDBBatch::ApproximateSize() const

src/dbwrapper.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class CDBBatch
7575
friend class CDBWrapper;
7676

7777
private:
78-
static constexpr size_t kHeader{12}; // See: src/leveldb/db/write_batch.cc#L27
79-
8078
const CDBWrapper &parent;
8179

8280
struct WriteBatchImpl;
@@ -85,8 +83,6 @@ class CDBBatch
8583
DataStream ssKey{};
8684
DataStream ssValue{};
8785

88-
size_t size_estimate{0};
89-
9086
void WriteImpl(std::span<const std::byte> key, DataStream& ssValue);
9187
void EraseImpl(std::span<const std::byte> key);
9288

@@ -120,7 +116,6 @@ class CDBBatch
120116
}
121117

122118
size_t ApproximateSize() const;
123-
size_t SizeEstimate() const { return size_estimate; } // TODO replace with ApproximateSize
124119
};
125120

126121
class CDBIterator

src/txdb.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,39 +113,27 @@ bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashB
113113
// transition from old_tip to hashBlock.
114114
// A vector is used for future extensibility, as we may want to support
115115
// interrupting after partial writes from multiple independent reorgs.
116-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
117116
batch.Erase(DB_BEST_BLOCK);
118-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
119117
batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
120-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
121118

122119
for (auto it{cursor.Begin()}; it != cursor.End();) {
123120
if (it->second.IsDirty()) {
124121
CoinEntry entry(&it->first);
125122
if (it->second.coin.IsSpent()) {
126-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
127123
batch.Erase(entry);
128-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
129124
} else {
130-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
131125
batch.Write(entry, it->second.coin);
132-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
133126
}
134127

135128
changed++;
136129
}
137130
count++;
138131
it = cursor.NextAndMaybeErase(*it);
139-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
140-
if (batch.SizeEstimate() > m_options.batch_write_bytes) {
141-
LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
132+
if (batch.ApproximateSize() > m_options.batch_write_bytes) {
133+
LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0));
142134

143-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
144135
m_db->WriteBatch(batch);
145-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
146136
batch.Clear();
147-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
148-
149137
if (m_options.simulate_crash_ratio) {
150138
static FastRandomContext rng;
151139
if (rng.randrange(m_options.simulate_crash_ratio) == 0) {
@@ -157,15 +145,11 @@ bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashB
157145
}
158146

159147
// In the last batch, mark the database as consistent with hashBlock again.
160-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
161148
batch.Erase(DB_HEAD_BLOCKS);
162-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
163149
batch.Write(DB_BEST_BLOCK, hashBlock);
164-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
165150

166-
LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
151+
LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0));
167152
bool ret = m_db->WriteBatch(batch);
168-
assert(batch.ApproximateSize() == batch.SizeEstimate()); // TODO remove
169153
LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
170154
return ret;
171155
}

0 commit comments

Comments
 (0)