Skip to content

Commit e81df49

Browse files
committed
Merge #9039: Various serialization simplifcations and optimizations
d59a518 Use fixed preallocation instead of costly GetSerializeSize (Pieter Wuille) 25a211a Add optimized CSizeComputer serializers (Pieter Wuille) a2929a2 Make CSerAction's ForRead() constexpr (Pieter Wuille) a603925 Avoid -Wshadow errors (Pieter Wuille) 5284721 Get rid of nType and nVersion (Pieter Wuille) 657e05a Make GetSerializeSize a wrapper on top of CSizeComputer (Pieter Wuille) fad9b66 Make nType and nVersion private and sometimes const (Pieter Wuille) c2c5d42 Make streams' read and write return void (Pieter Wuille) 50e8a9c Remove unused ReadVersion and WriteVersion (Pieter Wuille)
2 parents e984730 + d59a518 commit e81df49

39 files changed

+409
-543
lines changed

src/addrdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CBanEntry
4646
ADD_SERIALIZE_METHODS;
4747

4848
template <typename Stream, typename Operation>
49-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
49+
inline void SerializationOp(Stream& s, Operation ser_action) {
5050
READWRITE(this->nVersion);
5151
READWRITE(nCreateTime);
5252
READWRITE(nBanUntil);

src/addrman.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CAddrInfo : public CAddress
5858
ADD_SERIALIZE_METHODS;
5959

6060
template <typename Stream, typename Operation>
61-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
61+
inline void SerializationOp(Stream& s, Operation ser_action) {
6262
READWRITE(*(CAddress*)this);
6363
READWRITE(source);
6464
READWRITE(nLastSuccess);
@@ -293,7 +293,7 @@ class CAddrMan
293293
* very little in common.
294294
*/
295295
template<typename Stream>
296-
void Serialize(Stream &s, int nType, int nVersionDummy) const
296+
void Serialize(Stream &s) const
297297
{
298298
LOCK(cs);
299299

@@ -343,7 +343,7 @@ class CAddrMan
343343
}
344344

345345
template<typename Stream>
346-
void Unserialize(Stream& s, int nType, int nVersionDummy)
346+
void Unserialize(Stream& s)
347347
{
348348
LOCK(cs);
349349

@@ -448,11 +448,6 @@ class CAddrMan
448448
Check();
449449
}
450450

451-
unsigned int GetSerializeSize(int nType, int nVersion) const
452-
{
453-
return (CSizeComputer(nType, nVersion) << *this).size();
454-
}
455-
456451
void Clear()
457452
{
458453
std::vector<int>().swap(vRandom);

src/amount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CFeeRate
6464
ADD_SERIALIZE_METHODS;
6565

6666
template <typename Stream, typename Operation>
67-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
67+
inline void SerializationOp(Stream& s, Operation ser_action) {
6868
READWRITE(nSatoshisPerK);
6969
}
7070
};

src/blockencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
131131
break;
132132
}
133133

134-
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), cmpctblock.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION));
134+
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION));
135135

136136
return READ_STATUS_OK;
137137
}

