WeKnora/patches/browserskill/03-vom-render-performance.p...

103 lines
4.3 KiB
Diff

diff --git a/packages/vom/src/__tests__/render.test.ts b/packages/vom/src/__tests__/render.test.ts
index 0223a80..9554cf6 100644
--- a/packages/vom/src/__tests__/render.test.ts
+++ b/packages/vom/src/__tests__/render.test.ts
@@ -52,6 +52,31 @@ describe("renderVom single-layer page", () => {
expect(out.text).toContain('@e1 checkbox "Subscribe"' + marker + ' ="on"');
expect(out.refs).toHaveLength(1);
});
+ it("does not rescan every earlier row for repeated action context", () => {
+ const count = 600;
+ let nameReads = 0;
+ const nodes = [node({ id: 1, role: "RootWebArea" })];
+ for (let i = 0; i < count; i++) {
+ const label = node({ id: 2 + i * 2, parentId: 1, role: "StaticText" });
+ Object.defineProperty(label, "name", {
+ enumerable: true,
+ get() {
+ nameReads++;
+ return `Record ${i}`;
+ },
+ });
+ nodes.push(
+ label,
+ node({ id: 3 + i * 2, parentId: 1, tag: "button", role: "button", name: "Open" }),
+ );
+ }
+ const out = renderVom(scene(nodes));
+ expect(out.refs).toHaveLength(count);
+ expect(out.refs[0].ctx).toBe("Record 0");
+ expect(out.refs[count - 1].ctx).toBe(`Record ${count - 3}`);
+ // Count semantic reads rather than asserting a machine-dependent duration.
+ expect(nameReads).toBeLessThan(count * 100);
+ });
it("does not derive handle context across frame scopes", () => {
const out = renderVom(
diff --git a/packages/vom/src/render.ts b/packages/vom/src/render.ts
index 77c10c6..c66e65d 100644
--- a/packages/vom/src/render.ts
+++ b/packages/vom/src/render.ts
@@ -614,11 +614,32 @@ function collectSameContainerContext(
let guard = 0;
while (parentId !== null && guard <= state.parentMap.size) {
const siblings = state.children.get(parentId) ?? [];
- for (const sibling of siblings) {
- if (sibling.id === childId) break;
- collectWeakLabelsFromSubtree(sibling, node, nodeName, state, labels);
- while (labels.length > MAX_HANDLE_CONTEXT_ITEMS) labels.shift();
+ const end = state.siblingIndex.get(childId) ?? siblings.length;
+ let cache = state.siblingContextCache.get(parentId);
+ if (!cache) {
+ cache = new Map();
+ state.siblingContextCache.set(parentId, cache);
}
+ // The subtree collector depends on scope, target name and incoming labels.
+ // Include all three so descendants arriving from different containers retain
+ // the original context semantics. DFS visits sibling prefixes in order.
+ const key = JSON.stringify([node.contextScopeId, nodeName, labels]);
+ let prefix = cache.get(key);
+ if (!prefix || prefix.nextIndex > end) prefix = { nextIndex: 0, labels: [...labels] };
+ for (; prefix.nextIndex < end; prefix.nextIndex++) {
+ collectWeakLabelsFromSubtree(
+ siblings[prefix.nextIndex],
+ node,
+ nodeName,
+ state,
+ prefix.labels,
+ );
+ while (prefix.labels.length > MAX_HANDLE_CONTEXT_ITEMS) prefix.labels.shift();
+ }
+ labels.splice(0, labels.length, ...prefix.labels);
+ // Bound retained variants on pages with many distinct action names.
+ if (!cache.has(key) && cache.size >= 64) cache.delete(cache.keys().next().value!);
+ cache.set(key, prefix);
const parent = state.nodesById.get(parentId);
if (!parent || !sharesContextScope(node, parent) || isContextBoundary(parent)) break;
@@ -791,6 +812,8 @@ interface RenderState {
redactValues: boolean;
truncated: boolean;
children: Map<number | null, VomNode[]>;
+ siblingIndex: Map<number, number>;
+ siblingContextCache: Map<number, Map<string, { nextIndex: number; labels: string[] }>>;
parentMap: Map<number, number | null>;
nodesById: Map<number, VomNode>;
domContextIndex: DomContextIndex;
@@ -907,8 +930,15 @@ function createRenderState(
activeScopeBlocks: ActiveScopeBlock[],
): RenderState {
const children = buildChildren(nodes);
+ const siblingIndex = new Map<number, number>();
+ for (const siblings of children.values()) {
+ for (let index = 0; index < siblings.length; index++)
+ siblingIndex.set(siblings[index].id, index);
+ }
const state: RenderState = {
visualAncestors: new Set(),
+ siblingIndex,
+ siblingContextCache: new Map(),
lines: [...initialLines],
refs: [],
nextRef: 1,