Skip to content

state: walk canonical paths in place (findDescendant 3.6 -> 0.3 us) - #970

Merged
transfix merged 1 commit into
masterfrom
perf/state-path-fast-walk
Nov 2, 2026
Merged

transfix merged 1 commit into
masterfrom
perf/state-path-fast-walk

Conversation

@transfix

@transfix transfix commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Every per-frame state read pays for the path walk: the conference client's room tree, the ImGui backends, widget expressions, and the three leaf lookups left in avatar_panel::update after #968. A 7-segment findDescendant("conf.rooms.room.participants.A.avatar.color") took about 3.6 µs natively. After this change it takes about 0.3 µs.

Why it was slow

Every walk reads a path the same way: split it on SEPARATOR, trim each segment, and drop the empty ones. findDescendant, findDescendantShared and findThroughLinks did that with normalize_state_path (split, then a locale-aware boost::trim on each segment, then a join), and then split and trimmed the result a second time. operator(), sharedChild and childThroughLinks split and trimmed once. Each character the trim tests goes through std::use_facet on a fresh std::locale, and in MSVC's runtime both of those take a lock.

What changed (src/cvc/state/state.cpp)

  • for_each_state_key(path, visit) passes each key to a walk in turn. All six walks use it.
    • Canonical path (every segment non-empty, and each segment's first and last byte is printable non-space ASCII): it is walked in place over a string_view, with one reused key buffer. There is no split, trim or join.
    • Anything else: the old code normalizes it first, and then the result is walked. Messy input therefore keeps the exact meaning it had.
  • normalize_state_path returns canonical input unchanged. That speeds up linkTo (both overloads), normalize_path and resolveLink. linkTo now calls its trim_copy only when the normalized target is empty.
  • Why a printable-ASCII edge byte can skip the trim: no locale counts printable non-space ASCII as whitespace. Every byte a locale might count (controls, the space, bytes ≥ 0x80) goes the old, locale-aware way.

child_map and the locking are unchanged: one lock per hop, released before the next hop, and each hop is still held as a state_ptr.

Semantics pinned (StatePathWalk.*, gates PRs)

The new tests compare each walk with a verbatim copy of the old normalization:

  • NormalizePathMatchesLegacy: normalize_path over a table of ~75 odd spellings. The table covers separator-only and whitespace-only input, leading, trailing and duplicate separators, whitespace around segments, whitespace inside a name, non-ASCII, DEL and control bytes at segment edges, and NULs.
  • ReadWalksMatchLegacy: findDescendant, findThroughLinks, and findDescendantShared over the same table. For findDescendantShared it also checks the pins, including the hops pinned before a miss.
  • CreateWalksMatchLegacy: operator(), sharedChild (with pins) and childThroughLinks. It also checks the set of nodes each one creates: exactly one per canonical hop.
  • RandomPathsMatchLegacy: 5000 seeded random strings over names, separators, whitespace, \xc3, \x7f and \0.
  • LinkTargetMatchesLegacy: the stored linkTarget() from both linkTo overloads, including the "." root marker.

These tests were run on unchanged master first and passed there, so the reference matches the old behaviour. They pass with this change too.

Measurements

StatePathWalkBenchmark.LookupCost was added. It prints only, and its suite name keeps it out of PR CI. Numbers are µs per lookup, with 8 peers in the room tree, on Windows, static Release, MSVC 14.44. Each figure is the median of 3 runs.

walk (7 segments) master this PR
findDescendant 3.63 0.30
findDescendantShared 3.75 0.40
findThroughLinks 3.74 0.46
operator() (existing node) 1.71 0.37
normalize_path 1.91 0.14
findDescendant, messy spelling (slow path) 3.93 2.50

Not measured in wasm.

Testing (native Windows, static Release, tests ON)

  • state_test, whole suite in one process: 182 ran, 181 passed, and 1 was skipped. The skipped test is PerformanceHierarchyBenchmark, which only runs with --enable-stress-tests.
  • state_test through ctest -j8 (one process per case): 182/182.
  • ctest -N -E "<PR exclude regex>" keeps all 5 StatePathWalk cases and drops the benchmark.
  • The 31 other test executables that call these walks (links, Ariadne, rooms, state_exec), each run as one process with PR CI's exclusions. 30 pass in full, 1,383 tests in all.
  • ariadne_host_test passes 42 tests and fails 11. The 11 are AriadneQuit.TheQuitEventCloses plus ten drain_until timeouts of about 39 s each (AriadneFunctions.*, AriadneModuleTick.*, AriadneRunProgram.AnActionsFailureNamesItsProgram, AriadneResidents.AKilledModuleTickIsRestarted, AriadneResourceIntrinsics.AnActionSeesItsLimits). The same 11 fail when the same tree is rebuilt with master's state.cpp, and in an unrelated branch's build on this box. They happen on this Windows box with or without this change, and they are not investigated here.

Every path walk reads a path as its SEPARATOR-split segments, each trimmed,
empty ones dropped. findDescendant, findDescendantShared and findThroughLinks
got there by normalizing (split, a locale-aware boost::trim per segment, join)
and then splitting and trimming again; operator(), sharedChild and
childThroughLinks split and trimmed once. The locale-aware trim costs a
use_facet per byte tested, which locks in MSVC's runtime.

The six walks now share for_each_state_key(). A canonical path (non-empty
segments whose first and last bytes are printable non-space ASCII, bytes no
locale counts as whitespace) is walked in place over a string_view with one
reused key buffer. Anything else is normalized by the old code first, so messy
input keeps its meaning. normalize_state_path() returns canonical input as is,
which covers linkTo(), normalize_path() and resolveLink().

StatePathWalk.* pins the semantics against a verbatim copy of the old
normalization: a table of odd spellings and 5000 seeded random strings,
through every walk, the pins findDescendantShared takes, the nodes the create
walks make, and both linkTo() overloads. They pass on master unchanged too.
StatePathWalkBenchmark.LookupCost prints the per-lookup cost (kept out of PR
CI by its name). Native Release, MSVC, 7-segment leaf: findDescendant
3.63 -> 0.30 us, findDescendantShared 3.75 -> 0.40, operator() 1.71 -> 0.37.
@transfix
transfix merged commit 98be6d3 into master Nov 2, 2026
27 checks passed
@transfix
transfix deleted the perf/state-path-fast-walk branch November 2, 2026 07:59
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