perf(runtime): share names and size lists when instancing artboards (#15394)

* perf(runtime): share an instance's names with its source

An artboard instance clones every component of its source, and each clone
deep-copied the component's name sidecar: an allocation per named
component, and a second one for a name past the small-string size. Files
the editor exports keep every component's name, so that was an allocation
per object of every instance. The generated property clusters (names, and
the cold layout and corner radius fields) now use SharedSidecar, which a
copy shares with the object it was copied from until either side writes
to it.

The hand-written Sidecar members hold mutable per-object state and keep
their deep copy.

In the HUD perf tray, an added leaderboard row makes 51 fewer allocations
(591 to 540): 43 component names, and 8 more in the view model item the
host makes for it, whose values clone their names too. A lap row also
makes 51 fewer.

* perf(runtime): size an instance's lists like its source's

An artboard instance fills its drawables, advancing components, data
binds and the other lists initialize builds an entry at a time, and ends
up with exactly as many as its source. Each grew geometrically, so a row
paid several allocations per list and kept up to as many entries again in
unused capacity: an instance of an artboard with 282 drawables held room
for 512. The instance now sizes them from its source before cloning, and
a state machine instance sizes its data binds from its machine. A
container's dirty-bind list is sized with its binds, as all of them are
dirty once first bound.

In the HUD perf tray, an added leaderboard row makes 18 fewer allocations
(540 to 522); a lap row makes 15 fewer.

* perf(text): size a new text's runs and styled characters once

A cloned text's runs add themselves one at a time to its two run lists,
and the first styling of a text appends its characters and runs one at a
time, so a new text grew each of those four lists geometrically. A clone
now sizes its run lists from its source, and styling first sizes the
characters and runs it will append: a run's UTF-8 bytes bound its
characters. Past that first sizing it grows them at least twice over, the
way appending does, so a text that grows a little at a time doesn't
reallocate on every styling.

In the HUD perf tray, an added leaderboard row makes 19 fewer allocations
(522 to 503); a lap row makes 18 fewer.

* fix(text): size a cloned text's runs only when text is built

Text::clone reserved its runs, which only exist with WITH_RIVE_TEXT, so
a runtime built without text no longer compiled. The reserves are
guarded like the runs they size.

Rive-Mono-Commit: 8872e523060d9502ade28e88d993ae722d151627
diff --git a/dev/core_generator/lib/src/definition.dart b/dev/core_generator/lib/src/definition.dart
index e47878c..fd428a5 100644
--- a/dev/core_generator/lib/src/definition.dart
+++ b/dev/core_generator/lib/src/definition.dart
@@ -827,12 +827,13 @@
       }
 
       // Lazily-allocated sidecar holders: one 8-byte pointer per cluster, null
-      // until a property in the cluster is authored. Runtime builds only.
+      // until a property in the cluster is authored, and shared with the
+      // object it was copied from until written. Runtime builds only.
       if (sidecars.isNotEmpty) {
         code.writeln('#ifndef $withRiveEditorPreprocessor');
         for (final entry in sidecars.entries) {
           final structName = _sidecarStructName(entry.value.first);
-          code.writeln('Sidecar<$structName> m_${entry.key};');
+          code.writeln('SharedSidecar<$structName> m_${entry.key};');
         }
         code.writeln('#endif');
       }
@@ -1153,7 +1154,7 @@
         }
         endPropertyGuards(copyBuf, property);
       }
