perf(runtime): build and sync layouts with fewer allocations (#15378)

* fix(runtime): list a layout's style among its children once

A layout's style names the layout as its parent, so the style's own
onAddedDirty already adds it to the layout's children. The layout's
onAddedDirty then added it again, so the runtime listed every layout's
style twice and every walk over a layout's children visited it twice.
The editor build dedupes children, so it never showed. The layout now
adds the style only when the style is parented somewhere else.

In the HUD perf tray, 359 of the 3,227 children added at load were
these duplicates, and an added leaderboard row makes 2 fewer
allocations.

Two state machine tests picked shapes by their position among the
artboard's children, a count that included the duplicate; they now find
the shapes by name. The test that an instance's child list is sized
exactly used a row whose three children counted the duplicate, so it now
uses a row that still has three.

* perf(runtime): keep a layout item's style appliers inline

Every layout item lists the objects that apply its style: the item
itself, its style and, under a grid, its placement. The list was a lazy
heap vector, so a layout component allocated the vector, then a buffer
for one applier, then grew it for the second and again for a placement.
The first three appliers now live inline in LayoutData, and only a
fourth moves the list to the heap.

LayoutData grows from 728 to 752 bytes, and each layout item drops a
vector and its buffer from the heap (48 to 64 bytes). In the HUD perf
tray, an added leaderboard row makes 18 fewer allocations.

* perf(runtime): leave Yoga's grid properties unallocated on flex layouts

Yoga keeps a node's grid tracks and lines in a box it allocates on the
first write, so a node that isn't a grid never pays for them. Every
layout that isn't a stack still wrote its four track lists on each style
sync, empty or not, which allocated that box for every flex layout. The
lists are now written only when the layout has tracks, or old ones to
clear.

In the HUD perf tray, an added leaderboard row makes 5 fewer allocations,
and each flex layout drops its unused grid box from the heap.

* perf(runtime): size a layout's background path once

Path::addRoundedRect built a layout's background a verb and a point at a
time, so its arrays grew several times on the first build. It now
reserves the outline's exact size first: a move, four lines and a close,
plus a cubic for each rounded corner.

In the HUD perf tray, an added leaderboard row makes 6 fewer
allocations.

* perf(runtime): list an artboard's dirty layouts in a vector

An artboard kept the layouts whose style changed in an unordered_set, so
marking a layout allocated a node, and a busy set its buckets too. They
are now a vector in the order they were marked, and each layout says it
is listed: a layout in a spare bit of its flags, and an artboard listed
by its host in a bool, since an artboard can be listed by its host and by
itself at once. Both sit in existing padding, and Artboard shrinks from
1096 to 1080 bytes.

An artboard now leaves its own and its host's lists in ~Artboard, while
it is still whole. ~LayoutComponent used to do that for it, after
Artboard's members, the list among them, were already destroyed. An
artboard moving to another host also leaves the old host's list.

The editor moves a component to another artboard by setting its artboard
directly, and clears it that way before it frees or orphans one. A layout
moving that way leaves the old artboard's list first, and so do the
artboards a host moving that way lays out, a list's pooled rows among
them. Otherwise a moved one's flag would stay set, so the new artboard
would never list it, and a freed one would stay listed for the next sync
to read. Deleting a listed layout in the editor did that before this
change too: its destructor leaves the list through its artboard, which
the editor had already cleared.

In the HUD perf tray, an added leaderboard row makes 4 fewer allocations.

* perf(runtime): keep Yoga's child list when a layout's children didn't change

syncLayoutChildren removed every child from the layout's Yoga node,
which also freed the node's child list, and then added the children back
one at a time, growing a fresh list and a scratch list on every sync. A
sync now collects the children first, into a scratch list sized for the
ones the node had. When they are the same children in the same order, it
resets them as removing and adding them again would and keeps Yoga's
list as it is.

The children's layouts are still reset and the node is still marked
dirty, so a layout solves exactly as before.

In the HUD perf tray, an added leaderboard row makes 3 fewer
allocations, all in the root re-sync that hosting the row does.

* perf(runtime): host list rows without syncing the host's root layout

Hosting an artboard that shares its host's layout synced the layout
children of the artboard holding the host, its root, once per artboard.
A component list syncs its own layout parent once its rows are in place,
which is the layout that actually holds them and may not be the root, so
adding N rows synced the root N times for nothing: each sync collected
the root's children again and reset their Yoga layouts. A host now says
whether it syncs its layout parent itself, and the list does.

Nested artboard layouts still sync the root when they mount an artboard.

In the HUD perf tray, adding 400 leaderboard rows no longer syncs the
hud_race artboard's root layout 400 times, and makes 1 fewer allocation
per row.

* perf(runtime): tell whether a list shows a row's artboard without scanning

Every time a row's layout changed, the list checked that the row's
artboard was showing by scanning all its realized rows, so adding N rows,
each marking its layout dirty several times while it set up, cost O(N^2).
Putting an artboard in a row now tells it the row, and the list checks
that one row first. Without repeated items an artboard sits in one row at
most, the last it was put in, so a row that no longer holds it means none
does. A list that repeats items still scans when that row doesn't match.

The row sits in Artboard's padding, so Artboard stays 1080 bytes.

With the seven commits before it (CLI bench, M2 Max): in the HUD perf
tray an added leaderboard row makes 39 fewer allocations (707.7 to
668.7) and a lap time row 45 fewer, and Discord clone server and channel
switches allocate 2 to 6% less. Adding 400 leaderboard rows
measures 5.7% faster, 100 rows 7.8% and 100 lap time rows 8.4%; removing
rows and idle frames are unchanged within noise. Pixels match at all 69
bench checkpoints of the HUD, Discord clone and calendar trays.

* perf(runtime): tell a hosted artboard from a dirty layout without a virtual call

Marking a layout dirty asks whether it's an artboard its host listed, and
is<Artboard>() answered that with a virtual call on every mark. A state
machine that keys layout properties many times a frame stalled on it: since
the dirty layouts became a vector, one UAT corpus file (29581) ran 7% more
cycles per frame on fewer instructions. An artboard is its own artboard(),
so comparing that answers without the call.

In 29581, a steady frame takes 1.24 ms instead of 1.35 ms, and 18.9M
instead of 19.7M instructions (1.30 ms and 20.5M before this branch). The
corpus draws the same frames and makes the same allocations.

Rive-Mono-Commit: 4ca5e1e3e77075460ed1f466d6c09cc94cb9ea4f
18 files changed
tree: 0ef59c7b7bc8204d2f17f275a21797fe931cfa81
  1. .github/
  2. .vscode/
  3. build/
  4. decoders/
  5. dependencies/
  6. dev/
  7. include/
  8. renderer/
  9. rivinfo/
  10. scripting/
  11. skia/
  12. src/
  13. tests/
  14. utils/
  15. .dockerignore
  16. .gitattributes
  17. .gitignore
  18. .lua-format
  19. .rive_head
  20. .version
  21. Dockerfile
  22. Doxyfile
  23. dummy.cpp
  24. LICENSE
  25. premake5_v2.lua
  26. README.md
  27. runtime.natvis
README.md

Build Status Discord badge Twitter handle

rive-runtime

Rive hero image

Rive's C++ runtime — the lowest-level Rive runtime. Loads .riv files, advances state machines and animations, and draws via the abstract Renderer interface. The built-in GPU renderer (RiveRenderer) has RenderContextImpl backends for Metal, Vulkan, D3D11, D3D12, and OpenGL/WebGL.

Rive's Apple, Android, Flutter, Unity, Unreal, and web runtimes all wrap this library.

Features:

  • Loading artboards and their contents from .riv files.
  • Querying state machines from artboards.
  • Mutating the artboard hierarchy (the same mechanism used by state machines) and efficiently solving those changes via Artboard::advance.
  • A state-of-the-art vector renderer for Metal, Vulkan, D3D12, D3D11, and OpenGL/WebGL.
  • An abstract Renderer interface for hooking up an external vector renderer.

Prerequisites

  • A C++17 toolchain:
    • macOS: clang from Xcode Command Line Tools (xcode-select --install).
    • Linux: clang from your distro (e.g. apt install clang).
    • Windows: Visual Studio 2022 with the C++ Clang Compiler for Windows and MSBuild support for LLVM (clang-cl) toolset individual components.
  • git — the build script clones a pinned premake5 on first run. On Windows, install Git for Windows and during setup pick “Use Git and optional Unix tools from the Command Prompt” so that sh.exe ends up on PATH.
  • Platform SDK for the renderer you're targeting:
    • macOS / iOS: Xcode.
    • Windows: Windows SDK (for D3D).
    • Linux: a Vulkan or OpenGL development environment.
    • Vulkan: the Vulkan SDK.

Build

The build is driven by premake5 wrapped by a helper script:

  • build/build_rive.sh — macOS / Linux (bash) / Windows (MinGW).
  • build/build_rive.ps1 — a PowerShell convenience wrapper for Windows; it shells out to the bash script, so a bash environment (Git for Windows / MinGW, see Prerequisites) must still be on PATH.

The helper self-installs the pinned premake version on first run and dispatches to the right build system for your platform (gmake2 on macOS/Linux, MSBuild on Windows). It must be run from a directory that contains a premake5.lua — typically tests/, which builds the core library, the GPU renderer, and the player sample app.

macOS / Linux

git clone https://github.com/rive-app/rive-runtime.git
cd rive-runtime/tests
../build/build_rive.sh release

Windows

git clone https://github.com/rive-app/rive-runtime.git
cd rive-runtime\tests
..\build\build_rive.ps1 release

Common variants

(On Windows, substitute build_rive.ps1 for build_rive.sh.)

  • build_rive.sh (no args) — debug build for the host.
  • build_rive.sh release clean — clean then build release.
  • build_rive.sh ninja release — use Ninja instead of make/MSBuild.
  • build_rive.sh ios release — cross-compile for iOS.
  • build_rive.sh android release — cross-compile for Android (defaults to arm64).
  • build_rive.sh ninja release wasm — cross-compile for WebAssembly.
  • build_rive.sh --toolset=msc release (Windows) — build with MSVC's cl.exe instead of clang-cl. The build supports both toolchains; the lua warning suppressions cover MSVC too.

See the comment header at the top of build/build_rive.sh for the complete flag reference.

Build outputs

Artifacts land in out/<config>/ relative to the directory you built from (typically tests/). The config directory encodes any OS/arch flags you passed:

  • out/release, out/debug — host build.
  • out/ios_release, out/android_arm64_release, out/wasm_release — cross-compile builds.
ArtifactDescription
librive.a / rive.libCore runtime library.
librive_pls_renderer.a / rive_pls_renderer.libGPU renderer.
player / player.exeSample app — loads and renders a .riv file.
out/<config>/goldens, out/<config>/gms, out/<config>/benchTest harness binaries (regression, golden-image, benchmarking).
librive_decoders.a, librive_harfbuzz.a, …Supporting libraries.

Testing

The runtime's primary form of testing is golden testing — rendering known scenes and diffing the output against checked-in reference images via the goldens and gms test harness binaries (built into out/<config>/goldens and out/<config>/gms). See tests/ for how to run and rebaseline goldens.

Unit tests are secondary and use the Catch2 framework. From the repo root:

cd tests/unit_tests
./test.sh

Unit tests live in tests/unit_tests/runtime/ (core runtime) and tests/unit_tests/renderer/ (renderer). To add a test, create an xxx_test.cpp file in the appropriate directory — the harness picks it up automatically.

Code formatting

rive-runtime uses clang-format.

  • macOS: brew install clang-format.
  • Windows: already installed if you have the prerequisites — VS 2022's C++ Clang Compiler component ships clang-format.exe alongside clang-cl.exe at C:\Program Files\Microsoft Visual Studio\2022\<edition>\VC\Tools\Llvm\x64\bin\. You can also install LLVM standalone from llvm.org or via winget install LLVM.LLVM.
  • Linux: install via your distro's package manager (e.g. apt install clang-format).

Memory checks (macOS only)

To run the tests under macOS's built-in leaks tool:

cd tests/unit_tests
./test.sh memory

This wraps the test binary with leaks --atExit, which ships with macOS — no install required. The memory flag is ignored on Linux and Windows.

Disassembly explorer (macOS / Linux)

To inspect generated assembly per cpp file, install the Disassembly Explorer VSCode extension. A disassemble task is provided in .vscode/tasks.json. The underlying gen assembly task invokes clang++ directly, so it needs clang++ on PATH — works out of the box on macOS (Xcode CLI tools) and most Linux distros, but not on a default Windows + VS install (which provides clang-cl.exe, not clang++.exe).

Reach the task from Tasks: Run Task, or bind a key in keybindings.json:

[
    {
        "key": "cmd+d",
        "command": "workbench.action.tasks.runTask",
        "args": "disassemble"
    }
]