Skip to content

Commit d59a518

Browse files
committed
Use fixed preallocation instead of costly GetSerializeSize
Dbwrapper used GetSerializeSize() to compute the size of the buffer to preallocate. For some cases (specifically: CCoins) this requires a costly compression call. Avoid this by just using fixed size preallocations instead.
1 parent 25a211a commit d59a518

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/dbwrapper.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <leveldb/db.h>
1818
#include <leveldb/write_batch.h>
1919

20+
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
21+
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
22+
2023
class dbwrapper_error : public std::runtime_error
2124
{
2225
public:
@@ -60,12 +63,12 @@ class CDBBatch
6063
void Write(const K& key, const V& value)
6164
{
6265
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
63-
ssKey.reserve(GetSerializeSize(ssKey, key));
66+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
6467
ssKey << key;
6568
leveldb::Slice slKey(&ssKey[0], ssKey.size());
6669

6770
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
68-
ssValue.reserve(GetSerializeSize(ssValue, value));
71+
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
6972
ssValue << value;
7073
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
7174
leveldb::Slice slValue(&ssValue[0], ssValue.size());
@@ -77,7 +80,7 @@ class CDBBatch
7780
void Erase(const K& key)
7881
{
7982
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
80-
ssKey.reserve(GetSerializeSize(ssKey, key));
83+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
8184
ssKey << key;
8285
leveldb::Slice slKey(&ssKey[0], ssKey.size());
8386

@@ -107,7 +110,7 @@ class CDBIterator
107110

108111
template<typename K> void Seek(const K& key) {
109112
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
110-
ssKey.reserve(GetSerializeSize(ssKey, key));
113+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
111114
ssKey << key;
112115
leveldb::Slice slKey(&ssKey[0], ssKey.size());
113116
piter->Seek(slKey);
@@ -200,7 +203,7 @@ class CDBWrapper
200203
bool Read(const K& key, V& value) const
201204
{
202205
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
203-
ssKey.reserve(GetSerializeSize(ssKey, key));
206+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
204207
ssKey << key;
205208
leveldb::Slice slKey(&ssKey[0], ssKey.size());
206209

@@ -234,7 +237,7 @@ class CDBWrapper
234237
bool Exists(const K& key) const
235238
{
236239
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
237-
ssKey.reserve(GetSerializeSize(ssKey, key));
240+
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
238241
ssKey << key;
239242
leveldb::Slice slKey(&ssKey[0], ssKey.size());
240243

0 commit comments

Comments
 (0)