Improve STVector256 deserialization

This commit is contained in:
Nik Bougalis
2022-06-11 22:01:35 -07:00
parent 47ccd0b579
commit 9eb303f8e8
3 changed files with 23 additions and 16 deletions

View File

@@ -48,7 +48,8 @@ private:
std::size_t size_ = 0; std::size_t size_ = 0;
public: public:
using const_iterator = std::uint8_t const*; using value_type = std::uint8_t;
using const_iterator = value_type const*;
/** Default constructed Slice has length 0. */ /** Default constructed Slice has length 0. */
Slice() noexcept = default; Slice() noexcept = default;
@@ -75,13 +76,13 @@ public:
This may be zero for an empty range. This may be zero for an empty range.
*/ */
/** @{ */ /** @{ */
std::size_t [[nodiscard]] std::size_t
size() const noexcept size() const noexcept
{ {
return size_; return size_;
} }
std::size_t [[nodiscard]] std::size_t
length() const noexcept length() const noexcept
{ {
return size_; return size_;

View File

@@ -26,6 +26,7 @@
#define RIPPLE_BASICS_BASE_UINT_H_INCLUDED #define RIPPLE_BASICS_BASE_UINT_H_INCLUDED
#include <ripple/basics/Expected.h> #include <ripple/basics/Expected.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/contract.h> #include <ripple/basics/contract.h>
#include <ripple/basics/hardened_hash.h> #include <ripple/basics/hardened_hash.h>
#include <ripple/basics/strHex.h> #include <ripple/basics/strHex.h>
@@ -56,6 +57,11 @@ struct is_contiguous_container<
{ {
}; };
template <>
struct is_contiguous_container<Slice> : std::true_type
{
};
} // namespace detail } // namespace detail
/** Integers of any length that is a multiple of 32-bits /** Integers of any length that is a multiple of 32-bits

View File

@@ -26,19 +26,19 @@ namespace ripple {
STVector256::STVector256(SerialIter& sit, SField const& name) : STBase(name) STVector256::STVector256(SerialIter& sit, SField const& name) : STBase(name)
{ {
Blob data = sit.getVL(); auto const slice = sit.getSlice(sit.getVLDataLength());
auto const count = data.size() / (256 / 8);
mValue.reserve(count); if (slice.size() % uint256::size() != 0)
Blob::iterator begin = data.begin(); Throw<std::runtime_error>(
unsigned int uStart = 0; "Bad serialization for STVector256: " +
for (unsigned int i = 0; i != count; i++) std::to_string(slice.size()));
{
unsigned int uEnd = uStart + (256 / 8); auto const cnt = slice.size() / uint256::size();
// This next line could be optimized to construct a default
// uint256 in the vector and then copy into it mValue.reserve(cnt);
mValue.push_back(uint256(Blob(begin + uStart, begin + uEnd)));
uStart = uEnd; for (std::size_t i = 0; i != cnt; ++i)
} mValue.emplace_back(slice.substr(i * uint256::size(), uint256::size()));
} }
STBase* STBase*