perf(core): drop quadratic locator walks and end positioned taps at their timeout

Merged
KR
2d ago
+330-118

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. indexTree records each node's pre-order range once, so "is X inside Y" is an interval test and the candidates of any filter match or scope are 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; compareText delegates to it, so there is still a single implementation.
  • performWithin uses pollReads. 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 positioned tap({ 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 as LOCATOR_NOT_FOUND instead of ACTION_FAILED.
  • Retried locates stop at the deadline. resolveOnce caps its retry sleep (after a retryable error such as FRAME_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:

casenodesmainthis PRspeedup
filter({ has: button "Delete" })8023.36 ms0.050 ms68×
filter({ hasText })8023.43 ms0.081 ms42×
getByText innermost, 200 matches8021.59 ms0.076 ms21×
role in a listitem scope8021.20 ms0.048 ms25×
filter({ has: button "Delete" })8,002354 ms0.73 ms488×
filter({ hasText })8,002372 ms1.04 ms359×
getByText innermost, 2,000 matches8,002211 ms0.97 ms217×
role in a listitem scope8,002121 ms0.67 ms181×
matchesText × 10k1.98 ms2.02 mssame

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, selector with and without the hook), main's built resolve.js against 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 has ranges, and a selector inside has seeing only that candidate's descendants; they pass on main's build too, so they pin existing behavior.

  • performWithin deadline, on the fake clock:

    350 ms timeoutmainthis branch
    positioned tap, node never gets a boxLOCATOR_NOT_FOUND at 400 msLOCATOR_NOT_FOUND at 350 ms
    positioned tap, box appears during a read started at the deadlineLOCATOR_NOT_FOUND at 410 msLOCATOR_NOT_FOUND at 350 ms
    locator on a frame that stays missingLOCATOR_NOT_FOUND at 400 msLOCATOR_NOT_FOUND at 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.