Skip to content

Ariadne runtime frame: state paths kept resolved between frames (structure epoch, state_key) - #1477

Merged
transfix merged 2 commits into
masterfrom
perf/ariadne-runtime-frame-epoch
Nov 23, 2026
Merged

transfix merged 2 commits into
masterfrom
perf/ariadne-runtime-frame-epoch

Conversation

@transfix

Copy link
Copy Markdown
Owner

The Ariadne runtime frame (AriRuntime::frame → AppRuntime::drain) looked up every key it read or wrote by path, every frame. That cost is most of the frame in cvcdbg's demo3-ari. This keeps those paths resolved between frames. The keys, their text and when they're written stay the same.

Where demo3-ari's frame went (Chromium profile of AppRuntime::drain, CVC-DBG/cvcdbg's findings of 2026-11-22):

  • tick_scene was 74% of the drain. SceneWorld::follow was 48% of that: WorldVehicles::publish_entities took 35% of follow and SceneWorld::publish_stats 31.5%.
  • GlSceneAdapter::sync_visibility was 24%, and RibbonBinding::tick 10%.
  • Self time went to cvc::state path lookups (findThroughLinksShared, findDescendant, operator()), state::instance (a named mutex and a data-map lookup per call), number formatting and mutexes.

What changes

cvc::state::structureEpoch()

  • A count shared by every node of a tree.
  • It moves when a node is made or taken out (removeChild, sweepExpired, reset(false)), or when a link is set, cleared, or changes its mode or writability. Value and data writes never move it.
  • While it holds still, every path resolves as it did before, through links or not, so a cached miss is as good as a cached hit.

cvc::state_key (inc/cvc/state/state_key.h, header-only)

  • A path resolved once and walked again only after the epoch moved. It has two views, physical (findDescendant / operator()) and through links (findThroughLinks / childThroughLinks), so a cached key resolves exactly as the call it replaces.
  • It pins what it resolved in pin_vectors (the node before its ancestors), so it never dangles. A node taken out is let go at the key's next look.
  • One owner's.
  • Documented in docs/STATE_API.md ("Paths Kept Resolved").

What keeps its paths resolved now

  • cvc::ariadne bind reads (bind.h): bind_key, plus read_or, read_bool_or and write overloads that take a key. They cover:
    • scene visible: (sync_scene_visibility);
    • bound clip offsets;
    • bound opacity:;
    • repeat: counts;
    • ribbon and draped-link sources and their bound numbers (window:, value:, opacity:).
  • SceneWorld:
    • each physics.stats.* leaf, the hash switch, the enabled key and the world's source key are kept resolved;
    • step_ms_history is kept as text, each step's ms formatted once when it steps, instead of all 120 values re-formatted every frame;
    • num() uses snprintf("%.17g"), which writes the same text as the ostream at precision(17) it replaces;
    • the nodes line counts a node's bodies without a map lookup per body.
  • WorldVehicles: every put, get and seed, the pose and intent keys read before each physics step, the packed poses keys and the poses table are kept resolved. The cache is bounded at 4096 paths. It's only used on the world's thread; the methods any thread may call don't touch state.
  • world_clock_binding: its 10 control and telemetry keys, which a scene's clock pulls and pushes every frame.

No rate cap. Telemetry is still written every frame it was written before. A cap would change what a reader sees right after a follow (physics.stats.tick), and the native numbers below didn't need one.

Measured

cvcgl_ariadne_frame (new, headless) is a scene shaped like demo3-ari:

  • 12 pose vehicles placed from a host's table rewritten every frame;
  • a growing track and a windowed route spine per vehicle (ribbons from state);
  • 6 draped links with bound colour and opacity;
  • 8 jam discs;
  • 39 bound visible:s.

It times sync_scene_visibility + tick_scene, which is what AppRuntime::drain runs under AriRuntime, over 600 frames after 120 warm-up frames. Release build, Linux, 4 alternating runs of each, with the machine quieter (load ~35 on 32 cores):

master (96b2702 + this test) this branch
frame, mean 1.17–1.21 ms 0.60–0.70 ms
frame, p50 1.14–1.17 ms 0.59–0.61 ms
frame, p90 1.26–1.32 ms 0.67–1.06 ms
thread CPU / frame 1.17–1.21 ms 0.60–0.70 ms
visible: sync / frame 89–98 µs 9.5–11 µs

