Skip to content

Commit

Permalink
Batch construct batches
Browse files Browse the repository at this point in the history
Reuse the serialization buffers used for constructing the LevelDB
write batch. This avoids 2 allocations per utxo write.
  • Loading branch information
sipa authored and dagurval committed Dec 18, 2017
1 parent facf88b commit a5d169b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,37 @@ class CDBBatch
private:
leveldb::WriteBatch batch;

CDataStream ssKey;
CDataStream ssValue;

public:
CDBBatch() : ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION) { };

template <typename K, typename V>
void Write(const K& key, const V& value)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value;
leveldb::Slice slValue(&ssValue[0], ssValue.size());

batch.Put(slKey, slValue);
ssKey.clear();
ssValue.clear();
}

template <typename K>
void Erase(const K& key)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size());

batch.Delete(slKey);
ssKey.clear();
}
};

Expand Down

0 comments on commit a5d169b

Please sign in to comment.