Repository navigation
state: walk canonical paths in place (findDescendant 3.6 -> 0.3 us) - #970
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::updateafter #968. A 7-segmentfindDescendant("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,findDescendantSharedandfindThroughLinksdid that withnormalize_state_path(split, then a locale-awareboost::trimon each segment, then a join), and then split and trimmed the result a second time.operator(),sharedChildandchildThroughLinkssplit and trimmed once. Each character the trim tests goes throughstd::use_faceton a freshstd::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.string_view, with one reused key buffer. There is no split, trim or join.normalize_state_pathreturns canonical input unchanged. That speeds uplinkTo(both overloads),normalize_pathandresolveLink.linkTonow calls itstrim_copyonly when the normalized target is empty.child_mapand the locking are unchanged: one lock per hop, released before the next hop, and each hop is still held as astate_ptr.Semantics pinned (
StatePathWalk.*, gates PRs)The new tests compare each walk with a verbatim copy of the old normalization:
NormalizePathMatchesLegacy:normalize_pathover 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, andfindDescendantSharedover the same table. ForfindDescendantSharedit also checks the pins, including the hops pinned before a miss.CreateWalksMatchLegacy:operator(),sharedChild(with pins) andchildThroughLinks. 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,\x7fand\0.LinkTargetMatchesLegacy: the storedlinkTarget()from bothlinkTooverloads, 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.LookupCostwas 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.findDescendantfindDescendantSharedfindThroughLinksoperator()(existing node)normalize_pathfindDescendant, messy spelling (slow path)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 isPerformanceHierarchyBenchmark, which only runs with--enable-stress-tests.state_testthroughctest -j8(one process per case): 182/182.ctest -N -E "<PR exclude regex>"keeps all 5StatePathWalkcases and drops the benchmark.state_exec), each run as one process with PR CI's exclusions. 30 pass in full, 1,383 tests in all.ariadne_host_testpasses 42 tests and fails 11. The 11 areAriadneQuit.TheQuitEventClosesplus tendrain_untiltimeouts 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'sstate.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.