Skip to content

Commit c0a47da

Browse files
committed
Merge #13719: Avoid creating a temporary vector for size-prefixed elements
84547fa Avoid creating a temporary vector for size-prefixed elements (Pieter Wuille) Pull request description: This is a simple improvement to the PSBT serialization code, avoiding the need for temporary vectors everywhere. Tree-SHA512: 9f7243b7169ec8ba00ffad31af03c016ab84e4f76ebac810167f91f5e8008f3827ad59fbcee0cb2bd2334fc26466eb222404af24e7fb6ec040fd78229ebe0fd1
2 parents d2186b3 + 84547fa commit c0a47da

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/script/sign.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,24 @@ static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02;
116116
// as a 0 length key which indicates that this is the separator. The separator has no value.
117117
static constexpr uint8_t PSBT_SEPARATOR = 0x00;
118118

119-
// Takes a stream and multiple arguments and serializes them into a vector and then into the stream
119+
// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
120120
// The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
121121
template<typename Stream, typename... X>
122122
void SerializeToVector(Stream& s, const X&... args)
123123
{
124-
std::vector<unsigned char> ret;
125-
CVectorWriter ss(SER_NETWORK, PROTOCOL_VERSION, ret, 0);
126-
SerializeMany(ss, args...);
127-
s << ret;
124+
WriteCompactSize(s, GetSerializeSizeMany(s, args...));
125+
SerializeMany(s, args...);
128126
}
129127

130128
// Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
131129
template<typename Stream, typename... X>
132130
void UnserializeFromVector(Stream& s, X&... args)
133131
{
134-
std::vector<unsigned char> data;
135-
s >> data;
136-
CDataStream ss(data, SER_NETWORK, PROTOCOL_VERSION);
137-
UnserializeMany(ss, args...);
138-
if (!ss.eof()) {
132+
size_t expected_size = ReadCompactSize(s);
133+
size_t remaining_before = s.size();
134+
UnserializeMany(s, args...);
135+
size_t remaining_after = s.size();
136+
if (remaining_after + expected_size != remaining_before) {
139137
throw std::ios_base::failure("Size of value was not the stated size");
140138
}
141139
}

src/serialize.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,4 +991,12 @@ size_t GetSerializeSize(const S& s, const T& t)
991991
return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
992992
}
993993

994+
template <typename S, typename... T>
995+
size_t GetSerializeSizeMany(const S& s, const T&... t)
996+
{
997+
CSizeComputer sc(s.GetType(), s.GetVersion());
998+
SerializeMany(sc, t...);
999+
return sc.size();
1000+
}
1001+
9941002
#endif // BITCOIN_SERIALIZE_H

0 commit comments

Comments
 (0)