perf(electron/js): index IPC/package/file lookups and fuse summary passes (#13510)

perf(electron/js): index IPC/package/file lookups and fuse summary passes (#13507)
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 12:57:07 -05:00
1 parent 0eaea650c0
commit 5262268826
4 files changed
+185 -128

No files matched your search

@@ -338,96 +338,46 @@ export const summarizeElectronBoundaries = (
*/
coverageAnalysis: JavaScriptArtifactAnalysis = analysis,
): ElectronBoundarySummary => {
const files = new Map(analysis.files.map(({ file }) => [file.path, file]));
const javascript = analysis.files.flatMap(({ javascript: value }) =>
value === null ? [] : [value],
);
const windows = javascript.flatMap(({ electron }) =>
electron.browser_windows.filter(provenElectronFinding),
);
const bridges = javascript.flatMap(({ electron }) =>
electron.context_bridge_apis.filter(provenElectronFinding),
);
const validations = javascript.flatMap(({ electron }) =>
electron.sender_validations.filter(provenElectronFinding),
);
const utilities = analysis.files.flatMap(({ file, javascript: value }) =>
(value?.electron.utility_processes ?? [])
.filter(provenElectronFinding)
.map((finding) => ({
file,
finding,
})),
);
const nativeBindings = analysis.files.flatMap(({ file, javascript: value }) =>
(value?.electron.native_addon_bindings ?? [])
.filter(provenElectronFinding)
.map((finding) => ({
file,
finding,
})),
);
const ipc = ipcContext.records.filter(({ finding }) =>
provenElectronFinding(finding),
);
const pairing = ipcContext.states.filter(({ transmission }) =>
provenElectronFinding(transmission.finding),
);
const literalChannels = new Set(
ipc.flatMap(({ finding }) =>
finding.channel === null ? [] : [finding.channel],
),
);
const protocolCount = javascript.reduce(
(count, { electron }) =>
count + electron.protocols.filter(provenElectronFinding).length,
0,
);
// The coverage state is read from the retained analysis so that a rejected
// projection cannot hide an Electron reference the graph still reports.
const semantics = electronSemanticsState(coverageAnalysis);
return {
...(semantics === undefined ? {} : { electron_semantics: semantics }),
browser_windows: windows.length,
explicit_web_preferences: windows.reduce(
(count, window) => count + window.web_preferences.length,
0,
),
preload_entrypoints: windows.filter(
({ preload_path: path }) => path !== null,
).length,
context_bridge_apis: bridges.length,
exposed_api_members: bridges.reduce(
(count, bridge) => count + bridge.members.length,
0,
),
ipc: {
operations: ipc.length,
literal_channels: literalChannels.size,
dynamic_channel_operations: ipc.filter(
({ finding }) => finding.channel === null,
).length,
renderer_transmissions: ipc.filter(isRendererTransmission).length,
renderer_listeners: ipc.filter(
({ finding }) =>
finding.side === "renderer" && finding.mode === "listen",
).length,
main_handlers: ipc.filter(isMainHandler).length,
paired_renderer_transmissions: pairing.filter(
({ status }) => status === "paired",
).length,
ambiguous_renderer_transmissions: pairing.filter(
({ status }) => status === "ambiguous",
).length,
unpaired_literal_renderer_transmissions: pairing.filter(
({ status, transmission }) =>
status === "unpaired" && transmission.finding.channel !== null,
).length,
},
sender_validation_observations: validations.length,
utility_processes: utilities.length,
resolved_utility_entrypoints: utilities.filter(
({ file, finding }) =>
const files = new Map<string, JavaScriptArtifactFile>();
for (const { file } of analysis.files) files.set(file.path, file);
let browserWindows = 0;
let explicitWebPreferences = 0;
let preloadEntrypoints = 0;
let contextBridgeApis = 0;
let exposedApiMembers = 0;
let senderValidationObservations = 0;
let protocolCount = 0;
let utilityProcesses = 0;
let resolvedUtilityEntrypoints = 0;
let nativeAddonBindings = 0;
let resolvedNativeAddonBindings = 0;
let v8CachedDataCompilations = 0;
for (const { file, javascript: value } of analysis.files) {
if (value === null) continue;
const { electron } = value;
for (const window of electron.browser_windows) {
if (!provenElectronFinding(window)) continue;
browserWindows += 1;
explicitWebPreferences += window.web_preferences.length;
if (window.preload_path !== null) preloadEntrypoints += 1;
}
for (const bridge of electron.context_bridge_apis) {
if (!provenElectronFinding(bridge)) continue;
contextBridgeApis += 1;
exposedApiMembers += bridge.members.length;
}
for (const validation of electron.sender_validations) {
if (provenElectronFinding(validation)) senderValidationObservations += 1;
}
for (const protocol of electron.protocols) {
if (provenElectronFinding(protocol)) protocolCount += 1;
}
for (const finding of electron.utility_processes) {
if (!provenElectronFinding(finding)) continue;
utilityProcesses += 1;
if (
finding.module_path !== null &&
resolvesToFile({
file,
@@ -437,29 +387,93 @@ export const summarizeElectronBoundaries = (
jsonDocuments: analysis.jsonDocuments,
directoryPaths,
runtimePathKind: "node-entrypoint",
}),
).length,
native_addon_bindings: nativeBindings.length,
v8_cached_data_compilations: javascript.reduce(
(count, { electron }) =>
count +
(electron.dynamic_code ?? []).filter(
({ cached_data_status }) => cached_data_status === "declared",
).length,
0,
),
resolved_native_addon_bindings: nativeBindings.filter(({ file, finding }) =>
resolvesToFile({
file,
specifier: finding.specifier,
context: "module-specifier",
files,
jsonDocuments: analysis.jsonDocuments,
directoryPaths,
kind: "native-addon",
moduleKind: finding.module_kind,
}),
).length,
})
)
resolvedUtilityEntrypoints += 1;
}
for (const finding of electron.native_addon_bindings) {
if (!provenElectronFinding(finding)) continue;
nativeAddonBindings += 1;
if (
resolvesToFile({
file,
specifier: finding.specifier,
context: "module-specifier",
files,
jsonDocuments: analysis.jsonDocuments,
directoryPaths,
kind: "native-addon",
moduleKind: finding.module_kind,
})
)
resolvedNativeAddonBindings += 1;
}
for (const dynamic of electron.dynamic_code ?? []) {
if (dynamic.cached_data_status === "declared")
v8CachedDataCompilations += 1;
}
}
let ipcOperations = 0;
let dynamicChannelOperations = 0;
let rendererTransmissions = 0;
let rendererListeners = 0;
let mainHandlers = 0;
const literalChannels = new Set<string>();
for (const record of ipcContext.records) {
const { finding } = record;
if (!provenElectronFinding(finding)) continue;
ipcOperations += 1;
if (finding.channel === null) dynamicChannelOperations += 1;
else literalChannels.add(finding.channel);
if (isRendererTransmission(record)) rendererTransmissions += 1;
if (finding.side === "renderer" && finding.mode === "listen")
rendererListeners += 1;
if (isMainHandler(record)) mainHandlers += 1;
}
let pairedRendererTransmissions = 0;
let ambiguousRendererTransmissions = 0;
let unpairedLiteralRendererTransmissions = 0;
for (const state of ipcContext.states) {
if (!provenElectronFinding(state.transmission.finding)) continue;
if (state.status === "paired") pairedRendererTransmissions += 1;
else if (state.status === "ambiguous") ambiguousRendererTransmissions += 1;
else if (
state.status === "unpaired" &&
state.transmission.finding.channel !== null
)
unpairedLiteralRendererTransmissions += 1;
}
// The coverage state is read from the retained analysis so that a rejected
// projection cannot hide an Electron reference the graph still reports.
const semantics = electronSemanticsState(coverageAnalysis);
return {
...(semantics === undefined ? {} : { electron_semantics: semantics }),
browser_windows: browserWindows,
explicit_web_preferences: explicitWebPreferences,
preload_entrypoints: preloadEntrypoints,
context_bridge_apis: contextBridgeApis,
exposed_api_members: exposedApiMembers,
ipc: {
operations: ipcOperations,
literal_channels: literalChannels.size,
dynamic_channel_operations: dynamicChannelOperations,
renderer_transmissions: rendererTransmissions,
renderer_listeners: rendererListeners,
main_handlers: mainHandlers,
paired_renderer_transmissions: pairedRendererTransmissions,
ambiguous_renderer_transmissions: ambiguousRendererTransmissions,
unpaired_literal_renderer_transmissions:
unpairedLiteralRendererTransmissions,
},
sender_validation_observations: senderValidationObservations,
utility_processes: utilityProcesses,
resolved_utility_entrypoints: resolvedUtilityEntrypoints,
native_addon_bindings: nativeAddonBindings,
v8_cached_data_compilations: v8CachedDataCompilations,
resolved_native_addon_bindings: resolvedNativeAddonBindings,
protocol_registrations: protocolCount,
};
};
@@ -373,12 +373,23 @@ const addLiteralPairings = (state: IpcGraphState): void => {
};
const addValidationObservations = (state: IpcGraphState): void => {
const handlersByPath = new Map<string, ElectronIpcRecord[]>();
for (const record of state.records) {
if (record.finding.handler_location === null) continue;
const list = handlersByPath.get(record.file.path);
if (list === undefined) handlersByPath.set(record.file.path, [record]);
else list.push(record);
}
for (const analyzed of state.context.analysis.files) {
const { file, javascript } = analyzed;
if (javascript === null) continue;
const coverage = javascriptAnalysisCoverage(javascript);
const fileHandlers = handlersByPath.get(file.path);
for (const validation of javascript.electron.sender_validations) {
const handlerRecord = containingHandler(state.records, file, validation);
const handlerRecord =
fileHandlers === undefined
? undefined
: containingHandler(fileHandlers, validation);
const target =
handlerRecord === undefined
? electronFindingSourceNode(
@@ -438,14 +449,12 @@ const addValidationObservation = (input: ValidationObservationInput): void => {
const containingHandler = (
records: readonly ElectronIpcRecord[],
file: JavaScriptArtifactFile,
validation: ElectronSenderValidationFinding,
): ElectronIpcRecord | undefined => {
let selected: ElectronIpcRecord | undefined;
for (const record of records) {
const location = record.finding.handler_location;
if (
record.file.path !== file.path ||
location === null ||
!javascriptSourceRangeContains(location, validation.location)
)
@@ -468,9 +477,13 @@ const coverageFor = (
context: JavaScriptArtifactGraphContext,
file: JavaScriptArtifactFile,
): JavaScriptArtifactGraphCoverage | undefined => {
const javascript = context.analysis.files.find(
({ file: candidate }) => candidate.path === file.path,
)?.javascript;
const analyzed =
context.analyzedFilesByPath !== undefined
? context.analyzedFilesByPath.get(file.path)
: context.analysis.files.find(
({ file: candidate }) => candidate.path === file.path,
);
const javascript = analyzed?.javascript;
return javascript === null || javascript === undefined
? undefined
: javascriptAnalysisCoverage(javascript);
@@ -171,6 +171,23 @@ function* buildJavaScriptArtifactGraphInputSteps(
const unavailableFilesByPath = new Map(
fileSet.unavailable_relevant_files.map((file) => [file.path, file]),
);
const packagesByPath = new Map<
string,
JavaScriptArtifactAnalysis["packages"][number]
>();
for (const pkg of analysis.packages)
if (!packagesByPath.has(pkg.path)) packagesByPath.set(pkg.path, pkg);
const indexAnalyzedFiles = (
files: JavaScriptArtifactAnalysis["files"],
): ReadonlyMap<string, JavaScriptArtifactAnalysis["files"][number]> => {
const byPath = new Map<
string,
JavaScriptArtifactAnalysis["files"][number]
>();
for (const entry of files)
if (!byPath.has(entry.file.path)) byPath.set(entry.file.path, entry);
return byPath;
};
const context: JavaScriptArtifactGraphContext = {
accumulator,
snapshot,
@@ -183,6 +200,8 @@ function* buildJavaScriptArtifactGraphInputSteps(
),
root,
filesByPath: new Map(fileSet.files.map((file) => [file.path, file])),
analyzedFilesByPath: indexAnalyzedFiles(analysis.files),
packagesByPath,
unanalyzedFiles: {
coverage: snapshot.coverage,
get: (path) =>
@@ -210,15 +229,17 @@ function* buildJavaScriptArtifactGraphInputSteps(
yield;
// Raw observations remain on the file nodes. Only admitted expansions feed
// the relationship builders, including their Electron and bundler callers.
const projectionContext = {
const projectedFiles = analysis.files.map((file) =>
file.application_projection_failure === undefined
? file
: { ...file, javascript: null, semantic: null },
);
const projectionContext: JavaScriptArtifactGraphContext = {
...context,
analyzedFilesByPath: indexAnalyzedFiles(projectedFiles),
analysis: {
...analysis,
files: analysis.files.map((file) =>
file.application_projection_failure === undefined
? file
: { ...file, javascript: null, semantic: null },
),
files: projectedFiles,
},
};
const packageRoots = addJavaScriptPackageNodes(projectionContext);
@@ -45,6 +45,14 @@ export interface JavaScriptArtifactGraphContext {
readonly directoryPaths: ReadonlySet<string>;
readonly root: ApplicationNode;
readonly filesByPath: ReadonlyMap<string, JavaScriptArtifactFile>;
readonly analyzedFilesByPath?: ReadonlyMap<
string,
JavaScriptArtifactAnalysis["files"][number]
>;
readonly packagesByPath?: ReadonlyMap<
string,
JavaScriptArtifactAnalysis["packages"][number]
>;
/** Borrowed inventory lookup for present files outside the source projection. */
readonly unanalyzedFiles: JavaScriptUnanalyzedArtifactLookup | undefined;
readonly fileNodes: Map<string, ApplicationNode>;
@@ -143,9 +151,10 @@ export const applicationRootPath = (
const file =
context.filesByPath.get(path) ?? context.unanalyzedFiles?.get(path);
if (file?.container_sha256 !== source.container_sha256) continue;
const metadata = context.analysis.packages.find(
(value) => value.path === path,
);
const metadata =
context.packagesByPath !== undefined
? context.packagesByPath.get(path)
: context.analysis.packages.find((value) => value.path === path);
if (metadata?.status !== "included") return null;
const defaultEntry = context.filesByPath.get(`${prefix}index.js`);
if (