diff options
author | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2023-08-02 19:49:40 +0200 |
---|---|---|
committer | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2023-08-03 10:32:25 +0200 |
commit | c8839ec5cd81ba9ae88081747c49ecc758973dd1 (patch) | |
tree | 1b07e9b76c590fa8bfff1ccb1a90e8f8718f850b /src | |
parent | 1403d181c106bc271ad2522adebde07c7850069b (diff) | |
download | bitcoin-c8839ec5cd81ba9ae88081747c49ecc758973dd1.tar.xz |
refactor: use "if constexpr" in prevector's Serialize()
This gets rid of unnecessarily creating a temporary object T() to call
the right function.
Diffstat (limited to 'src')
-rw-r--r-- | src/serialize.h | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/src/serialize.h b/src/serialize.h index aa9f834654..cfbbd1e940 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -642,8 +642,6 @@ 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&); @@ -751,24 +749,16 @@ 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 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)); + } } |