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 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822andClaude Opus 5.5 authored and GitHub committed 2026-10-14 18:20:34 -07:00
1 parent 5e8f7c536e
commit 949228b606
1 file changed
+27 -13
@@ -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<string>;
}
const ipcInvocationKey = (location: unknown): string =>
String(JSON.stringify(location));
const indexIpcFinding = (
index: Map<string, ElectronIpcFinding[]>,
index: Map<string, IpcEndpointFacts>,
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<string, ElectronIpcFinding[]> | undefined;
const ipcAtEndpoint = (): Map<string, ElectronIpcFinding[]> => {
let ipcByEndpoint: Map<string, IpcEndpointFacts> | undefined;
const ipcAtEndpoint = (): Map<string, IpcEndpointFacts> => {
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) {