Skip to content

Commit 0fafaca

Browse files
committed
refactor: use "if constexpr" in prevector's Unserialize()
This gets rid of unnecessarily creating a temporary object T() to call the right function.
1 parent c8839ec commit 0fafaca

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

src/serialize.h

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,6 @@ template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_st
643643
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
644644
*/
645645
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
646-
template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
647-
template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
648646
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
649647

650648
/**
@@ -762,35 +760,25 @@ void Serialize(Stream& os, const prevector<N, T>& v)
762760
}
763761

764762

765-
template<typename Stream, unsigned int N, typename T>
766-
void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
763+
template <typename Stream, unsigned int N, typename T>
764+
void Unserialize(Stream& is, prevector<N, T>& v)
767765
{
768-
// Limit size per read so bogus size value won't cause out of memory
769-
v.clear();
770-
unsigned int nSize = ReadCompactSize(is);
771-
unsigned int i = 0;
772-
while (i < nSize)
773-
{
774-
unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
775-
v.resize_uninitialized(i + blk);
776-
is.read(AsWritableBytes(Span{&v[i], blk}));
777-
i += blk;
766+
if constexpr (std::is_same_v<T, unsigned char>) {
767+
// Limit size per read so bogus size value won't cause out of memory
768+
v.clear();
769+
unsigned int nSize = ReadCompactSize(is);
770+
unsigned int i = 0;
771+
while (i < nSize) {
772+
unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
773+
v.resize_uninitialized(i + blk);
774+
is.read(AsWritableBytes(Span{&v[i], blk}));
775+
i += blk;
776+
}
777+
} else {
778+
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
778779
}
779780
}
780781

781-
template<typename Stream, unsigned int N, typename T, typename V>
782-
void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
783-
{
784-
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
785-
}
786-
787-
template<typename Stream, unsigned int N, typename T>
788-
inline void Unserialize(Stream& is, prevector<N, T>& v)
789-
{
790-
Unserialize_impl(is, v, T());
791-
}
792-
793-
794782

795783
/**
796784
* vector

0 commit comments

Comments
 (0)