The tree's shape moved 0 times in the timed frames, so no kept path walked again.

Instructions inside tick_scene (callgrind, the same ~730 frames, so load doesn't affect them):

master this branch
tick_scene 3226 M 1628 M −50%
SceneWorld::follow 2026 M 675 M −67%
WorldVehicles::publish_entities 953 M 238 M −75%
SceneWorld::publish_stats 502 M 57 M −89%
WorldVehicles::read_pose_keys (before each step) 243 M 103 M −58%
ribbon tick 197 M 63 M −68%
draped-link tick 78 M 27 M −66%

About 320 M in both is the vehicles' looks being realized while they spawn. Without it, a steady frame is 3.98 M → 1.79 M instructions (−55%). Most of what's left is cvcGL posing the box look's chassis and wheels (physics_scene_binding::present, pose_vehicle_wheels), not state. demo3-ari's model look poses no wheels.

Web numbers aren't measured here. The web demos run cvcpkg's published libcvc, so demo3-ari gets this only through a libcvc release plus a cvcdbg pin. Its Firefox/Chromium frame histogram needs that build.

Tests

  • state_test: StateStructureEpoch.*:
    • the epoch moves on every shape change and on no write, including a sweep;
    • a key follows its path across removal and remaking;
    • a through-links key follows a link set, moved, cleared and made writable, and one whose broken target is made later;
    • a key pins what it resolved and lets go of it.
  • ariadne_bind_test: SyncFollowsKeysTakenOutAndLinksSet, where visibility follows:
    • a source deleted and made again;
    • a target taken out;
    • a link set over the source's parent;
    • a binding whose path changes.
  • cvcgl_ariadne_frame checks that the frame still publishes what it did:
    • physics.stats.*, at two ticks a 60 Hz frame;
    • each vehicle's entities.<id>.*, equal to its pose;
    • visible: following written and deleted keys;
    • a deleted telemetry key published again the next frame, and the whole stats subtree likewise;
    • jam visibility read through a transparent link, then through its new target, then without it.

…cture epoch, state_key)

The runtime frame (AriRuntime::frame -> AppRuntime::drain) looked up every key it read or wrote
by path every frame: the root through the app's data map under a named mutex, then a lock and a
map search per segment. In demo3-ari that and the telemetry's formatting were most of the frame.

- cvc::state::structureEpoch(): a count shared by a tree's nodes that moves when a node is made
  or taken out, or a link is set, cleared or changes mode or writability; never on a write.
- cvc::state_key (state_key.h): a path resolved once (physical or through links, as the call it
  replaces), walked again only after the epoch moved; it pins what it resolved (pin_vector).
- Kept this way: scene visible:, opacity:, clip offsets, repeat: counts, ribbon and link sources
  and bound numbers, the scene clock's keys, SceneWorld's physics.stats.* and its keys,
  WorldVehicles' telemetry, pose, intent and table keys.
- SceneWorld: step_ms_history formatted once a step, num() by snprintf("%.17g") (the same text
  as the ostream it replaces). WorldVehicles: an entity's numbers formatted again only when they
  changed, stats lines likewise, parse_poses without per-character strings (from_chars where it
  reads the whole token, else stod), the poses table's specs made only to spawn.
- state::value(string): one lock for the link, read-only and equality checks.

Same keys, same text, written as often. cvcgl_ariadne_frame (a demo3-ari-shaped scene, 12 pose
vehicles) times the adapter's frame: 1.17-1.21 ms -> 0.60-0.70 ms here, visible: sync 89-98 us
-> 9.5-11 us; tick_scene's instructions -50% (callgrind).
…e-frame-epoch

# Conflicts:
#	src/cvc/ariadne/scene_world.cpp
@transfix
transfix merged commit c6b091d into master Nov 23, 2026
30 of 31 checks passed
@transfix
transfix deleted the perf/ariadne-runtime-frame-epoch branch November 23, 2026 19:09
transfix added a commit that referenced this pull request Nov 23, 2026
loader.cpp conflicted where both sides add functions after load_file, in the
yaml build and in the stub: this branch's validate_string/validate_file and
master's parse_value (#1477). Both kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant