mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
Faster std::byte (pre)vector (un)serialize
This commit is contained in:
@@ -710,14 +710,12 @@ template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_st
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* prevector
|
* 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> inline void Serialize(Stream& os, const prevector<N, T>& 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> inline void Unserialize(Stream& is, prevector<N, T>& v);
|
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* vector
|
* vector
|
||||||
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
|
|
||||||
*/
|
*/
|
||||||
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
|
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
|
||||||
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
|
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)
|
|||||||
template <typename Stream, unsigned int N, typename T>
|
template <typename Stream, unsigned int N, typename T>
|
||||||
void Serialize(Stream& os, const prevector<N, T>& v)
|
void Serialize(Stream& os, const prevector<N, T>& v)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same_v<T, unsigned char>) {
|
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
if (!v.empty())
|
if (!v.empty()) os.write(MakeByteSpan(v));
|
||||||
os.write(MakeByteSpan(v));
|
|
||||||
} else {
|
} else {
|
||||||
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
|
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
}
|
}
|
||||||
@@ -833,7 +830,7 @@ void Serialize(Stream& os, const prevector<N, T>& v)
|
|||||||
template <typename Stream, unsigned int N, typename T>
|
template <typename Stream, unsigned int N, typename T>
|
||||||
void Unserialize(Stream& is, prevector<N, T>& v)
|
void Unserialize(Stream& is, prevector<N, T>& v)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same_v<T, unsigned char>) {
|
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
|
||||||
// Limit size per read so bogus size value won't cause out of memory
|
// Limit size per read so bogus size value won't cause out of memory
|
||||||
v.clear();
|
v.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
@@ -856,10 +853,9 @@ void Unserialize(Stream& is, prevector<N, T>& v)
|
|||||||
template <typename Stream, typename T, typename A>
|
template <typename Stream, typename T, typename A>
|
||||||
void Serialize(Stream& os, const std::vector<T, A>& v)
|
void Serialize(Stream& os, const std::vector<T, A>& v)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same_v<T, unsigned char>) {
|
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
if (!v.empty())
|
if (!v.empty()) os.write(MakeByteSpan(v));
|
||||||
os.write(MakeByteSpan(v));
|
|
||||||
} else if constexpr (std::is_same_v<T, bool>) {
|
} else if constexpr (std::is_same_v<T, bool>) {
|
||||||
// A special case for std::vector<bool>, as dereferencing
|
// A special case for std::vector<bool>, as dereferencing
|
||||||
// std::vector<bool>::const_iterator does not result in a const bool&
|
// 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)
|
|||||||
template <typename Stream, typename T, typename A>
|
template <typename Stream, typename T, typename A>
|
||||||
void Unserialize(Stream& is, std::vector<T, A>& v)
|
void Unserialize(Stream& is, std::vector<T, A>& v)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same_v<T, unsigned char>) {
|
if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
|
||||||
// Limit size per read so bogus size value won't cause out of memory
|
// Limit size per read so bogus size value won't cause out of memory
|
||||||
v.clear();
|
v.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
|
|||||||
Reference in New Issue
Block a user