Skip to content

Commit facaa14

Browse files
author
MarcoFalke
committed
Faster std::byte (pre)vector (un)serialize
1 parent dca0f23 commit facaa14

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/serialize.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,12 @@ template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_st
710710

711711
/**
712712
* prevector
713-
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
714713
*/
715714
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
716715
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
717716

718717
/**
719718
* vector
720-
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
721719
*/
722720
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
723721
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
@@ -820,10 +818,9 @@ void Unserialize(Stream& is, std::basic_string<C>& str)
820818
template <typename Stream, unsigned int N, typename T>
821819
void Serialize(Stream& os, const prevector<N, T>& v)
822820
{
823-
if constexpr (std::is_same_v<T, unsigned char>) {
821+
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
824822
WriteCompactSize(os, v.size());
825-
if (!v.empty())
826-
os.write(MakeByteSpan(v));
823+
if (!v.empty()) os.write(MakeByteSpan(v));
827824
} else {
828825
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
829826
}
@@ -833,7 +830,7 @@ void Serialize(Stream& os, const prevector<N, T>& v)
833830
template <typename Stream, unsigned int N, typename T>
834831
void Unserialize(Stream& is, prevector<N, T>& v)
835832
{
836-
if constexpr (std::is_same_v<T, unsigned char>) {
833+
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
837834
// Limit size per read so bogus size value won't cause out of memory
838835
v.clear();
839836
unsigned int nSize = ReadCompactSize(is);
@@ -856,10 +853,9 @@ void Unserialize(Stream& is, prevector<N, T>& v)
856853
template <typename Stream, typename T, typename A>
857854
void Serialize(Stream& os, const std::vector<T, A>& v)
858855
{
859-
if constexpr (std::is_same_v<T, unsigned char>) {
856+
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
860857
WriteCompactSize(os, v.size());
861-
if (!v.empty())
862-
os.write(MakeByteSpan(v));
858+
if (!v.empty()) os.write(MakeByteSpan(v));
863859
} else if constexpr (std::is_same_v<T, bool>) {
864860
// A special case for std::vector<bool>, as dereferencing
865861
// std::vector<bool>::const_iterator does not result in a const bool&
@@ -877,7 +873,7 @@ void Serialize(Stream& os, const std::vector<T, A>& v)
877873
template <typename Stream, typename T, typename A>
878874
void Unserialize(Stream& is, std::vector<T, A>& v)
879875
{
880-
if constexpr (std::is_same_v<T, unsigned char>) {
876+
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
881877
// Limit size per read so bogus size value won't cause out of memory
882878
v.clear();
883879
unsigned int nSize = ReadCompactSize(is);

0 commit comments

Comments
 (0)