-      // Deep-copy each sidecar cluster (Sidecar<T> handles null vs allocated).
+      // Share each sidecar cluster (SharedSidecar<T> copies it on write).
       if (sidecars.isNotEmpty) {
         code.writeln('#ifndef $withRiveEditorPreprocessor');
         for (final entry in sidecars.entries) {
diff --git a/include/rive/artboard.hpp b/include/rive/artboard.hpp
index 1db2b4a..52f67fe 100644
--- a/include/rive/artboard.hpp
+++ b/include/rive/artboard.hpp
@@ -246,6 +246,10 @@
   // the clone ends up with once initialized, so they don't grow an entry at
   // a time.
   static void reserveLikeSource(Core* clone, const Core* source);
+  // Sizes the lists an instance fills while it clones and initializes (its
+  // data binds, drawables, advancing components and the like) for the
+  // entries [source] has, which the instance ends up with too.
+  void reserveListsLike(const Artboard& source);
   // Lazily-built index of this artboard's data binds that target keyframes,
   // keyed by the (shared) keyframe. Keyframe binds live in dataBinds() but
   // are never applied directly (keyframes aren't Components); instead a
@@ -1096,6 +1100,7 @@
     artboardClone->m_instanceCount = m_instanceCount;
 #endif
     artboardClone->m_artboardSource = isInstance() ? m_artboardSource : this;
+    artboardClone->reserveListsLike(*this);
     cloneObjectDataBinds(this, artboardClone.get(), artboardClone.get());
 
     std::vector<Core*>& cloneObjects = artboardClone->m_Objects;
diff --git a/include/rive/data_bind/data_bind_container.hpp b/include/rive/data_bind/data_bind_container.hpp
index 1e1a68d..20af09e 100644
--- a/include/rive/data_bind/data_bind_container.hpp
+++ b/include/rive/data_bind/data_bind_container.hpp
@@ -82,6 +82,13 @@
   // in the frame. A no-op when the bind is not dirty.
   void flushDataBind(DataBind* dataBind) { updateDataBind(dataBind, false); }
   const std::vector<DataBind*>& dataBinds() const { return m_dataBinds; }
+  // Room for [count] binds, and for all of them to be dirty at once, as they
+  // are when first bound, for a container about to get that many.
+  void reserveDataBinds(size_t count)
+  {
+    m_dataBinds.reserve(count);
+    m_dirtyDataBinds.reserve(count);
+  }
   virtual void addDirtyDataBind(DataBind* dataBind);
   virtual void rebind() {};
   virtual void relinkDataContext() {};
diff --git a/include/rive/generated/component_base.hpp b/include/rive/generated/component_base.hpp
index 27bb458..3f437db 100644
--- a/include/rive/generated/component_base.hpp
+++ b/include/rive/generated/component_base.hpp
@@ -38,7 +38,7 @@
 #endif
   Id m_ParentId = 0;
 #ifndef WITH_RIVE_EDITOR
-  Sidecar<ComponentNameSidecar> m_name;
+  SharedSidecar<ComponentNameSidecar> m_name;
 #endif
 public:
 #ifdef WITH_RIVE_EDITOR
diff --git a/include/rive/generated/layout/layout_component_style_base.hpp b/include/rive/generated/layout/layout_component_style_base.hpp
index 006e3e7..02bf234 100644
--- a/include/rive/generated/layout/layout_component_style_base.hpp
+++ b/include/rive/generated/layout/layout_component_style_base.hpp
@@ -217,9 +217,9 @@
   float m_CornerRadiusBR = 0.0f;
 #endif
 #ifndef WITH_RIVE_EDITOR
-  Sidecar<LayoutComponentStyleBorderSidecar> m_border;
-  Sidecar<LayoutComponentStyleAbsolutePositionSidecar> m_absolutePosition;
-  Sidecar<LayoutComponentStyleCornerRadiusSidecar> m_cornerRadius;
+  SharedSidecar<LayoutComponentStyleBorderSidecar> m_border;
+  SharedSidecar<LayoutComponentStyleAbsolutePositionSidecar> m_absolutePosition;
+  SharedSidecar<LayoutComponentStyleCornerRadiusSidecar> m_cornerRadius;
 #endif
 public:
   inline float gapHorizontal() const { return m_GapHorizontal; }
diff --git a/include/rive/generated/layout/layout_sizing_style_base.hpp b/include/rive/generated/layout/layout_sizing_style_base.hpp
index 136d128..8911db2 100644
--- a/include/rive/generated/layout/layout_sizing_style_base.hpp
+++ b/include/rive/generated/layout/layout_sizing_style_base.hpp
@@ -87,7 +87,7 @@
   uint8_t m_JustifySelfValue = 6;
   uint8_t m_DisplayValue = 0;
 #ifndef WITH_RIVE_EDITOR
-  Sidecar<LayoutSizingStyleMinMaxSizingSidecar> m_minMaxSizing;
+  SharedSidecar<LayoutSizingStyleMinMaxSizingSidecar> m_minMaxSizing;
 #endif
 public:
 #ifdef WITH_RIVE_EDITOR
diff --git a/include/rive/generated/shapes/rectangle_base.hpp b/include/rive/generated/shapes/rectangle_base.hpp
index 892cca8..8136607 100644
--- a/include/rive/generated/shapes/rectangle_base.hpp
+++ b/include/rive/generated/shapes/rectangle_base.hpp
@@ -51,7 +51,7 @@
   float m_CornerRadiusBR = 0.0f;
 #endif
 #ifndef WITH_RIVE_EDITOR
-  Sidecar<RectangleCornerRadiusSidecar> m_cornerRadius;
+  SharedSidecar<RectangleCornerRadiusSidecar> m_cornerRadius;
 #endif
 public:
 #ifdef WITH_RIVE_EDITOR
diff --git a/include/rive/sidecar.hpp b/include/rive/sidecar.hpp
index 33c33ec..cdcd2c9 100644
--- a/include/rive/sidecar.hpp
+++ b/include/rive/sidecar.hpp
@@ -1,6 +1,8 @@
 #ifndef _RIVE_SIDECAR_HPP_
 #define _RIVE_SIDECAR_HPP_
 
+#include <atomic>
+#include <cstdint>
 #include <utility>
 
 namespace rive
@@ -87,5 +89,110 @@
   T* m_v = nullptr;
 };
 
+// A Sidecar for the generated authored property clusters (a component's name,
+// a layout style's cold fields), which a copy shares with the object it was
+// copied from until either side writes to it. Copying takes a reference
+// instead of allocating, and the first write to a shared cluster gives the
+// writer a copy of its own. An artboard instance clones every component of
+// its source, and almost none of them write these again, so most instances
+// never allocate their own.
+//
+// Writes go through ensureAllocated(); get() is read-only, so nothing can
+// write into a cluster another object still shares. The count is atomic as
+// instances of one source can be made on different threads.
+template <typename T> class SharedSidecar
+{
+public:
+  SharedSidecar() = default;
+  ~SharedSidecar() { release(); }
+
+  SharedSidecar(const SharedSidecar& other) : m_b(other.m_b) { retain(); }
+  SharedSidecar& operator=(const SharedSidecar& other)
+  {
+    if (m_b != other.m_b)
+    {
+      other.retain();
+      release();
+      m_b = other.m_b;
+    }
+    return *this;
+  }
+
+  SharedSidecar(SharedSidecar&& other) noexcept : m_b(other.m_b)
+  {
+    other.m_b = nullptr;
+  }
+  SharedSidecar& operator=(SharedSidecar&& other) noexcept
+  {
+    if (this != &other)
+    {
+      release();
+      m_b = other.m_b;
+      other.m_b = nullptr;
+    }
+    return *this;
+  }
+
+  const T* get() const { return m_b != nullptr ? &m_b->value : nullptr; }
+
+  // Allocate on first use, or copy a shared cluster, then return the backing
+  // object, ours alone. Generated setters and deserialize write through it.
+  T* ensureAllocated()
+  {
+    if (m_b == nullptr)
+    {
+      m_b = new Block();
+    }
+    else if (m_b->refs.load(std::memory_order_acquire) != 1)
+    {
+      Block* own = new Block(m_b->value);
+      release();
+      m_b = own;
+    }
+    return &m_b->value;
+  }
+
+  void reset()
+  {
+    release();
+    m_b = nullptr;
+  }
+
+#ifdef TESTING
+  // Whether another object holds this cluster too.
+  bool isShared() const
+  {
+    return m_b != nullptr && m_b->refs.load(std::memory_order_acquire) != 1;
+  }
+#endif
+
+private:
+  struct Block
+  {
+    Block() = default;
+    explicit Block(const T& from) : value(from) {}
+    T value;
+    std::atomic<uint32_t> refs{1};
+  };
+
+  void retain() const
+  {
+    if (m_b != nullptr)
+    {
+      m_b->refs.fetch_add(1, std::memory_order_relaxed);
+    }
+  }
+  void release()
+  {
+    if (m_b != nullptr &&
+        m_b->refs.fetch_sub(1, std::memory_order_acq_rel) == 1)
+    {
+      delete m_b;
+    }
+  }
+
+  Block* m_b = nullptr;
+};
+
 } // namespace rive
 #endif
