From bd08a008b42dac921bd9c031637e378899c1cd1d Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 20:01:52 +0200 Subject: [PATCH 1/6] refactor: use fold expressions instead of recursive calls in SerializeMany() Instead of recursively calling `SerializeMany` and peeling off one argument at a time, use a fold expression. 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. --- src/serialize.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index 0cda0ac7d5e..a56df871e7d 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1039,16 +1039,10 @@ public: int GetVersion() const { return nVersion; } }; -template -void SerializeMany(Stream& s) +template +void SerializeMany(Stream& s, const Args&... args) { -} - -template -void SerializeMany(Stream& s, const Arg& arg, const Args&... args) -{ - ::Serialize(s, arg); - ::SerializeMany(s, args...); + (::Serialize(s, args), ...); } template From 1403d181c106bc271ad2522adebde07c7850069b Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 20:03:24 +0200 Subject: [PATCH 2/6] refactor: use fold expressions instead of recursive calls in UnserializeMany() Instead of recursively calling `UnserializeMany` and peeling off one argument at a time, use a fold expression. 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. --- src/serialize.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index a56df871e7d..aa9f8346540 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1045,16 +1045,10 @@ void SerializeMany(Stream& s, const Args&... args) (::Serialize(s, args), ...); } -template -inline void UnserializeMany(Stream& s) +template +inline void UnserializeMany(Stream& s, Args&&... args) { -} - -template -inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args) -{ - ::Unserialize(s, arg); - ::UnserializeMany(s, args...); + (::Unserialize(s, args), ...); } template From c8839ec5cd81ba9ae88081747c49ecc758973dd1 Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 19:49:40 +0200 Subject: [PATCH 3/6] refactor: use "if constexpr" in prevector's Serialize() This gets rid of unnecessarily creating a temporary object T() to call the right function. --- src/serialize.h | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index aa9f8346540..cfbbd1e9405 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -642,8 +642,6 @@ template 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 void Serialize_impl(Stream& os, const prevector& v, const unsigned char&); -template void Serialize_impl(Stream& os, const prevector& v, const V&); template inline void Serialize(Stream& os, const prevector& v); template void Unserialize_impl(Stream& is, prevector& v, const unsigned char&); template void Unserialize_impl(Stream& is, prevector& v, const V&); @@ -751,24 +749,16 @@ void Unserialize(Stream& is, std::basic_string& str) /** * prevector */ -template -void Serialize_impl(Stream& os, const prevector& v, const unsigned char&) +template +void Serialize(Stream& os, const prevector& v) { - WriteCompactSize(os, v.size()); - if (!v.empty()) - os.write(MakeByteSpan(v)); -} - -template -void Serialize_impl(Stream& os, const prevector& v, const V&) -{ - Serialize(os, Using>(v)); -} - -template -inline void Serialize(Stream& os, const prevector& v) -{ - Serialize_impl(os, v, T()); + if constexpr (std::is_same_v) { + WriteCompactSize(os, v.size()); + if (!v.empty()) + os.write(MakeByteSpan(v)); + } else { + Serialize(os, Using>(v)); + } } From 0fafaca4d3bbf0c0b5bfe1ec617ab15252ea51e6 Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 19:51:55 +0200 Subject: [PATCH 4/6] refactor: use "if constexpr" in prevector's Unserialize() This gets rid of unnecessarily creating a temporary object T() to call the right function. --- src/serialize.h | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index cfbbd1e9405..8dd14cd0e33 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -643,8 +643,6 @@ template void Unserialize(Stream& is, std::basic_st * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ template inline void Serialize(Stream& os, const prevector& v); -template void Unserialize_impl(Stream& is, prevector& v, const unsigned char&); -template void Unserialize_impl(Stream& is, prevector& v, const V&); template inline void Unserialize(Stream& is, prevector& v); /** @@ -762,35 +760,25 @@ void Serialize(Stream& os, const prevector& v) } -template -void Unserialize_impl(Stream& is, prevector& v, const unsigned char&) +template +void Unserialize(Stream& is, prevector& v) { - // 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; + if constexpr (std::is_same_v) { + // 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>(v)); } } -template -void Unserialize_impl(Stream& is, prevector& v, const V&) -{ - Unserialize(is, Using>(v)); -} - -template -inline void Unserialize(Stream& is, prevector& v) -{ - Unserialize_impl(is, v, T()); -} - - /** * vector From 088caa68fb8efd8624709d643913b8a7e1218f8a Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 19:54:11 +0200 Subject: [PATCH 5/6] refactor: use "if constexpr" in std::vector's Serialize() This gets rid of unnecessarily creating a temporary object T() to call the right function. --- src/serialize.h | 47 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index 8dd14cd0e33..72672b459e4 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -649,9 +649,6 @@ template inline void Unserialize(St * vector * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ -template void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&); -template void Serialize_impl(Stream& os, const std::vector& v, const bool&); -template void Serialize_impl(Stream& os, const std::vector& v, const V&); template inline void Serialize(Stream& os, const std::vector& v); template void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&); template void Unserialize_impl(Stream& is, std::vector& v, const V&); @@ -783,38 +780,26 @@ void Unserialize(Stream& is, prevector& v) /** * vector */ -template -void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&) +template +void Serialize(Stream& os, const std::vector& v) { - WriteCompactSize(os, v.size()); - if (!v.empty()) - os.write(MakeByteSpan(v)); -} - -template -void Serialize_impl(Stream& os, const std::vector& v, const bool&) -{ - // A special case for std::vector, as dereferencing - // std::vector::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); + if constexpr (std::is_same_v) { + WriteCompactSize(os, v.size()); + if (!v.empty()) + os.write(MakeByteSpan(v)); + } else if constexpr (std::is_same_v) { + // A special case for std::vector, as dereferencing + // std::vector::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>(v)); } } -template -void Serialize_impl(Stream& os, const std::vector& v, const V&) -{ - Serialize(os, Using>(v)); -} - -template -inline void Serialize(Stream& os, const std::vector& v) -{ - Serialize_impl(os, v, T()); -} - template void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&) From f054bd072afb72d8dae7adc521ce15c13b236700 Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Wed, 2 Aug 2023 19:55:20 +0200 Subject: [PATCH 6/6] refactor: use "if constexpr" in std::vector's Unserialize() This gets rid of unnecessarily creating a temporary object T() to call the right function. --- src/serialize.h | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index 72672b459e4..39f2c0f3ae7 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -650,8 +650,6 @@ template inline void Unserialize(St * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ template inline void Serialize(Stream& os, const std::vector& v); -template void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&); -template void Unserialize_impl(Stream& is, std::vector& v, const V&); template inline void Unserialize(Stream& is, std::vector& v); /** @@ -801,35 +799,25 @@ void Serialize(Stream& os, const std::vector& v) } -template -void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&) +template +void Unserialize(Stream& is, std::vector& v) { - // 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) { + // 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>(v)); } } -template -void Unserialize_impl(Stream& is, std::vector& v, const V&) -{ - Unserialize(is, Using>(v)); -} - -template -inline void Unserialize(Stream& is, std::vector& v) -{ - Unserialize_impl(is, v, T()); -} - - /** * pair