Skip to content

Commit

Permalink
Merge bitcoin#9039: Various serialization simplifcations and optimiza…
Browse files Browse the repository at this point in the history
…tions

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)
  • Loading branch information
laanwj authored and CryptoCentric committed Feb 24, 2019
1 parent 8556322 commit 5185fb9
Show file tree
Hide file tree
Showing 36 changed files with 383 additions and 510 deletions.
2 changes: 1 addition & 1 deletion src/addrdb.h
Expand Up @@ -46,7 +46,7 @@ class CBanEntry
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);

READWRITE(nCreateTime);
Expand Down
11 changes: 3 additions & 8 deletions src/addrman.h
Expand Up @@ -56,7 +56,7 @@ class CAddrInfo : public CAddress
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*(CAddress*)this);
READWRITE(source);
READWRITE(nLastSuccess);
Expand Down Expand Up @@ -289,7 +289,7 @@ class CAddrMan
* very little in common.
*/
template<typename Stream>
void Serialize(Stream &s, int nType, int nVersionDummy) const
void Serialize(Stream &s) const
{
LOCK(cs);

Expand Down Expand Up @@ -339,7 +339,7 @@ class CAddrMan
}

template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersionDummy)
void Unserialize(Stream& s)
{
LOCK(cs);

Expand Down Expand Up @@ -444,11 +444,6 @@ class CAddrMan
Check();
}

unsigned int GetSerializeSize(int nType, int nVersion) const
{
return (CSizeComputer(nType, nVersion) << *this).size();
}

void Clear()
{
std::vector<int>().swap(vRandom);
Expand Down
2 changes: 1 addition & 1 deletion src/amount.h
Expand Up @@ -64,7 +64,7 @@ class CFeeRate
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nSatoshisPerK);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/bloom.h
Expand Up @@ -73,7 +73,7 @@ class CBloomFilter
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(vData);
READWRITE(nHashFuncs);
READWRITE(nTweak);
Expand Down
9 changes: 5 additions & 4 deletions src/chain.h
Expand Up @@ -28,7 +28,7 @@ class CBlockFileInfo
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nBlocks));
READWRITE(VARINT(nSize));
READWRITE(VARINT(nUndoSize));
Expand Down Expand Up @@ -76,7 +76,7 @@ struct CDiskBlockPos
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nFile));
READWRITE(VARINT(nPos));
}
Expand Down Expand Up @@ -358,8 +358,9 @@ class CDiskBlockIndex : public CBlockIndex
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH))
inline void SerializationOp(Stream& s, Operation ser_action) {
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH))
READWRITE(VARINT(nVersion));

READWRITE(VARINT(nHeight));
Expand Down
12 changes: 6 additions & 6 deletions src/coins.h
Expand Up @@ -56,20 +56,20 @@ class Coin
}

