mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Reduce memory allocation, remove some functions in Serializer.
This commit is contained in:
@@ -41,6 +41,7 @@ private:
|
||||
Blob mData;
|
||||
|
||||
public:
|
||||
explicit
|
||||
Serializer (int n = 256)
|
||||
{
|
||||
mData.reserve (n);
|
||||
@@ -107,7 +108,6 @@ public:
|
||||
int addZeros (size_t uBytes);
|
||||
|
||||
int addVL (Blob const& vector);
|
||||
int addVL (std::string const& string);
|
||||
int addVL (const void* ptr, int len);
|
||||
|
||||
// disassemble functions
|
||||
@@ -232,7 +232,6 @@ public:
|
||||
{
|
||||
mData.clear ();
|
||||
}
|
||||
int removeLastByte ();
|
||||
bool chop (int num);
|
||||
|
||||
// vector-like functions
|
||||
@@ -297,17 +296,17 @@ public:
|
||||
return h.str ();
|
||||
}
|
||||
|
||||
// low-level VL length encode/decode functions
|
||||
static Blob encodeVL (int length);
|
||||
static int decodeLengthLength (int b1);
|
||||
static int decodeVLLength (int b1);
|
||||
static int decodeVLLength (int b1, int b2);
|
||||
static int decodeVLLength (int b1, int b2, int b3);
|
||||
private:
|
||||
static int lengthVL (int length)
|
||||
{
|
||||
return length + encodeLengthLength (length);
|
||||
}
|
||||
static int encodeLengthLength (int length); // length to encode length
|
||||
static int decodeLengthLength (int b1);
|
||||
static int decodeVLLength (int b1);
|
||||
static int decodeVLLength (int b1, int b2);
|
||||
static int decodeVLLength (int b1, int b2, int b3);
|
||||
int addEncoded (int length);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -278,21 +278,6 @@ bool Serializer::chop (int bytes)
|
||||
return true;
|
||||
}
|
||||
|
||||
int Serializer::removeLastByte ()
|
||||
{
|
||||
int size = mData.size () - 1;
|
||||
|
||||
if (size < 0)
|
||||
{
|
||||
assert (false);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = mData[size];
|
||||
mData.resize (size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Serializer::getRaw (Blob& o, int offset, int length) const
|
||||
{
|
||||
if ((offset + length) > mData.size ()) return false;
|
||||
@@ -363,7 +348,7 @@ uint256 Serializer::getPrefixHash (std::uint32_t prefix, const unsigned char* da
|
||||
|
||||
int Serializer::addVL (Blob const& vector)
|
||||
{
|
||||
int ret = addRaw (encodeVL (vector.size ()));
|
||||
int ret = addEncoded (vector.size ());
|
||||
addRaw (vector);
|
||||
assert (mData.size () == (ret + vector.size () + encodeLengthLength (vector.size ())));
|
||||
return ret;
|
||||
@@ -371,7 +356,7 @@ int Serializer::addVL (Blob const& vector)
|
||||
|
||||
int Serializer::addVL (const void* ptr, int len)
|
||||
{
|
||||
int ret = addRaw (encodeVL (len));
|
||||
int ret = addEncoded (len);
|
||||
|
||||
if (len)
|
||||
addRaw (ptr, len);
|
||||
@@ -379,16 +364,6 @@ int Serializer::addVL (const void* ptr, int len)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Serializer::addVL (std::string const& string)
|
||||
{
|
||||
int ret = addRaw (string.size ());
|
||||
|
||||
if (!string.empty ())
|
||||
addRaw (string.data (), string.size ());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Serializer::getVL (Blob& objectVL, int offset, int& length) const
|
||||
{
|
||||
int b1;
|
||||
@@ -470,31 +445,34 @@ bool Serializer::getVLLength (int& length, int offset) const
|
||||
return true;
|
||||
}
|
||||
|
||||
Blob Serializer::encodeVL (int length)
|
||||
int Serializer::addEncoded (int length)
|
||||
{
|
||||
unsigned char lenBytes[4];
|
||||
std::array<std::uint8_t, 4> bytes;
|
||||
int numBytes = 0;
|
||||
|
||||
if (length <= 192)
|
||||
{
|
||||
lenBytes[0] = static_cast<unsigned char> (length);
|
||||
return Blob (&lenBytes[0], &lenBytes[1]);
|
||||
bytes[0] = static_cast<unsigned char> (length);
|
||||
numBytes = 1;
|
||||
}
|
||||
else if (length <= 12480)
|
||||
{
|
||||
length -= 193;
|
||||
lenBytes[0] = 193 + static_cast<unsigned char> (length >> 8);
|
||||
lenBytes[1] = static_cast<unsigned char> (length & 0xff);
|
||||
return Blob (&lenBytes[0], &lenBytes[2]);
|
||||
bytes[0] = 193 + static_cast<unsigned char> (length >> 8);
|
||||
bytes[1] = static_cast<unsigned char> (length & 0xff);
|
||||
numBytes = 2;
|
||||
}
|
||||
else if (length <= 918744)
|
||||
{
|
||||
length -= 12481;
|
||||
lenBytes[0] = 241 + static_cast<unsigned char> (length >> 16);
|
||||
lenBytes[1] = static_cast<unsigned char> ((length >> 8) & 0xff);
|
||||
lenBytes[2] = static_cast<unsigned char> (length & 0xff);
|
||||
return Blob (&lenBytes[0], &lenBytes[3]);
|
||||
bytes[0] = 241 + static_cast<unsigned char> (length >> 16);
|
||||
bytes[1] = static_cast<unsigned char> ((length >> 8) & 0xff);
|
||||
bytes[2] = static_cast<unsigned char> (length & 0xff);
|
||||
numBytes = 3;
|
||||
}
|
||||
else throw std::overflow_error ("lenlen");
|
||||
|
||||
return addRaw (&bytes[0], numBytes);
|
||||
}
|
||||
|
||||
int Serializer::encodeLengthLength (int length)
|
||||
|
||||
@@ -79,8 +79,18 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode,
|
||||
{
|
||||
if (format == snfWIRE)
|
||||
{
|
||||
Serializer s (rawNode);
|
||||
int type = s.removeLastByte ();
|
||||
if (rawNode.empty ())
|
||||
{
|
||||
#ifdef BEAST_DEBUG
|
||||
deprecatedLogs().journal("SHAMapTreeNode").fatal <<
|
||||
"Wire format node is empty";
|
||||
assert (false);
|
||||
#endif
|
||||
throw std::runtime_error ("invalid node AW type");
|
||||
}
|
||||
|
||||
Serializer s (rawNode.begin (), rawNode.end () - 1);
|
||||
int type = rawNode.back ();
|
||||
int len = s.getLength ();
|
||||
|
||||
if ((type < 0) || (type > 4))
|
||||
|
||||
Reference in New Issue
Block a user