src/blockencodings.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct TransactionCompressor {
2121
ADD_SERIALIZE_METHODS;
2222

2323
template <typename Stream, typename Operation>
24-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
24+
inline void SerializationOp(Stream& s, Operation ser_action) {
2525
READWRITE(tx); //TODO: Compress tx encoding
2626
}
2727
};
@@ -35,7 +35,7 @@ class BlockTransactionsRequest {
3535
ADD_SERIALIZE_METHODS;
3636

3737
template <typename Stream, typename Operation>
38-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
38+
inline void SerializationOp(Stream& s, Operation ser_action) {
3939
READWRITE(blockhash);
4040
uint64_t indexes_size = (uint64_t)indexes.size();
4141
READWRITE(COMPACTSIZE(indexes_size));
@@ -81,7 +81,7 @@ class BlockTransactions {
8181
ADD_SERIALIZE_METHODS;
8282

8383
template <typename Stream, typename Operation>
84-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
84+
inline void SerializationOp(Stream& s, Operation ser_action) {
8585
READWRITE(blockhash);
8686
uint64_t txn_size = (uint64_t)txn.size();
8787
READWRITE(COMPACTSIZE(txn_size));
@@ -109,7 +109,7 @@ struct PrefilledTransaction {
109109
ADD_SERIALIZE_METHODS;
110110

111111
template <typename Stream, typename Operation>
112-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
112+
inline void SerializationOp(Stream& s, Operation ser_action) {
113113
uint64_t idx = index;
114114
READWRITE(COMPACTSIZE(idx));
115115
if (idx > std::numeric_limits<uint16_t>::max())
@@ -157,7 +157,7 @@ class CBlockHeaderAndShortTxIDs {
157157
ADD_SERIALIZE_METHODS;
158158

159159
template <typename Stream, typename Operation>
160-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
160+
inline void SerializationOp(Stream& s, Operation ser_action) {
161161
READWRITE(header);
162162
READWRITE(nonce);
163163

src/bloom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CBloomFilter
7373
ADD_SERIALIZE_METHODS;
7474

7575
template <typename Stream, typename Operation>
76-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
76+
inline void SerializationOp(Stream& s, Operation ser_action) {
7777
READWRITE(vData);
7878
READWRITE(nHashFuncs);
7979
READWRITE(nTweak);

src/chain.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CBlockFileInfo
2828
ADD_SERIALIZE_METHODS;
2929

3030
template <typename Stream, typename Operation>
31-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
31+
inline void SerializationOp(Stream& s, Operation ser_action) {
3232
READWRITE(VARINT(nBlocks));
3333
READWRITE(VARINT(nSize));
3434
READWRITE(VARINT(nUndoSize));
@@ -76,7 +76,7 @@ struct CDiskBlockPos
7676
ADD_SERIALIZE_METHODS;
7777

7878
template <typename Stream, typename Operation>
79-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
79+
inline void SerializationOp(Stream& s, Operation ser_action) {
8080
READWRITE(VARINT(nFile));
8181
READWRITE(VARINT(nPos));
8282
}
@@ -357,8 +357,9 @@ class CDiskBlockIndex : public CBlockIndex
357357
ADD_SERIALIZE_METHODS;
358358

359359
template <typename Stream, typename Operation>
360-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
361-
if (!(nType & SER_GETHASH))
360+
inline void SerializationOp(Stream& s, Operation ser_action) {
361+
int nVersion = s.GetVersion();
362+
if (!(s.GetType() & SER_GETHASH))
362363
READWRITE(VARINT(nVersion));
363364

364365
READWRITE(VARINT(nHeight));

src/coins.h

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -153,65 +153,42 @@ class CCoins
153153
return fCoinBase;
154154
}
155155

156-
unsigned int GetSerializeSize(int nType, int nVersion) const {
157-
unsigned int nSize = 0;
158-
unsigned int nMaskSize = 0, nMaskCode = 0;
159-
CalcMaskSize(nMaskSize, nMaskCode);
160-
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
161-
bool fSecond = vout.size() > 1 && !vout[1].IsNull();
162-
assert(fFirst || fSecond || nMaskCode);
163-
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
164-
// version
165-
nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
166-
// size of header code
167-
nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
168-
// spentness bitmask
169-
nSize += nMaskSize;
170-
// txouts themself
171-
for (unsigned int i = 0; i < vout.size(); i++)
172-
if (!vout[i].IsNull())
173-
nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
174-
// height
175-
nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
176-
return nSize;
177-
}
178-
179156
template<typename Stream>
180-
void Serialize(Stream &s, int nType, int nVersion) const {
157+
void Serialize(Stream &s) const {
181158
unsigned int nMaskSize = 0, nMaskCode = 0;
182159
CalcMaskSize(nMaskSize, nMaskCode);
183160
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
184161
bool fSecond = vout.size() > 1 && !vout[1].IsNull();
185162
assert(fFirst || fSecond || nMaskCode);
186163
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
187164
// version
188-
::Serialize(s, VARINT(this->nVersion), nType, nVersion);
165+
::Serialize(s, VARINT(this->nVersion));
189166
// header code
190-
::Serialize(s, VARINT(nCode), nType, nVersion);
167+
::Serialize(s, VARINT(nCode));
191168
// spentness bitmask
192169
for (unsigned int b = 0; b<nMaskSize; b++) {
193170
unsigned char chAvail = 0;
194171
for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
195172
if (!vout[2+b*8+i].IsNull())
196173
chAvail |= (1 << i);
197-
::Serialize(s, chAvail, nType, nVersion);
174+
::Serialize(s, chAvail);
198175
}
199176
// txouts themself
200177
for (unsigned int i = 0; i < vout.size(); i++) {
201178
if (!vout[i].IsNull())
202-
::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
179+
::Serialize(s, CTxOutCompressor(REF(vout[i])));
203180
}
204181
// coinbase height
205-
::Serialize(s, VARINT(nHeight), nType, nVersion);
182+
::Serialize(s, VARINT(nHeight));
206183
}
207184

208185
template<typename Stream>
209-
void Unserialize(Stream &s, int nType, int nVersion) {
186+
void Unserialize(Stream &s) {
210187
unsigned int nCode = 0;
211188
// version
212-
::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
189+
::Unserialize(s, VARINT(this->nVersion));
213190
// header code
214-
::Unserialize(s, VARINT(nCode), nType, nVersion);
191+
::Unserialize(s, VARINT(nCode));
215192
fCoinBase = nCode & 1;
216193
std::vector<bool> vAvail(2, false);
217194
vAvail[0] = (nCode & 2) != 0;
@@ -220,7 +197,7 @@ class CCoins
220197
// spentness bitmask
221198
while (nMaskCode > 0) {
222199
unsigned char chAvail = 0;
223-
::Unserialize(s, chAvail, nType, nVersion);
200+
::Unserialize(s, chAvail);
224201
for (unsigned int p = 0; p < 8; p++) {
225202
bool f = (chAvail & (1 << p)) != 0;
226203
vAvail.push_back(f);
@@ -232,10 +209,10 @@ class CCoins
232209
vout.assign(vAvail.size(), CTxOut());
233210
for (unsigned int i = 0; i < vAvail.size(); i++) {
234211
if (vAvail[i])
235-
::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
212+
::Unserialize(s, REF(CTxOutCompressor(vout[i])));
236213
}
237214
// coinbase height
238-
::Unserialize(s, VARINT(nHeight), nType, nVersion);
215+
::Unserialize(s, VARINT(nHeight));
239216
Cleanup();
240217
}
241218

src/compressor.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,8 @@ class CScriptCompressor
5555
public:
5656
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
5757

58-
unsigned int GetSerializeSize(int nType, int nVersion) const {
59-
std::vector<unsigned char> compr;
60-
if (Compress(compr))
61-
return compr.size();
62-
unsigned int nSize = script.size() + nSpecialScripts;
63-
return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
64-
}
65-
6658
template<typename Stream>
67-
void Serialize(Stream &s, int nType, int nVersion) const {
59+
void Serialize(Stream &s) const {
6860
std::vector<unsigned char> compr;
6961
if (Compress(compr)) {
7062
s << CFlatData(compr);
@@ -76,7 +68,7 @@ class CScriptCompressor
7668
}
7769

7870
template<typename Stream>
79-
void Unserialize(Stream &s, int nType, int nVersion) {
71+
void Unserialize(Stream &s) {
8072
unsigned int nSize = 0;
8173
s >> VARINT(nSize);
8274
if (nSize < nSpecialScripts) {
@@ -112,7 +104,7 @@ class CTxOutCompressor
112104
ADD_SERIALIZE_METHODS;
113105

114106
template <typename Stream, typename Operation>
115-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
107+
inline void SerializationOp(Stream& s, Operation ser_action) {
116108
if (!ser_action.ForRead()) {
117109
uint64_t nVal = CompressAmount(txout.nValue);
118110
READWRITE(VARINT(nVal));

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(ssKey.GetSerializeSize(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(ssValue.GetSerializeSize(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(ssKey.GetSerializeSize(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(ssKey.GetSerializeSize(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(ssKey.GetSerializeSize(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(ssKey.GetSerializeSize(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)