aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2023-08-04 13:35:34 +0100
committerfanquake <fanquake@gmail.com>2023-08-04 14:50:49 +0200
commitf138422d37ba79e6ab0edf8f459581f1243fba08 (patch)
tree46f1eb0b635827855bfb1121ee86cb39b6dc5333 /src
parenta4ca4975880c4f870c6047065c70610af2529e74 (diff)
parentf054bd072afb72d8dae7adc521ce15c13b236700 (diff)
downloadbitcoin-f138422d37ba79e6ab0edf8f459581f1243fba08.tar.xz
Merge bitcoin/bitcoin#28203: refactor: serialization simplifications
f054bd072afb72d8dae7adc521ce15c13b236700 refactor: use "if constexpr" in std::vector's Unserialize() (Martin Leitner-Ankerl) 088caa68fb8efd8624709d643913b8a7e1218f8a refactor: use "if constexpr" in std::vector's Serialize() (Martin Leitner-Ankerl) 0fafaca4d3bbf0c0b5bfe1ec617ab15252ea51e6 refactor: use "if constexpr" in prevector's Unserialize() (Martin Leitner-Ankerl) c8839ec5cd81ba9ae88081747c49ecc758973dd1 refactor: use "if constexpr" in prevector's Serialize() (Martin Leitner-Ankerl) 1403d181c106bc271ad2522adebde07c7850069b refactor: use fold expressions instead of recursive calls in UnserializeMany() (Martin Leitner-Ankerl) bd08a008b42dac921bd9c031637e378899c1cd1d refactor: use fold expressions instead of recursive calls in SerializeMany() (Martin Leitner-Ankerl) Pull request description: This simplifies the serialization code a bit and should also make it a bit faster. * use fold expressions instead of recursive calls. This simplifies the code, makes it most likely faster because it reduces the number of function calls, and compiles faster because there are fewer template instantiations. * use `if constexpr` instead of unnecessarily creating a temporary object only to call the right overload. This is used for `std::vector` and `prevector` serialization. ACKs for top commit: MarcoFalke: only change is to add a missing `&`. lgtm, re-ACK f054bd072afb72d8dae7adc521ce15c13b236700 📦 jonatack: ACK f054bd072afb72d8dae7adc521ce15c13b236700 sipa: utACK f054bd072afb72d8dae7adc521ce15c13b236700 john-moffett: ACK f054bd072afb72d8dae7adc521ce15c13b236700 Tree-SHA512: 0417bf2d6be486c581732297945449211fc3481bac82964e27628b38ef55a47dfa58d730148aeaf1b19fa8eb1076489cc646ceebb178162a9afa59034601501d
Diffstat (limited to 'src')
-rw-r--r--src/serialize.h187
1 files changed, 63 insertions, 124 deletions
diff --git a/src/serialize.h b/src/serialize.h
index 0cda0ac7d5..39f2c0f3ae 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -642,23 +642,14 @@ template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_st
* prevector
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
*/
-template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
-template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
-template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
-template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
/**
* vector
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
*/
-template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
-template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&);
-template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
-template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
-template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
/**
@@ -751,122 +742,82 @@ void Unserialize(Stream& is, std::basic_string<C>& str)
/**
* prevector
*/
-template<typename Stream, unsigned int N, typename T>
-void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
-{
- WriteCompactSize(os, v.size());
- if (!v.empty())
- os.write(MakeByteSpan(v));
-}
-
-template<typename Stream, unsigned int N, typename T, typename V>
-void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
-{
- Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
-}
-
-template<typename Stream, unsigned int N, typename T>
-inline void Serialize(Stream& os, const prevector<N, T>& v)
-{
- Serialize_impl(os, v, T());
-}
-
-
-template<typename Stream, unsigned int N, typename T>
-void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
-{
- // Limit size per read so bogus size value won't cause out of memory
- v.clear();
- unsigned int nSize = ReadCompactSize(is);
- unsigned int i = 0;
- while (i < nSize)
- {
- unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
- v.resize_uninitialized(i + blk);
- is.read(AsWritableBytes(Span{&v[i], blk}));
- i += blk;
+template <typename Stream, unsigned int N, typename T>
+void Serialize(Stream& os, const prevector<N, T>& v)
+{
+ if constexpr (std::is_same_v<T, unsigned char>) {
+ WriteCompactSize(os, v.size());
+ if (!v.empty())
+ os.write(MakeByteSpan(v));
+ } else {
+ Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
}
}
-template<typename Stream, unsigned int N, typename T, typename V>
-void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
-{
- Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
-}
-template<typename Stream, unsigned int N, typename T>
-inline void Unserialize(Stream& is, prevector<N, T>& v)
+template <typename Stream, unsigned int N, typename T>
+void Unserialize(Stream& is, prevector<N, T>& v)
{
- Unserialize_impl(is, v, T());
+ if constexpr (std::is_same_v<T, unsigned char>) {
+ // Limit size per read so bogus size value won't cause out of memory
+ v.clear();
+ unsigned int nSize = ReadCompactSize(is);
+ unsigned int i = 0;
+ while (i < nSize) {
+ unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
+ v.resize_uninitialized(i + blk);
+ is.read(AsWritableBytes(Span{&v[i], blk}));
+ i += blk;
+ }
+ } else {
+ Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
+ }
}
-
/**
* vector
*/
-template<typename Stream, typename T, typename A>
-void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
-{
- WriteCompactSize(os, v.size());
- if (!v.empty())
- os.write(MakeByteSpan(v));
-}
-
-template<typename Stream, typename T, typename A>
-void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
-{
- // A special case for std::vector<bool>, as dereferencing
- // std::vector<bool>::const_iterator does not result in a const bool&
- // due to std::vector's special casing for bool arguments.
- WriteCompactSize(os, v.size());
- for (bool elem : v) {
- ::Serialize(os, elem);
+template <typename Stream, typename T, typename A>
+void Serialize(Stream& os, const std::vector<T, A>& v)
+{
+ if constexpr (std::is_same_v<T, unsigned char>) {
+ WriteCompactSize(os, v.size());
+ if (!v.empty())
+ os.write(MakeByteSpan(v));
+ } else if constexpr (std::is_same_v<T, bool>) {
+ // A special case for std::vector<bool>, as dereferencing
+ // std::vector<bool>::const_iterator does not result in a const bool&
+ // due to std::vector's special casing for bool arguments.
+ WriteCompactSize(os, v.size());
+ for (bool elem : v) {
+ ::Serialize(os, elem);
+ }
+ } else {
+ Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
}
}
-template<typename Stream, typename T, typename A, typename V>
-void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
-{
- Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
-}
-template<typename Stream, typename T, typename A>
-inline void Serialize(Stream& os, const std::vector<T, A>& v)
+template <typename Stream, typename T, typename A>
+void Unserialize(Stream& is, std::vector<T, A>& v)
{
- Serialize_impl(os, v, T());
-}
-
-
-template<typename Stream, typename T, typename A>
-void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
-{
- // Limit size per read so bogus size value won't cause out of memory
- v.clear();
- unsigned int nSize = ReadCompactSize(is);
- unsigned int i = 0;
- while (i < nSize)
- {
- unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
- v.resize(i + blk);
- is.read(AsWritableBytes(Span{&v[i], blk}));
- i += blk;
+ if constexpr (std::is_same_v<T, unsigned char>) {
+ // Limit size per read so bogus size value won't cause out of memory
+ v.clear();
+ unsigned int nSize = ReadCompactSize(is);
+ unsigned int i = 0;
+ while (i < nSize) {
+ unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
+ v.resize(i + blk);
+ is.read(AsWritableBytes(Span{&v[i], blk}));
+ i += blk;
+ }
+ } else {
+ Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
}
}
-template<typename Stream, typename T, typename A, typename V>
-void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
-{
- Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
-}
-
-template<typename Stream, typename T, typename A>
-inline void Unserialize(Stream& is, std::vector<T, A>& v)
-{
- Unserialize_impl(is, v, T());
-}
-
-
/**
* pair
@@ -1039,28 +990,16 @@ public:
int GetVersion() const { return nVersion; }
};
-template<typename Stream>
-void SerializeMany(Stream& s)
-{
-}
-
-template<typename Stream, typename Arg, typename... Args>
-void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
-{
- ::Serialize(s, arg);
- ::SerializeMany(s, args...);
-}
-
-template<typename Stream>
-inline void UnserializeMany(Stream& s)
+template <typename Stream, typename... Args>
+void SerializeMany(Stream& s, const Args&... args)
{
+ (::Serialize(s, args), ...);
}
-template<typename Stream, typename Arg, typename... Args>
-inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
+template <typename Stream, typename... Args>
+inline void UnserializeMany(Stream& s, Args&&... args)
{
- ::Unserialize(s, arg);
- ::UnserializeMany(s, args...);
+ (::Unserialize(s, args), ...);
}
template<typename Stream, typename... Args>