Compare commits

...

1 Commits

Author SHA1 Message Date
Mayukha Vadari
44616773e7 perf: Make STVar, STObject and STArray moves noexcept
std::vector moves elements on reallocation only if the move
constructor is noexcept; otherwise it copies them. These move
constructors were not noexcept, so every time STObject::v_ or
STArray::v_ grew, each existing element was deep-copied through the
virtual STBase::copy. Add static_asserts so this does not regress.
2026-10-14 11:44:37 -04:00
6 changed files with 23 additions and 12 deletions

View File

@@ -42,9 +42,9 @@ public:
STArray&
operator=(STArray const&) = default;
STArray(STArray&&);
STArray(STArray&&) noexcept;
STArray&
operator=(STArray&&);
operator=(STArray&&) noexcept;
STArray(SField const& f, std::size_t n);
STArray(SerialIter& sit, SField const& f, int depth = 0);

View File

@@ -85,9 +85,9 @@ public:
STObject&
operator=(STObject const&) = default;
STObject(STObject&&);
STObject(STObject&&) noexcept;
STObject&
operator=(STObject&& other);
operator=(STObject&& other) noexcept;
STObject(SOTemplate const& type, SField const& name);
STObject(

View File

@@ -45,11 +45,11 @@ private:
public:
~STVar();
STVar(STVar const& other);
STVar(STVar&& other);
STVar(STVar&& other) noexcept;
STVar&
operator=(STVar const& rhs);
STVar&
operator=(STVar&& rhs);
operator=(STVar&& rhs) noexcept;
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
STVar(STBase&& t) : p_(t.move(kMaxSize, &d_))

View File

@@ -11,16 +11,20 @@
#include <cstddef>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
namespace xrpl {
STArray::STArray(STArray&& other) : STBase(other.getFName()), v_(std::move(other.v_))
// Lets std::vector move, not copy, elements when it grows.
static_assert(std::is_nothrow_move_constructible_v<STArray>);
STArray::STArray(STArray&& other) noexcept : STBase(other.getFName()), v_(std::move(other.v_))
{
}
STArray&
STArray::operator=(STArray&& other)
STArray::operator=(STArray&& other) noexcept
{
setFName(other.getFName());
v_ = std::move(other.v_);

View File

@@ -37,12 +37,16 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
namespace xrpl {
STObject::STObject(STObject&& other)
// Lets std::vector move, not copy, elements when it grows.
static_assert(std::is_nothrow_move_constructible_v<STObject>);
STObject::STObject(STObject&& other) noexcept
: STBase(other.getFName()), v_(std::move(other.v_)), type_(other.type_)
{
}
@@ -126,7 +130,7 @@ STObject::add(Serializer& s) const
}
STObject&
STObject::operator=(STObject&& other)
STObject::operator=(STObject&& other) noexcept
{
setFName(other.getFName());
type_ = other.type_;

View File

@@ -25,6 +25,9 @@
namespace xrpl::detail {
// Lets std::vector move, not copy, elements when it grows.
static_assert(std::is_nothrow_move_constructible_v<STVar>);
DefaultObjectT gDefaultObject;
NonPresentObjectT gNonPresentObject;
@@ -41,7 +44,7 @@ STVar::STVar(STVar const& other)
p_ = other.p_->copy(kMaxSize, &d_);
}
STVar::STVar(STVar&& other)
STVar::STVar(STVar&& other) noexcept
{
if (other.onHeap())
{
@@ -74,7 +77,7 @@ STVar::operator=(STVar const& rhs)
}
STVar&
STVar::operator=(STVar&& rhs)
STVar::operator=(STVar&& rhs) noexcept
{
if (&rhs != this)
{