aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-08-02 19:51:55 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-08-03 10:34:09 +0200
commit0fafaca4d3bbf0c0b5bfe1ec617ab15252ea51e6 (patch)
tree460bf35bf9655059fc374d0368a375a2c19f9e07 /src
parentc8839ec5cd81ba9ae88081747c49ecc758973dd1 (diff)
downloadbitcoin-0fafaca4d3bbf0c0b5bfe1ec617ab15252ea51e6.tar.xz
refactor: use "if constexpr" in prevector's Unserialize()
This gets rid of unnecessarily creating a temporary object T() to call the right function.
Diffstat (limited to 'src')
-rw-r--r--src/serialize.h42
1 files changed, 15 insertions, 27 deletions
diff --git a/src/serialize.h b/src/serialize.h
index cfbbd1e940..8dd14cd0e3 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -643,8 +643,6 @@ template<typename Stream, typename C> 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<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);
/**
@@ -762,35 +760,25 @@ 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>
+void Unserialize(Stream& is, prevector<N, T>& 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<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));
}
}
-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)
-{
- Unserialize_impl(is, v, T());
-}
-
-
/**
* vector