Skip to content

Commit

Permalink
Use fixed preallocation instead of costly GetSerializeSize
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sipa committed Nov 7, 2016
1 parent 25a211a commit d59a518
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/dbwrapper.h
Expand Up @@ -17,6 +17,9 @@
#include <leveldb/db.h>
#include <leveldb/write_batch.h>

static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;

class dbwrapper_error : public std::runtime_error
{
public:
Expand Down Expand Up @@ -60,12 +63,12 @@ class CDBBatch
void Write(const K& key, const V& value)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(GetSerializeSize(ssValue, value));
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(&ssValue[0], ssValue.size());
Expand All @@ -77,7 +80,7 @@ class CDBBatch
void Erase(const K& key)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

Expand Down Expand Up @@ -107,7 +110,7 @@ class CDBIterator

template<typename K> void Seek(const K& key) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());
piter->Seek(slKey);
Expand Down Expand Up @@ -200,7 +203,7 @@ class CDBWrapper
bool Read(const K& key, V& value) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

Expand Down Expand Up @@ -234,7 +237,7 @@ class CDBWrapper
bool Exists(const K& key) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

Expand Down

0 comments on commit d59a518

Please sign in to comment.