perf(core): drop quadratic locator walks and end positioned taps at their timeout
resolveExpression (the locator semantics the desktop engine and custom engines run over a semantic tree) walked up a parent map for every containment check and copied a candidate array per filter match. On a large screen that is quadratic: a filter({ has }) over 1,000 list rows took ~350 ms per resolve, and resolves run on every poll.
- Subtree intervals.
indexTreerecords each node's pre-order range once, so "is X inside Y" is an interval test and the candidates of anyfiltermatch orscopeare one contiguous slice of the pre-order array (packages/core/src/engine/resolve.ts). - Linear
innermostOnly. In document order a match contains another match exactly when it contains the next one, so the innermost pass is one scan instead of all pairs. - Matchers compiled once per pass, not per node.
compileTextMatcher(packages/core/src/internal/text.ts) is now the one text rule;compareTextdelegates to it, so there is still a single implementation. performWithinusespollReads. Its hand-rolled poll loop is replaced by the shared one (packages/core/src/locator/engine.ts). Same error and cut-off handling, with two visible differences: a positionedtap({ position })no longer sleeps past its timeout (the old loop overshot by up to one poll delay), and it skips the read at the deadline, since a box found then could never be tapped, so the wait ends asLOCATOR_NOT_FOUNDinstead ofACTION_FAILED.- Retried locates stop at the deadline.
resolveOncecaps its retry sleep (after a retryable error such asFRAME_NOT_FOUND) at the time left, so a locator waiting on a missing frame ends at its timeout instead of the next poll tick.
resolveExpression's results, error codes, and which error it throws first are unchanged (see Verified). A filter whose source matches nest (groups inside groups) still scans each match's subtree, so hasText there costs nodes × depth.
Benchmark
resolveExpression median per call, a list screen with echoed labels as iOS reports them, main vs this branch:
| case | nodes | main | this PR | speedup |
|---|---|---|---|---|
filter({ has: button "Delete" }) | 802 | 3.36 ms | 0.050 ms | 68× |
filter({ hasText }) | 802 | 3.43 ms | 0.081 ms | 42× |
getByText innermost, 200 matches | 802 | 1.59 ms | 0.076 ms | 21× |
role in a listitem scope | 802 | 1.20 ms | 0.048 ms | 25× |
filter({ has: button "Delete" }) | 8,002 | 354 ms | 0.73 ms | 488× |
filter({ hasText }) | 8,002 | 372 ms | 1.04 ms | 359× |
getByText innermost, 2,000 matches | 8,002 | 211 ms | 0.97 ms | 217× |
role in a listitem scope | 8,002 | 121 ms | 0.67 ms | 181× |
matchesText × 10k | 1.98 ms | 2.02 ms | same |
Verified
Ran it locally: yes
-
Differential fuzz: 50,000 random trees × random expressions (every kind, nested
has/hasNot/scope/union, invalid regexps, non-string roles,frame,selectorwith and without the hook), main's builtresolve.jsagainst this branch's: 0 differences in result ids or thrown error code and message. -
New unit tests pin innermost matches across deep and distant echoes, nested
hasranges, and aselectorinsidehasseeing only that candidate's descendants; they pass on main's build too, so they pin existing behavior. -
performWithindeadline, on the fake clock:350 ms timeout main this branch positioned tap, node never gets a box LOCATOR_NOT_FOUNDat 400 msLOCATOR_NOT_FOUNDat 350 mspositioned tap, box appears during a read started at the deadline LOCATOR_NOT_FOUNDat 410 msLOCATOR_NOT_FOUNDat 350 mslocator on a frame that stays missing LOCATOR_NOT_FOUNDat 400 msLOCATOR_NOT_FOUNDat 350 ms -
pnpm test:testbed: 598 passed, 3 skipped.pnpm test:web-benchmark: 95 passed (43 files).
Reviewed with sol-loop: 2 rounds with gpt-6.2-sol (before the review fixes in 8422ef8). Sol verdict: approved
Changes made by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, running in T3 Code.