From 949228b6069d96766dba8545284ecbafd3cbe93f Mon Sep 17 00:00:00 2001 From: Kaoru0822 Date: Wed, 14 Oct 2026 18:20:34 -0700 Subject: [PATCH] perf(javascript): check wrapper IPC duplicates by invocation set (#3783) The endpoint index from #3779 still scanned every earlier finding at the same endpoint and serialized each invocation location. One wrapper called N times for one channel shares an endpoint, so duplicate checks stayed quadratic in N. Record per endpoint whether a directly observed send/invoke exists and the set of serialized invocation locations. The duplicate rule is unchanged and each check is constant time. Refs #3670 Co-authored-by: Claude Opus 5.5 --- .../javascript/electronWrappedCallPaths.ts | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/domain/javascript/electronWrappedCallPaths.ts b/src/domain/javascript/electronWrappedCallPaths.ts index 38ad923f0d..e41fafe78d 100644 --- a/src/domain/javascript/electronWrappedCallPaths.ts +++ b/src/domain/javascript/electronWrappedCallPaths.ts @@ -46,14 +46,30 @@ const callLabel = (node: t.Node): string => const ipcEndpointKey = (value: ElectronIpcFinding): string => JSON.stringify([value.side, value.operation, value.channel, value.location]); +/** Duplicate evidence already recorded for one endpoint and call site. */ +interface IpcEndpointFacts { + // A directly observed transmission stays one fact however it is reached. + observedTransmission: boolean; + readonly invocations: Set; +} + +const ipcInvocationKey = (location: unknown): string => + String(JSON.stringify(location)); + const indexIpcFinding = ( - index: Map, + index: Map, value: ElectronIpcFinding, ): void => { const key = ipcEndpointKey(value); - const bucket = index.get(key); - if (bucket === undefined) index.set(key, [value]); - else bucket.push(value); + let facts = index.get(key); + if (facts === undefined) { + facts = { observedTransmission: false, invocations: new Set() }; + index.set(key, facts); + } + facts.observedTransmission ||= + value.resolution_basis === undefined && + (value.mode === "send" || value.mode === "invoke"); + facts.invocations.add(ipcInvocationKey(value.invocation_location)); }; interface PathContext { @@ -164,8 +180,8 @@ export const collectElectronWrappedCallPaths = ( }; // Duplicate checks consult only facts at the same endpoint and call site. // Scanning every prior fact made bundles with many registrations quadratic. - let ipcByEndpoint: Map | undefined; - const ipcAtEndpoint = (): Map => { + let ipcByEndpoint: Map | undefined; + const ipcAtEndpoint = (): Map => { if (ipcByEndpoint !== undefined) return ipcByEndpoint; ipcByEndpoint = new Map(); for (const { value } of accumulator.ipc) @@ -374,13 +390,11 @@ export const collectElectronWrappedCallPaths = ( }; // A fully observed transmission stays one source fact when its callback is // traced. Registrations retain invocation identity because handlers compete. - const invocationKey = JSON.stringify(invocationLocation); - const duplicate = (ipcAtEndpoint().get(ipcEndpointKey(finding)) ?? []).some( - (value) => - (value.resolution_basis === undefined && - (value.mode === "send" || value.mode === "invoke")) || - JSON.stringify(value.invocation_location) === invocationKey, - ); + const recorded = ipcAtEndpoint().get(ipcEndpointKey(finding)); + const duplicate = + recorded !== undefined && + (recorded.observedTransmission || + recorded.invocations.has(ipcInvocationKey(invocationLocation))); const findingGuarded = context.conditions.length > 0 || hasConditions(first) || guardedDispatch; if (!duplicate && !findingGuarded) {