template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const {
void Serialize(Stream &s) const {
assert(!IsSpent());
uint32_t code = nHeight * 2 + fCoinBase;
::Serialize(s, VARINT(code), nType, nVersion);
::Serialize(s, CTxOutCompressor(REF(out)), nType, nVersion);
::Serialize(s, VARINT(code));
::Serialize(s, CTxOutCompressor(REF(out)));
}

template<typename Stream>
void Unserialize(Stream &s, int nType, int nVersion) {
void Unserialize(Stream &s) {
uint32_t code = 0;
::Unserialize(s, VARINT(code), nType, nVersion);
::Unserialize(s, VARINT(code));
nHeight = code >> 1;
fCoinBase = code & 1;
::Unserialize(s, REF(CTxOutCompressor(out)), nType, nVersion);
::Unserialize(s, REF(CTxOutCompressor(out)));
}

bool IsSpent() const {
Expand Down
14 changes: 3 additions & 11 deletions src/compressor.h
Expand Up @@ -55,16 +55,8 @@ class CScriptCompressor
public:
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }

unsigned int GetSerializeSize(int nType, int nVersion) const {
std::vector<unsigned char> compr;
if (Compress(compr))
return compr.size();
unsigned int nSize = script.size() + nSpecialScripts;
return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
}

template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const {
void Serialize(Stream &s) const {
std::vector<unsigned char> compr;
if (Compress(compr)) {
s << CFlatData(compr);
Expand All @@ -76,7 +68,7 @@ class CScriptCompressor
}

template<typename Stream>
void Unserialize(Stream &s, int nType, int nVersion) {
void Unserialize(Stream &s) {
unsigned int nSize = 0;
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
Expand Down Expand Up @@ -112,7 +104,7 @@ class CTxOutCompressor
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
if (!ser_action.ForRead()) {
uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal));
Expand Down
14 changes: 8 additions & 6 deletions src/hash.h
Expand Up @@ -240,15 +240,17 @@ class CHashWriter
private:
CHash256 ctx;

const int nType;
const int nVersion;
public:
int nType;
int nVersion;

CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}

CHashWriter& write(const char *pch, size_t size) {
int GetType() const { return nType; }
int GetVersion() const { return nVersion; }

void write(const char *pch, size_t size) {
ctx.Write((const unsigned char*)pch, size);
return (*this);
}

// invalidates the object
Expand All @@ -261,7 +263,7 @@ class CHashWriter
template<typename T>
CHashWriter& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj, nType, nVersion);
::Serialize(*this, obj);
return (*this);
}
};
Expand Down Expand Up @@ -296,7 +298,7 @@ class CHashVerifier : public CHashWriter
CHashVerifier<Source>& operator>>(T& obj)
{
// Unserialize from this stream
::Unserialize(*this, obj, nType, nVersion);
::Unserialize(*this, obj);
return (*this);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/key.h
Expand Up @@ -163,7 +163,7 @@ struct CExtKey {
CExtPubKey Neuter() const;
void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
template <typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const
void Serialize(Stream& s) const
{
unsigned int len = BIP32_EXTKEY_SIZE;
::WriteCompactSize(s, len);
Expand All @@ -172,7 +172,7 @@ struct CExtKey {
s.write((const char *)&code[0], len);
}
template <typename Stream>
void Unserialize(Stream& s, int nType, int nVersion)
void Unserialize(Stream& s)
{
unsigned int len = ::ReadCompactSize(s);
unsigned char code[BIP32_EXTKEY_SIZE];
Expand Down
4 changes: 2 additions & 2 deletions src/merkleblock.h
Expand Up @@ -85,7 +85,7 @@ class CPartialMerkleTree
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nTransactions);
READWRITE(vHash);
std::vector<unsigned char> vBytes;
Expand Down Expand Up @@ -148,7 +148,7 @@ class CMerkleBlock
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(header);
READWRITE(txn);
}
Expand Down
2 changes: 1 addition & 1 deletion src/net.h
Expand Up @@ -184,7 +184,7 @@ class CConnman
void PushMessageWithVersionAndFlag(CNode* pnode, int nVersion, int flag, const std::string& sCommand, Args&&... args)
{
auto msg(BeginMessage(pnode, nVersion, flag, sCommand));
::SerializeMany(msg, msg.nType, msg.nVersion, std::forward<Args>(args)...);
::SerializeMany(msg, std::forward<Args>(args)...);
EndMessage(msg);
PushMessage(pnode, msg, sCommand);
}
Expand Down
6 changes: 3 additions & 3 deletions src/netaddress.h
Expand Up @@ -87,7 +87,7 @@ class CNetAddr
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(FLATDATA(ip));
}

Expand Down Expand Up @@ -124,7 +124,7 @@ class CSubNet
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(network);
READWRITE(FLATDATA(netmask));
READWRITE(FLATDATA(valid));
Expand Down Expand Up @@ -161,7 +161,7 @@ class CService : public CNetAddr
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(FLATDATA(ip));
unsigned short portN = htons(port);
READWRITE(FLATDATA(portN));
Expand Down
9 changes: 5 additions & 4 deletions src/primitives/block.h
Expand Up @@ -38,7 +38,7 @@ class CBlockHeader
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);

READWRITE(hashPrevBlock);
Expand Down Expand Up @@ -97,7 +97,7 @@ class CBlock : public CBlockHeader
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*(CBlockHeader*)this);
READWRITE(vtx);
}
Expand Down Expand Up @@ -145,8 +145,9 @@ struct CBlockLocator
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH))
inline void SerializationOp(Stream& s, Operation ser_action) {
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH))
READWRITE(nVersion);
READWRITE(vHave);
}
Expand Down
12 changes: 6 additions & 6 deletions src/primitives/transaction.h
Expand Up @@ -24,7 +24,7 @@ class COutPoint
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(hash);
READWRITE(n);
}
Expand Down Expand Up @@ -101,7 +101,7 @@ class CTxIn
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout);
READWRITE(*(CScriptBase*)(&scriptSig));
READWRITE(nSequence);
Expand Down Expand Up @@ -147,7 +147,7 @@ class CTxOut
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nValue);
READWRITE(*(CScriptBase*)(&scriptPubKey));
}
Expand Down Expand Up @@ -176,7 +176,7 @@ class CTxOut
if (scriptPubKey.IsUnspendable())
return 0;

size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
size_t nSize = GetSerializeSize(*this, SER_DISK, 0)+148u;
return 3*minRelayTxFee.GetFee(nSize);
}

Expand Down Expand Up @@ -243,7 +243,7 @@ class CTransaction
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*const_cast<int32_t*>(&this->nVersion));
nVersion = this->nVersion;
READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
Expand Down Expand Up @@ -311,7 +311,7 @@ struct CMutableTransaction
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(vin);
Expand Down
13 changes: 7 additions & 6 deletions src/protocol.h
Expand Up @@ -49,7 +49,7 @@ class CMessageHeader
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(FLATDATA(pchMessageStart));
READWRITE(FLATDATA(pchCommand));
Expand Down Expand Up @@ -294,14 +294,15 @@ class CAddress : public CService
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
inline void SerializationOp(Stream& s, Operation ser_action)
{
if (ser_action.ForRead())
Init();
if (nType & SER_DISK)
int nVersion = s.GetVersion();
if (s.GetType() & SER_DISK)
READWRITE(nVersion);
if ((nType & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
if ((s.GetType() & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
READWRITE(nTime);
uint64_t nServicesInt = nServices;
READWRITE(nServicesInt);
Expand Down Expand Up @@ -358,7 +359,7 @@ class CInv
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(type);
READWRITE(hash);
Expand Down

0 comments on commit 5185fb9

Please sign in to comment.