Skip to content

Commit 172f5fa

Browse files
committed
Support deserializing into temporaries
Currently, the READWRITE macro cannot be passed any non-const temporaries, as the SerReadWrite function only accepts lvalue references. Deserializing into a temporary is very common, however. See for example things like 's >> VARINT(n)'. The VARINT macro produces a temporary wrapper that holds a reference to n. Fix this by accepting non-const rvalue references instead of lvalue references. We don't propagate the rvalue-ness down, as there are no useful optimizations that only apply to temporaries. Then use this new functionality to get rid of many (but not all) uses of the 'REF' macro (which casts away constness).
1 parent 2761bca commit 172f5fa

File tree

9 files changed

+28
-28
lines changed

9 files changed

+28
-28
lines changed

src/blockencodings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ class BlockTransactions {
9090
while (txn.size() < txn_size) {
9191
txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
9292
for (; i < txn.size(); i++)
93-
READWRITE(REF(TransactionCompressor(txn[i])));
93+
READWRITE(TransactionCompressor(txn[i]));
9494
}
9595
} else {
9696
for (size_t i = 0; i < txn.size(); i++)
97-
READWRITE(REF(TransactionCompressor(txn[i])));
97+
READWRITE(TransactionCompressor(txn[i]));
9898
}
9999
}
100100
};
@@ -115,7 +115,7 @@ struct PrefilledTransaction {
115115
if (idx > std::numeric_limits<uint16_t>::max())
116116
throw std::ios_base::failure("index overflowed 16-bits");
117117
index = idx;
118-
READWRITE(REF(TransactionCompressor(tx)));
118+
READWRITE(TransactionCompressor(tx));
119119
}
120120
};
121121

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Coin
6969
::Unserialize(s, VARINT(code));
7070
nHeight = code >> 1;
7171
fCoinBase = code & 1;
72-
::Unserialize(s, REF(CTxOutCompressor(out)));
72+
::Unserialize(s, CTxOutCompressor(out));
7373
}
7474

7575
bool IsSpent() const {

src/compressor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CScriptCompressor
7373
s >> VARINT(nSize);
7474
if (nSize < nSpecialScripts) {
7575
std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
76-
s >> REF(CFlatData(vch));
76+
s >> CFlatData(vch);
7777
Decompress(nSize, vch);
7878
return;
7979
}
@@ -84,7 +84,7 @@ class CScriptCompressor
8484
s.ignore(nSize);
8585
} else {
8686
script.resize(nSize);
87-
s >> REF(CFlatData(script));
87+
s >> CFlatData(script);
8888
}
8989
}
9090
};

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class CHashVerifier : public CHashWriter
173173
}
174174

175175
template<typename T>
176-
CHashVerifier<Source>& operator>>(T& obj)
176+
CHashVerifier<Source>& operator>>(T&& obj)
177177
{
178178
// Unserialize from this stream
179179
::Unserialize(*this, obj);

src/script/bitcoinconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TxInputStream
4040
}
4141

4242
template<typename T>
43-
TxInputStream& operator>>(T& obj)
43+
TxInputStream& operator>>(T&& obj)
4444
{
4545
::Unserialize(*this, obj);
4646
return *this;

src/serialize.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ I ReadVarInt(Stream& is)
350350
}
351351
}
352352

353-
#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
354-
#define VARINT(obj) REF(WrapVarInt(REF(obj)))
355-
#define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
356-
#define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
353+
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
354+
#define VARINT(obj) WrapVarInt(REF(obj))
355+
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
356+
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
357357

358358
/**
359359
* Wrapper for serializing arrays and POD.
@@ -538,7 +538,7 @@ inline void Serialize(Stream& os, const T& a)
538538
}
539539

540540
template<typename Stream, typename T>
541-
inline void Unserialize(Stream& is, T& a)
541+
inline void Unserialize(Stream& is, T&& a)
542542
{
543543
a.Unserialize(is);
544544
}
@@ -884,10 +884,10 @@ void SerializeMany(Stream& s)
884884
}
885885

886886
template<typename Stream, typename Arg, typename... Args>
887-
void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
887+
void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
888888
{
889-
::Serialize(s, std::forward<Arg>(arg));
890-
::SerializeMany(s, std::forward<Args>(args)...);
889+
::Serialize(s, arg);
890+
::SerializeMany(s, args...);
891891
}
892892

893893
template<typename Stream>
@@ -896,20 +896,20 @@ inline void UnserializeMany(Stream& s)
896896
}
897897

898898
template<typename Stream, typename Arg, typename... Args>
899-
inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
899+
inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
900900
{
901901
::Unserialize(s, arg);
902902
::UnserializeMany(s, args...);
903903
}
904904

905905
template<typename Stream, typename... Args>
906-
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
906+
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
907907
{
908-
::SerializeMany(s, std::forward<Args>(args)...);
908+
::SerializeMany(s, args...);
909909
}
910910

911911
template<typename Stream, typename... Args>
912-
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
912+
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
913913
{
914914
::UnserializeMany(s, args...);
915915
}

src/streams.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OverrideStream
4242
}
4343

4444
template<typename T>
45-
OverrideStream<Stream>& operator>>(T& obj)
45+
OverrideStream<Stream>& operator>>(T&& obj)
4646
{
4747
// Unserialize from this stream
4848
::Unserialize(*this, obj);
@@ -399,7 +399,7 @@ class CDataStream
399399
}
400400

401401
template<typename T>
402-
CDataStream& operator>>(T& obj)
402+
CDataStream& operator>>(T&& obj)
403403
{
404404
// Unserialize from this stream
405405
::Unserialize(*this, obj);
@@ -543,7 +543,7 @@ class CAutoFile
543543
}
544544

545545
template<typename T>
546-
CAutoFile& operator>>(T& obj)
546+
CAutoFile& operator>>(T&& obj)
547547
{
548548
// Unserialize from this stream
549549
if (!file)
@@ -686,7 +686,7 @@ class CBufferedFile
686686
}
687687

688688
template<typename T>
689-
CBufferedFile& operator>>(T& obj) {
689+
CBufferedFile& operator>>(T&& obj) {
690690
// Unserialize from this stream
691691
::Unserialize(*this, obj);
692692
return (*this);

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class CCoins
348348
vout.assign(vAvail.size(), CTxOut());
349349
for (unsigned int i = 0; i < vAvail.size(); i++) {
350350
if (vAvail[i])
351-
::Unserialize(s, REF(CTxOutCompressor(vout[i])));
351+
::Unserialize(s, CTxOutCompressor(vout[i]));
352352
}
353353
// coinbase height
354354
::Unserialize(s, VARINT(nHeight));

src/undo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TxInUndoDeserializer
5454
int nVersionDummy;
5555
::Unserialize(s, VARINT(nVersionDummy));
5656
}
57-
::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))));
57+
::Unserialize(s, CTxOutCompressor(REF(txout->out)));
5858
}
5959

6060
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
@@ -76,7 +76,7 @@ class CTxUndo
7676
uint64_t count = vprevout.size();
7777
::Serialize(s, COMPACTSIZE(REF(count)));
7878
for (const auto& prevout : vprevout) {
79-
::Serialize(s, REF(TxInUndoSerializer(&prevout)));
79+
::Serialize(s, TxInUndoSerializer(&prevout));
8080
}
8181
}
8282

@@ -90,7 +90,7 @@ class CTxUndo
9090
}
9191
vprevout.resize(count);
9292
for (auto& prevout : vprevout) {
93-
::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
93+
::Unserialize(s, TxInUndoDeserializer(&prevout));
9494
}
9595
}
9696
};

0 commit comments

Comments
 (0)