From afad95fc911f824599a872876ebb646bf772f2ec Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:42:16 -0500 Subject: [PATCH] perf(artifacts/contracts): replace O(N^2) scans with hash joins/sweep-line and hoist expandToolAvailability group maps (#13585) * perf(artifacts/contracts): replace O(N^2) scans with hash joins/sweep-line and hoist expandToolAvailability group maps * perf(dotnet): retain bounded bundle configuration scans --- src/artifacts/EspFlashRegionReads.ts | 30 +++++++++++----- src/browser/CdpCaptureDocuments.ts | 7 ++-- src/contracts/toolOutputSchemaPrimitives.ts | 38 +++++++++++---------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/artifacts/EspFlashRegionReads.ts b/src/artifacts/EspFlashRegionReads.ts index 9bdd81ad31..42dd185c6d 100644 --- a/src/artifacts/EspFlashRegionReads.ts +++ b/src/artifacts/EspFlashRegionReads.ts @@ -86,16 +86,28 @@ interface WrittenSegment { /** The segments region boundaries cut, kept where some region covers them. */ const coveredSegments = (ranges: readonly FileRange[]): WrittenSegment[] => { - const cuts = [ - ...new Set(ranges.flatMap(({ start, length }) => [start, start + length])), - ].sort((left, right) => left - right); - return cuts.slice(1).flatMap((end, index) => { + const deltas = new Map(); + for (const { start, length } of ranges) { + if (length <= 0) continue; + deltas.set(start, (deltas.get(start) ?? 0) + 1); + deltas.set(start + length, (deltas.get(start + length) ?? 0) - 1); + } + const cuts = [...deltas.keys()].sort((left, right) => left - right); + const segments: WrittenSegment[] = []; + let active = 0; + for (let index = 0; index + 1 < cuts.length; index += 1) { const start = cuts[index]!; - const covered = ranges.some( - (range) => range.start <= start && end <= range.start + range.length, - ); - return covered ? [{ start, end, first: -1, last: -1, bytes: 0 }] : []; - }); + active += deltas.get(start) ?? 0; + if (active > 0) + segments.push({ + start, + end: cuts[index + 1]!, + first: -1, + last: -1, + bytes: 0, + }); + } + return segments; }; const tally = ( diff --git a/src/browser/CdpCaptureDocuments.ts b/src/browser/CdpCaptureDocuments.ts index 0317b71d5b..00dc7459cf 100644 --- a/src/browser/CdpCaptureDocuments.ts +++ b/src/browser/CdpCaptureDocuments.ts @@ -284,7 +284,8 @@ export function* captureDom( const { nodeType, nodeName, nodeValue, parent, attributes } = admitted.value; const nodeIndex = baseIndex + index; - const attributeNames = [...new Set(attributes.map(([name]) => name))]; + const attributeSet = new Set(attributes.map(([name]) => name)); + const attributeNames = [...attributeSet]; const domElement = domXmlElement(ancestry, index); const htmlTagName = domHtmlTagName(nodeName, domElement); if (domElement.xmlDocument === undefined) @@ -293,8 +294,8 @@ export function* captureDom( if ( domElement.xmlDocument === false && nodeType === 1 && - attributeNames.includes("xlink:href") && - !attributeNames.includes("href") + attributeSet.has("xlink:href") && + !attributeSet.has("href") ) { const backendNodeId = backendNodeIds[index]; svgQuery = diff --git a/src/contracts/toolOutputSchemaPrimitives.ts b/src/contracts/toolOutputSchemaPrimitives.ts index a0e2e8f2fa..b2404fe743 100644 --- a/src/contracts/toolOutputSchemaPrimitives.ts +++ b/src/contracts/toolOutputSchemaPrimitives.ts @@ -604,28 +604,30 @@ export function expandToolAvailability( group.client_requirements ?? inventory.client_requirements, })), ); - return inventory.groups.flatMap((group) => - group.names.map((name) => + return inventory.groups.flatMap((group) => { + const effectSet = new Set(group.effects); + const annotationSet = new Set(group.annotations); + const effects = Object.fromEntries( + TOOL_EFFECT_NAMES.map((effect) => [effect, effectSet.has(effect)]), + ); + const annotations = Object.fromEntries( + TOOL_ANNOTATION_NAMES.map((annotation) => [ + annotation, + annotationSet.has(annotation), + ]), + ); + const client_requirements = + group.client_requirements ?? inventory.client_requirements; + return group.names.map((name) => toolAvailability.parse({ ...group, name, - client_requirements: - group.client_requirements ?? inventory.client_requirements, - effects: Object.fromEntries( - TOOL_EFFECT_NAMES.map((effect) => [ - effect, - group.effects.includes(effect), - ]), - ), - annotations: Object.fromEntries( - TOOL_ANNOTATION_NAMES.map((annotation) => [ - annotation, - group.annotations.includes(annotation), - ]), - ), + client_requirements, + effects, + annotations, }), - ), - ); + ); + }); } /** Session status facts shared by every detail level and session kind. */