Skip to content

Commit 1d13673

Browse files
authored
perf(find): reuse snapshot index once per ranking pass (#1971)
* refactor(find): isolate match ranking policy Move find's candidate ordering — on-screen preference, actionability scoring, and the area/input-order tie-break — out of find-match-resolution.ts into a focused sibling. Pure move: no score input, ranked order, ambiguity refusal, or --first/--last behavior changes. The extraction establishes the seam the follow-up indexing change needs, so the ranking pass has one production entry point to build a topology in. Refs #1690 * perf(find): index snapshot topology once per match ranking pass Ranking a mutating find's candidates asked the whole tree the same three questions once per candidate: same-rect descendants filtered every node, the nearest hittable ancestor rebuilt a full index map, and the overly-broad-ancestor check re-filtered every node for viewport roots. With m matches over an n-node capture that is O(m x n) full-tree work before find can act or refuse. buildActionableTouchTopology reads those three collections in one pass (nodes by index, children by parent index, normalized viewport-root rects). preferOnscreenMatches builds exactly one per multi-match pass and threads it through every score; resolveActionableTouchResolution takes it as an optional argument so one-off interaction callers keep the cheap two-argument shape. findNearestAncestor gained the same optional prebuilt map its snapshot-presentation sibling already accepted, and classifyActionableTouchCandidates now reuses one topology instead of building a bare index map and re-resolving per candidate. Score inputs, ranked order, area and input-order tie-breaks, ambiguity refusal, and --first/--last are unchanged; only the derivation is shared. Observed red first: with the wiring hunk removed, the new ranking regression reports builder calls 0 (expected 1) and 64 filter + 32 map whole-tree scans (expected 0) over 32 candidates. The topology docstring records two seams the reviewer asked for. #1690 names src/snapshot/snapshot-processing.ts as findNearestAncestor's home; that path is gone and packages/contracts/src/snapshot-tree.ts is the seam that replaced it, so the issue's file list is drifted rather than a second site to change. And viewportRootRects is not interchangeable with snapshot-visibility's precomputedViewportRects: normalizeRect drops negative width/height where hasValidRect keeps them, which changes which rect wins pickLargestRect. Refs #1690 * refactor: hide actionable touch indexing
1 parent 443bbd0 commit 1d13673

13 files changed

Lines changed: 550 additions & 165 deletions

‎packages/contracts/src/snapshot-tree.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export function findSnapshotAncestor<T>(
3131
return null;
3232
}
3333

34-
/**
35-
* Returns the nearest ancestor matching `predicate`; false means keep walking.
36-
*/
34+
/** Returns the nearest ancestor matching `predicate`; false means keep walking. */
3735
export function findNearestAncestor(
3836
nodes: SnapshotNode[],
3937
node: SnapshotNode,

‎packages/contracts/src/snapshot.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ test('findNearestAncestor adapts a predicate to the shared tree walk', () => {
7474
{ ref: 'e20', index: 20, parentIndex: 10, type: 'Cell' },
7575
];
7676

77-
assert.equal(
78-
findNearestAncestor(nodes, nodes[1]!, (ancestor) => ancestor.type === 'Window')?.index,
79-
10,
80-
);
77+
const isWindow = (ancestor: SnapshotNode) => ancestor.type === 'Window';
78+
79+
assert.equal(findNearestAncestor(nodes, nodes[1]!, isWindow)?.index, 10);
8180
});
8281

8382
test('snapshot tree and scroll semantics identify nodes through their stable indexes', () => {

‎src/core/interaction-targeting.fixtures.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,110 @@ export const ELEMENT14_DISTINCT_SUBTREE_NODES: RawSnapshotNode[] = [
7474
hittable: true,
7575
},
7676
];
77+
78+
/**
79+
* One node per decision `resolveActionableTouchResolution` can reach, so an
80+
* indexed pass and an unindexed one can be compared across the whole policy
81+
* rather than on the branch a single example happens to hit: a same-rect
82+
* actionable descendant (1 -> 2), semantic targets (3, 4), a nonhittable leaf
83+
* under a hittable ancestor (5), both overly-broad shapes — a scrolling
84+
* container (7) and the viewport-sized root (10) — a covered node (8), and a
85+
* parentless, rectless node with no usable target at all (9).
86+
*/
87+
export const INDEXED_PARITY_POLICY_NODES: RawSnapshotNode[] = [
88+
{
89+
index: 0,
90+
depth: 0,
91+
type: 'XCUIElementTypeApplication',
92+
rect: { x: 0, y: 0, width: 390, height: 844 },
93+
hittable: true,
94+
},
95+
{
96+
index: 1,
97+
depth: 1,
98+
parentIndex: 0,
99+
type: 'XCUIElementTypeOther',
100+
label: 'Save wrapper',
101+
rect: { x: 20, y: 100, width: 120, height: 44 },
102+
hittable: false,
103+
},
104+
{
105+
index: 2,
106+
depth: 2,
107+
parentIndex: 1,
108+
type: 'XCUIElementTypeImage',
109+
identifier: 'save-hit-area',
110+
rect: { x: 20, y: 100, width: 120, height: 44 },
111+
hittable: true,
112+
},
113+
{
114+
index: 3,
115+
depth: 1,
116+
parentIndex: 0,
117+
type: 'XCUIElementTypeButton',
118+
label: 'Save',
119+
rect: { x: 20, y: 200, width: 100, height: 40 },
120+
hittable: false,
121+
},
122+
{
123+
index: 4,
124+
depth: 1,
125+
parentIndex: 0,
126+
type: 'XCUIElementTypeCell',
127+
label: 'Account row',
128+
rect: { x: 10, y: 260, width: 370, height: 60 },
129+
hittable: true,
130+
},
131+
{
132+
index: 5,
133+
depth: 2,
134+
parentIndex: 4,
135+
type: 'XCUIElementTypeStaticText',
136+
label: 'Account',
137+
rect: { x: 24, y: 272, width: 80, height: 20 },
138+
hittable: false,
139+
},
140+
{
141+
index: 6,
142+
depth: 1,
143+
parentIndex: 0,
144+
type: 'XCUIElementTypeScrollView',
145+
rect: { x: 0, y: 340, width: 390, height: 300 },
146+
hittable: true,
147+
},
148+
{
149+
index: 7,
150+
depth: 2,
151+
parentIndex: 6,
152+
type: 'XCUIElementTypeOther',
153+
label: 'Feed item',
154+
rect: { x: 20, y: 360, width: 200, height: 40 },
155+
hittable: false,
156+
},
157+
{
158+
index: 8,
159+
depth: 1,
160+
parentIndex: 0,
161+
type: 'XCUIElementTypeStaticText',
162+
label: 'Under overlay',
163+
rect: { x: 20, y: 700, width: 100, height: 20 },
164+
hittable: false,
165+
interactionBlocked: 'covered',
166+
},
167+
{
168+
index: 9,
169+
depth: 0,
170+
type: 'XCUIElementTypeOther',
171+
label: 'Virtual item',
172+
hittable: false,
173+
},
174+
{
175+
index: 10,
176+
depth: 1,
177+
parentIndex: 0,
178+
type: 'XCUIElementTypeStaticText',
179+
label: 'Status',
180+
rect: { x: 20, y: 760, width: 60, height: 20 },
181+
hittable: false,
182+
},
183+
];

‎src/core/interaction-targeting.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
import { makeSnapshotState } from '../__tests__/test-utils/snapshot-builders.ts';
1010
import {
1111
classifyActionableTouchCandidates,
12+
createActionableTouchResolver,
1213
resolveActionableTouchResolution,
1314
} from './interaction-targeting.ts';
1415
import {
1516
ELEMENT14_DISTINCT_SUBTREE_NODES,
1617
EQUIVALENT_WRAPPER_CHAIN_NODES,
18+
INDEXED_PARITY_POLICY_NODES,
1719
} from './interaction-targeting.fixtures.ts';
1820

1921
test('collapses one same-label wrapper chain to its shared actionable node', () => {
@@ -201,3 +203,31 @@ test('falls back to the original node when no usable touch target exists', () =>
201203
assert.equal(resolution.reason, 'original');
202204
assert.equal(resolution.node.label, 'Virtual item');
203205
});
206+
207+
test('the batch resolver preserves every actionability policy branch', () => {
208+
const snapshot = makeSnapshotState(INDEXED_PARITY_POLICY_NODES);
209+
const resolveTouch = createActionableTouchResolver(snapshot.nodes);
210+
211+
const unindexed = snapshot.nodes.map((node) =>
212+
resolveActionableTouchResolution(snapshot.nodes, node),
213+
);
214+
const indexed = snapshot.nodes.map(resolveTouch);
215+
216+
assert.deepEqual(indexed, unindexed);
217+
assert.deepEqual(
218+
indexed.map((resolution) => [resolution.node.index, resolution.reason]),
219+
[
220+
[0, 'hittable-ancestor'],
221+
[2, 'same-rect-descendant'],
222+
[2, 'hittable-ancestor'],
223+
[3, 'semantic-target'],
224+
[4, 'semantic-target'],
225+
[4, 'hittable-ancestor'],
226+
[6, 'hittable-ancestor'],
227+
[7, 'overly-broad-ancestor'],
228+
[8, 'covered'],
229+
[9, 'original'],
230+
[10, 'overly-broad-ancestor'],
231+
],
232+
);
233+
});

0 commit comments

Comments
 (0)