Batch construct batches

Reuse the serialization buffers used for constructing the LevelDB
write batch. This avoids 2 allocations per utxo write.
This commit is contained in:
Pieter Wuille 2016-12-13 23:53:39 -08:00
parent 26fe5c98ab
commit ed5896976a

View File

@ -53,38 +53,41 @@ private:
const CDBWrapper &parent; const CDBWrapper &parent;
leveldb::WriteBatch batch; leveldb::WriteBatch batch;
CDataStream ssKey;
CDataStream ssValue;
public: public:
/** /**
* @param[in] _parent CDBWrapper that this batch is to be submitted to * @param[in] _parent CDBWrapper that this batch is to be submitted to
*/ */
CDBBatch(const CDBWrapper &_parent) : parent(_parent) { }; CDBBatch(const CDBWrapper &_parent) : parent(_parent), ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION) { };
template <typename K, typename V> template <typename K, typename V>
void Write(const K& key, const V& value) void Write(const K& key, const V& value)
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value; ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(&ssValue[0], ssValue.size()); leveldb::Slice slValue(&ssValue[0], ssValue.size());
batch.Put(slKey, slValue); batch.Put(slKey, slValue);
ssKey.clear();
ssValue.clear();
} }
template <typename K> template <typename K>
void Erase(const K& key) void Erase(const K& key)
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
batch.Delete(slKey); batch.Delete(slKey);
ssKey.clear();
} }
}; };