diff --git a/include/rive/text/text.hpp b/include/rive/text/text.hpp
index 0e2f663..1d3a077 100644
--- a/include/rive/text/text.hpp
+++ b/include/rive/text/text.hpp
@@ -20,6 +20,7 @@
 #include "rive/dirtyable.hpp"
 #include "rive/enums.hpp"
 
+#include <algorithm>
 #include <functional>
 #include <memory>
 #include <string>
@@ -74,9 +75,25 @@
   std::vector<Unichar> m_value;
   std::vector<TextRun> m_runs;
 
+  template <typename T> static void growTo(std::vector<T>& list, size_t count)
+  {
+    if (count > list.capacity())
+    {
+      list.reserve(std::max(count, list.capacity() * 2));
+    }
+  }
+
 public:
   bool empty() const;
   void clear();
+  // Room for [unichars] characters in [runs] runs: exactly that the first
+  // time, and past it at least double, the way appending grows, so a text
+  // that grows a little at a time doesn't reallocate on every styling.
+  void reserve(size_t unichars, size_t runs)
+  {
+    growTo(m_value, unichars);
+    growTo(m_runs, runs);
+  }
   void append(
       rcp<Font> font,
       float size,
diff --git a/src/animation/state_machine_instance.cpp b/src/animation/state_machine_instance.cpp
index 98d226b..93e51fa 100644
--- a/src/animation/state_machine_instance.cpp
+++ b/src/animation/state_machine_instance.cpp
@@ -4409,6 +4409,7 @@
   // instance. That enables binding each instance to its own context without
   // polluting the rest.
   auto dataBindCount = machine->dataBindCount();
+  reserveDataBinds(dataBindCount);
   for (size_t i = 0; i < dataBindCount; i++)
   {
     auto dataBind = machine->dataBind(i);
diff --git a/src/artboard.cpp b/src/artboard.cpp
index 1cecec9..b91b9ab 100644
--- a/src/artboard.cpp
+++ b/src/artboard.cpp
@@ -1843,6 +1843,23 @@
   }
 }
 
+void Artboard::reserveListsLike(const Artboard& source)
+{
+  reserveDataBinds(source.dataBinds().size());
+  // Initialize fills these an entry at a time. The source's already hold its
+  // layout proxies, which the instance's get too.
+  m_Drawables.reserve(source.m_Drawables.size());
+  m_advancingComponents.reserve(source.m_advancingComponents.size());
+  m_hostDataContextConsumers.reserve(source.m_hostDataContextConsumers.size());
+  m_ArtboardHosts.reserve(source.m_ArtboardHosts.size());
+  m_NestedArtboards.reserve(source.m_NestedArtboards.size());
+  m_ComponentLists.reserve(source.m_ComponentLists.size());
+  m_clippingShapes.reserve(source.m_clippingShapes.size());
+  m_layerMasks.reserve(source.m_layerMasks.size());
+  m_DrawTargets.reserve(source.m_DrawTargets.size());
+  m_Joysticks.reserve(source.m_Joysticks.size());
+}
+
 void Artboard::cloneObjectDataBinds(
     const Core* object,
     Core* clone,
diff --git a/src/text/text.cpp b/src/text/text.cpp
index 4c55a65..9aabcdc 100644
--- a/src/text/text.cpp
+++ b/src/text/text.cpp
@@ -2011,6 +2011,21 @@
     float fontScale) const
 {
   styledText.clear();
+  // Room for the runs appended below, which skip a run with no font or no
+  // text. A run's UTF-8 bytes bound its characters.
+  size_t textBytes = 0;
+  size_t styledRuns = 0;
+  for (auto valueRun : m_allRuns)
+  {
+    auto style = valueRun->style();
+    if (style != nullptr && style->font() != nullptr &&
+        !valueRun->text().empty())
+    {
+      textBytes += valueRun->text().size();
+      styledRuns++;
+    }
+  }
+  styledText.reserve(textBytes, styledRuns);
   uint16_t runIndex = 0;
   for (auto valueRun : m_allRuns)
   {
@@ -2740,6 +2755,11 @@
   Text* twin = TextBase::clone()->as<Text>();
   twin->m_fileFeatures = m_fileFeatures;
   twin->m_file = m_file;
+#ifdef WITH_RIVE_TEXT
+  // Its runs add themselves one at a time once cloned.
+  twin->m_runs.reserve(m_runs.size());
+  twin->m_allRuns.reserve(m_allRuns.size());
+#endif
   return twin;
 }
 
diff --git a/tests/unit_tests/runtime/instance_list_sizing_test.cpp b/tests/unit_tests/runtime/instance_list_sizing_test.cpp
new file mode 100644
index 0000000..72d5fae
--- /dev/null
+++ b/tests/unit_tests/runtime/instance_list_sizing_test.cpp
@@ -0,0 +1,65 @@
+#include "rive/animation/state_machine.hpp"
+#include "rive/animation/state_machine_instance.hpp"
+#include "rive/artboard.hpp"
+#include "rive/file.hpp"
+#include "rive_file_reader.hpp"
+#include "rive_testing.hpp"
+
+static const char* sizingFiles[] = {
+    "assets/data_binding_test.riv",
+    "assets/data_binding_test_2.riv",
+    "assets/component_list_rtl_row.riv",
+    "assets/layout/layout_horizontal.riv",
+};
+
+TEST_CASE(
+    "An artboard instance sizes its lists like its source",
+    "[instancing]")
+{
+  size_t checked = 0;
+  for (const char* path : sizingFiles)
+  {
+    auto file = ReadRiveFile(path);
+    for (size_t i = 0; i < file->artboardCount(); i++)
+    {
+      auto source = file->artboard(i);
+      auto instance = source->instance();
+      REQUIRE(instance != nullptr);
+      // Sized once from the source, not grown an entry at a time.
+      CHECK(instance->drawables().size() == source->drawables().size());
+      CHECK(instance->drawables().capacity() == source->drawables().size());
+      CHECK(instance->dataBinds().size() <= source->dataBinds().size());
+      CHECK(instance->dataBinds().capacity() == source->dataBinds().size());
+      checked += source->drawables().size() + source->dataBinds().size();
+    }
+  }
+  // The files have something to size.
+  CHECK(checked > 0);
+}
+
+TEST_CASE(
+    "A state machine instance sizes its data binds like its machine",
+    "[instancing]")
+{
+  size_t checked = 0;
+  for (const char* path : sizingFiles)
+  {
+    auto file = ReadRiveFile(path);
+    for (size_t i = 0; i < file->artboardCount(); i++)
+    {
+      auto source = file->artboard(i);
+      auto instance = source->instance();
+      for (size_t m = 0; m < source->stateMachineCount(); m++)
+      {
+        auto machine = source->stateMachine(m);
+        auto machineInstance = instance->stateMachineAt(m);
+        REQUIRE(machineInstance != nullptr);
+        CHECK(
+            machineInstance->dataBinds().capacity() ==
+            machine->dataBindCount());
+        checked += machine->dataBindCount();
+      }
+    }
+  }
+  CHECK(checked > 0);
+}
diff --git a/tests/unit_tests/runtime/shared_sidecar_test.cpp b/tests/unit_tests/runtime/shared_sidecar_test.cpp
new file mode 100644
index 0000000..a24371e
--- /dev/null
+++ b/tests/unit_tests/runtime/shared_sidecar_test.cpp
@@ -0,0 +1,103 @@
+#include "rive/artboard.hpp"
+#include "rive/file.hpp"
+#include "rive/shapes/shape.hpp"
+#include "rive/sidecar.hpp"
+#include "rive/text/text_value_run.hpp"
+#include "rive_file_reader.hpp"
+#include "rive_testing.hpp"
+#include <string>
+
+namespace
+{
+struct Cluster
+{
+  std::string text;
+};
+} // namespace
+
+TEST_CASE("SharedSidecar shares a copy until either side writes", "[sidecar]")
+{
+  rive::SharedSidecar<Cluster> source;
+  CHECK(source.get() == nullptr);
+  source.ensureAllocated()->text = "source";
+
+  rive::SharedSidecar<Cluster> copy = source;
+  CHECK(copy.get() == source.get());
+  CHECK(source.isShared());
+
+  // The writer gets its own cluster; the other side keeps the old value.
+  copy.ensureAllocated()->text = "copy";
+  CHECK(copy.get() != source.get());
+  CHECK(source.get()->text == "source");
+  CHECK(copy.get()->text == "copy");
+  CHECK(!source.isShared());
+  CHECK(!copy.isShared());
+
+  // Writing to the source after sharing again leaves the copy alone too.
+  rive::SharedSidecar<Cluster> second;
+  second = source;
+  source.ensureAllocated()->text = "changed";
+  CHECK(second.get()->text == "source");
+  CHECK(source.get()->text == "changed");
+
+  // The last holder of a cluster frees it; the others still read it.
+  {
+    rive::SharedSidecar<Cluster> temporary = second;
+    CHECK(second.isShared());
+  }
+  CHECK(!second.isShared());
+  CHECK(second.get()->text == "source");
+
+  rive::SharedSidecar<Cluster> moved = std::move(second);
+  CHECK(second.get() == nullptr);
+  CHECK(moved.get()->text == "source");
+  moved.reset();
+  CHECK(moved.get() == nullptr);
+
+  // An empty cluster copies as empty.
+  rive::SharedSidecar<Cluster> empty;
+  rive::SharedSidecar<Cluster> emptyCopy = empty;
+  CHECK(emptyCopy.get() == nullptr);
+  CHECK(!emptyCopy.isShared());
+}
+
+TEST_CASE(
+    "An artboard instance shares its components' names with the source",
+    "[sidecar]")
+{
+  auto file = ReadRiveFile("assets/background_measure.riv");
+  auto source = file->artboard();
+  auto instance = file->artboardDefault();
+  REQUIRE(instance != nullptr);
+
+  auto sourceShape = source->find<rive::Shape>("background");
+  auto instanceShape = instance->find<rive::Shape>("background");
+  REQUIRE(sourceShape != nullptr);
+  REQUIRE(instanceShape != nullptr);
+  REQUIRE(sourceShape != instanceShape);
+  // Shared, not copied: both read the same string.
+  CHECK(&instanceShape->name() == &sourceShape->name());
+
+  auto sourceRun = source->find<rive::TextValueRun>("nameRun");
+  auto instanceRun = instance->find<rive::TextValueRun>("nameRun");
+  REQUIRE(sourceRun != nullptr);
+  REQUIRE(instanceRun != nullptr);
+  CHECK(&instanceRun->name() == &sourceRun->name());
+
+  // Renaming the instance's component leaves the source's name.
+  instanceShape->name("renamed");
+  CHECK(instanceShape->name() == "renamed");
+  CHECK(sourceShape->name() == "background");
+  CHECK(source->find<rive::Shape>("background") == sourceShape);
+
+  // And renaming the source's leaves an instance made before it.
+  auto second = file->artboardDefault();
+  sourceRun->name("sourceRenamed");
+  CHECK(sourceRun->name() == "sourceRenamed");
+  CHECK(instanceRun->name() == "nameRun");
+  CHECK(second->find<rive::TextValueRun>("nameRun") != nullptr);
+
+  // The source and its other instances outlive one instance's names.
+  instance = nullptr;
+  CHECK(second->find<rive::Shape>("background")->name() == "background");
+}
diff --git a/tests/unit_tests/runtime/text_run_sizing_test.cpp b/tests/unit_tests/runtime/text_run_sizing_test.cpp
new file mode 100644
index 0000000..a6577c0
--- /dev/null
+++ b/tests/unit_tests/runtime/text_run_sizing_test.cpp
@@ -0,0 +1,69 @@
+#include "rive/artboard.hpp"
+#include "rive/file.hpp"
+#include "rive/text/text.hpp"
+#include "rive_file_reader.hpp"
+#include "rive_testing.hpp"
+#include <vector>
+
+static std::vector<rive::Text*> textsOf(rive::Artboard* artboard)
+{
+  std::vector<rive::Text*> texts;
+  for (auto object : artboard->objects())
+  {
+    if (object != nullptr && object->is<rive::Text>())
+    {
+      texts.push_back(object->as<rive::Text>());
+    }
+  }
+  return texts;
+}
+
+TEST_CASE("A cloned text sizes its run list like its source", "[text]")
+{
+  size_t runs = 0;
+  for (const char* path :
+       {"assets/new_text.riv",
+        "assets/runtime_nested_text_runs.riv",
+        "assets/modifier_to_run.riv",
+        "assets/text_fit_test.riv"})
+  {
+    auto file = ReadRiveFile(path);
+    for (size_t i = 0; i < file->artboardCount(); i++)
+    {
+      auto source = file->artboard(i);
+      auto instance = source->instance();
+      auto sourceTexts = textsOf(source);
+      auto instanceTexts = textsOf(instance.get());
+      REQUIRE(sourceTexts.size() == instanceTexts.size());
+      for (size_t t = 0; t < sourceTexts.size(); t++)
+      {
+        const auto& sourceRuns = sourceTexts[t]->runs();
+        const auto& instanceRuns = instanceTexts[t]->runs();
+        CHECK(instanceRuns.size() == sourceRuns.size());
+        // Sized once, not grown a run at a time.
+        CHECK(instanceRuns.capacity() == sourceRuns.size());
+        runs += sourceRuns.size();
+      }
+    }
+  }
+  CHECK(runs > 0);
+}
+
+TEST_CASE("Styled text sizes exactly once, then grows like appending", "[text]")
+{
+  rive::StyledText styled;
+  styled.reserve(10, 2);
+  CHECK(styled.unichars().capacity() == 10);
+  CHECK(styled.runs().capacity() == 2);
+  // Enough room already: nothing changes.
+  styled.reserve(8, 1);
+  CHECK(styled.unichars().capacity() == 10);
+  CHECK(styled.runs().capacity() == 2);
+  // Growing past it at least doubles, so growing a little at a time doesn't
+  // reallocate every time.
+  styled.reserve(11, 3);
+  CHECK(styled.unichars().capacity() == 20);
+  CHECK(styled.runs().capacity() == 4);
+  styled.reserve(50, 3);
+  CHECK(styled.unichars().capacity() == 50);
+}