perf(javascript): index feature, semantic, IPC, and source map lookups (#13319)

perf(javascript): eliminate double preload AST pass and index feature/semantic/IPC/sourcemap lookups
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 03:43:13 -05:00
1 parent 79887f7a3b
commit fde2c87b86
17 files changed
+1860 -535

No files matched your search

+353 -178
View File
@@ -1,6 +1,8 @@
import { isJavaScriptModuleUnit } from "./javascriptModuleUnits.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import type { JavaScriptApplicationGraph } from "./javascriptApplicationGraph.js";
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
import { getFeatureGraphIndex } from "./javascriptFeatureGraphIndex.js";
import {
featureSeedMatchMode,
type ApplicationFeatureSeed,
@@ -15,69 +17,178 @@ export interface ApplicationFeatureSeedMatch {
readonly field: string;
}
interface CandidateField {
readonly basis: ApplicationFeatureSeedMatch["basis"];
readonly field: string;
/** Structural field path, retaining literal keys but omitting array indexes. */
readonly selectionPath?: string;
readonly value: string;
interface SuffixPattern {
readonly name: string;
readonly dotName: string;
}
type TypedSeedKind = "api" | "channel" | "module" | "native-export";
const suffixPatterns = (names: readonly string[]): readonly SuffixPattern[] =>
names.map((name) => ({ name, dotName: `.${name}` }));
const SCALAR_PATTERNS: Readonly<
Record<TypedSeedKind, readonly SuffixPattern[]>
> = {
api: suffixPatterns([
"label",
"api_name",
"api_key",
"key",
"methods",
"members",
]),
channel: suffixPatterns(["label", "channel", "key"]),
module: suffixPatterns([
"label",
"module_key",
"path",
"source",
"original_source",
"source_root",
"exports",
]),
"native-export": suffixPatterns([
"label",
"key",
"requested_members",
"members",
]),
};
const MEMBER_PATTERNS: Readonly<
Record<TypedSeedKind, readonly SuffixPattern[]>
> = {
api: suffixPatterns(["methods", "members"]),
channel: [],
module: suffixPatterns(["exports"]),
"native-export": suffixPatterns(["requested_members", "members"]),
};
const keyMatchesAny = (
key: string,
patterns: readonly SuffixPattern[],
): boolean => {
for (const { name, dotName } of patterns)
if (key === name || key.endsWith(dotName)) return true;
return false;
};
/** Locate every graph node matching a typed literal seed without regex execution. */
export const findApplicationFeatureSeeds = (
nodes: readonly ApplicationNode[],
seed: ApplicationFeatureSeed,
): ApplicationFeatureSeedMatch[] =>
nodes
.flatMap((node) => seedMatch(node, seed))
.sort((left, right) =>
compareUnicodeCodePoints(
`${left.node_id}\0${left.basis}\0${left.field}`,
`${right.node_id}\0${right.basis}\0${right.field}`,
),
);
): ApplicationFeatureSeedMatch[] => collectSeedMatches([nodes], seed);
const seedMatch = (
node: ApplicationNode,
/** Locate matching seeds using immutable graph indexes when available. */
export const findApplicationFeatureSeedsInGraph = (
graph: JavaScriptApplicationGraph,
seed: ApplicationFeatureSeed,
): ApplicationFeatureSeedMatch[] => {
const index = getFeatureGraphIndex(graph);
if (index === undefined)
return findApplicationFeatureSeeds(graph.nodes, seed);
const mode = featureSeedMatchMode(seed);
if (seed.kind === "node-id" && mode === "exact") {
const query = normalizeFeatureSeedText(seed.value, seed.case_sensitive);
const node = index.nodesById.get(query);
return node === undefined
? []
: [
{
node_id: node.node_id,
kind: node.kind,
basis: "node-id",
field: "node_id",
},
];
}
return collectSeedMatches(
candidateNodeBuckets(graph, index, seed.kind),
seed,
);
};
const candidateNodeBuckets = (
graph: JavaScriptApplicationGraph,
index: NonNullable<ReturnType<typeof getFeatureGraphIndex>>,
kind: ApplicationFeatureSeed["kind"],
): readonly (readonly ApplicationNode[])[] => {
if (kind === "string" || kind === "node-id") return [graph.nodes];
if (kind === "route") return [index.nodesByKind.get("endpoint") ?? []];
if (kind === "api")
return [
index.nodesByKind.get("context-bridge-api") ?? [],
index.nodesByKind.get("javascript-expression") ?? [],
];
if (kind === "channel") return [index.nodesByKind.get("ipc-channel") ?? []];
if (kind === "module")
return [
index.nodesByKind.get("javascript-asset") ?? [],
index.nodesByKind.get("javascript-module") ?? [],
index.nodesByKind.get("source-module") ?? [],
];
return [index.nodesByKind.get("native-export") ?? []];
};
const collectSeedMatches = (
buckets: readonly (readonly ApplicationNode[])[],
seed: ApplicationFeatureSeed,
): ApplicationFeatureSeedMatch[] => {
const mode = featureSeedMatchMode(seed);
const caseSensitive = seed.case_sensitive;
const query = normalizeFeatureSeedText(seed.value, caseSensitive);
if (!kindMatches(node, seed.kind)) return [];
const requestedEntryPath =
seed.kind === "api" && "entry_path" in seed ? seed.entry_path : undefined;
if (
seed.kind === "api" &&
requestedEntryPath !== undefined &&
!node.observations.some(({ properties }) => {
const entryPath = properties.entry_path;
return (
Array.isArray(entryPath) &&
entryPath.length === requestedEntryPath.length &&
entryPath.every(
(segment, index) => segment === requestedEntryPath[index],
)
const matches: ApplicationFeatureSeedMatch[] = [];
for (const nodes of buckets) {
for (const node of nodes) {
if (!kindMatches(node, seed.kind)) continue;
if (
seed.kind === "api" &&
requestedEntryPath !== undefined &&
!hasMatchingEntryPath(node, requestedEntryPath)
)
continue;
const match = firstMatchingCandidate(
node,
seed.kind,
query,
mode,
caseSensitive,
);
})
)
return [];
const match = candidateFields(node, seed.kind).find((candidate) => {
const value = normalizeFeatureSeedText(candidate.value, caseSensitive);
return mode === "exact" ? value === query : value.includes(query);
});
return match === undefined
? []
: [
{
if (match !== undefined)
matches.push({
node_id: node.node_id,
kind: node.kind,
basis: match.basis,
field: match.field,
},
];
});
}
}
if (matches.length > 1)
matches.sort((left, right) =>
compareUnicodeCodePoints(
`${left.node_id}\0${left.basis}\0${left.field}`,
`${right.node_id}\0${right.basis}\0${right.field}`,
),
);
return matches;
};
const hasMatchingEntryPath = (
node: ApplicationNode,
requestedEntryPath: readonly string[],
): boolean =>
node.observations.some(({ properties }) => {
const entryPath = properties.entry_path;
return (
Array.isArray(entryPath) &&
entryPath.length === requestedEntryPath.length &&
entryPath.every((segment, index) => segment === requestedEntryPath[index])
);
});
const kindMatches = (
node: ApplicationNode,
kind: ApplicationFeatureSeed["kind"],
@@ -101,173 +212,229 @@ const kindMatches = (
return node.kind === "native-export";
};
const candidateFields = (
const textMatches = (
candidate: string,
query: string,
mode: "exact" | "contains",
caseSensitive: boolean,
): boolean => {
const normalized = normalizeFeatureSeedText(candidate, caseSensitive);
return mode === "exact" ? normalized === query : normalized.includes(query);
};
interface MatchedField {
readonly basis: ApplicationFeatureSeedMatch["basis"];
readonly field: string;
}
const firstMatchingCandidate = (
node: ApplicationNode,
seedKind: ApplicationFeatureSeed["kind"],
): CandidateField[] => {
query: string,
mode: "exact" | "contains",
caseSensitive: boolean,
): MatchedField | undefined => {
if (seedKind === "node-id")
return [{ basis: "node-id", field: "node_id", value: node.node_id }];
return textMatches(node.node_id, query, mode, caseSensitive)
? { basis: "node-id", field: "node_id" }
: undefined;
if (seedKind === "route")
return node.observations.flatMap((observation, index) =>
(["value", "path"] as const).flatMap((key) => {
const value = observation.properties[key];
return typeof value === "string"
? [
{
basis: "property" as const,
field: `observations[${String(index)}].properties.${key}`,
value,
},
]
: [];
}),
);
const fields: CandidateField[] = [
...identityFields(node),
...node.observations.flatMap((observation, index) => [
...(observation.label === null
? []
: [
{
basis: "label" as const,
field: `observations[${String(index)}].label`,
value: observation.label,
},
]),
...(observation.source_map_reference !== null &&
observation.source_map_reference.resolution.kind !== "unresolved"
? [
{
basis: "property" as const,
field: `observations[${String(index)}].source_map_reference.resolution.path`,
value: observation.source_map_reference.resolution.path,
},
]
: []),
...propertyFields(
observation.properties,
`observations[${String(index)}].properties`,
),
]),
];
if (seedKind === "string") return fields;
const names = relevantFields[seedKind];
return fields.filter(({ field, selectionPath = field }) =>
names.some((name) => {
const path = memberFieldSuffixes.has(name) ? selectionPath : field;
return path === "identity" || path.endsWith(name);
}),
return firstMatchingRouteField(node, query, mode, caseSensitive);
const identityMatch = firstMatchingIdentityField(
node,
seedKind,
query,
mode,
caseSensitive,
);
if (identityMatch !== undefined) return identityMatch;
for (let index = 0; index < node.observations.length; index += 1) {
const observation = node.observations[index]!;
if (
observation.label !== null &&
textMatches(observation.label, query, mode, caseSensitive)
)
return { basis: "label", field: `observations[${String(index)}].label` };
if (
(seedKind === "string" || seedKind === "module") &&
observation.source_map_reference !== null &&
observation.source_map_reference.resolution.kind !== "unresolved" &&
textMatches(
observation.source_map_reference.resolution.path,
query,
mode,
caseSensitive,
)
)
return {
basis: "property",
field: `observations[${String(index)}].source_map_reference.resolution.path`,
};
const propertyMatch = firstMatchingPropertyField(
observation.properties,
`observations[${String(index)}].properties`,
seedKind,
query,
mode,
caseSensitive,
);
if (propertyMatch !== undefined)
return { basis: "property", field: propertyMatch };
}
return undefined;
};
const memberFieldSuffixes = new Set([
".members",
".methods",
".exports",
".requested_members",
]);
const firstMatchingRouteField = (
node: ApplicationNode,
query: string,
mode: "exact" | "contains",
caseSensitive: boolean,
): MatchedField | undefined => {
for (let index = 0; index < node.observations.length; index += 1) {
const { properties } = node.observations[index]!;
for (const key of ["value", "path"] as const) {
const value = properties[key];
if (
typeof value === "string" &&
textMatches(value, query, mode, caseSensitive)
)
return {
basis: "property",
field: `observations[${String(index)}].properties.${key}`,
};
}
}
return undefined;
};
const relevantFields = {
route: [".label", ".value", ".path"],
api: [".label", ".api_name", ".api_key", ".key", ".methods", ".members"],
channel: [".label", ".channel", ".key"],
module: [
".label",
".module_key",
".path",
".source",
".original_source",
".source_root",
".exports",
],
"native-export": [".label", ".key", ".requested_members", ".members"],
} as const;
const identityFields = (node: ApplicationNode): CandidateField[] => {
const identity = node.identity;
const firstMatchingIdentityField = (
node: ApplicationNode,
seedKind: "string" | TypedSeedKind,
query: string,
mode: "exact" | "contains",
caseSensitive: boolean,
): MatchedField | undefined => {
const { identity } = node;
if (identity.strategy === "content-digest")
return [{ basis: "identity", field: "identity", value: identity.sha256 }];
if (identity.strategy === "source-map-original")
return [
{
basis: "identity",
field: "identity.original_source",
value: identity.original_source,
},
...(identity.source_root === null
? []
: [
{
basis: "identity" as const,
field: "identity.source_root",
value: identity.source_root,
},
]),
];
return textMatches(identity.sha256, query, mode, caseSensitive)
? { basis: "identity", field: "identity" }
: undefined;
if (identity.strategy === "source-map-original") {
if (seedKind !== "string" && seedKind !== "module") return undefined;
if (textMatches(identity.original_source, query, mode, caseSensitive))
return { basis: "identity", field: "identity.original_source" };
if (
identity.source_root !== null &&
textMatches(identity.source_root, query, mode, caseSensitive)
)
return { basis: "identity", field: "identity.source_root" };
return undefined;
}
if (identity.strategy === "canonical-path")
return [
{ basis: "identity", field: "identity.path", value: identity.path },
];
return (seedKind === "string" || seedKind === "module") &&
textMatches(identity.path, query, mode, caseSensitive)
? { basis: "identity", field: "identity.path" }
: undefined;
if (identity.strategy === "artifact-local-key")
return [{ basis: "identity", field: "identity.key", value: identity.key }];
return seedKind !== "module" &&
textMatches(identity.key, query, mode, caseSensitive)
? { basis: "identity", field: "identity.key" }
: undefined;
if (identity.strategy === "runtime-instance")
return [
{
basis: "identity",
field: "identity.runtime_key",
value: identity.runtime_key,
},
];
return [];
return seedKind === "string" &&
textMatches(identity.runtime_key, query, mode, caseSensitive)
? { basis: "identity", field: "identity.runtime_key" }
: undefined;
return undefined;
};
const propertyFields = (
interface PropertyFrame {
readonly current: JsonValue;
readonly path: string;
readonly inMemberArray: boolean;
}
const firstMatchingPropertyField = (
value: Readonly<Record<string, JsonValue>>,
prefix: string,
): CandidateField[] => {
const output: CandidateField[] = [];
const pending: {
readonly current: JsonValue;
readonly path: string;
readonly selectionPath: string;
}[] = [{ current: value, path: prefix, selectionPath: prefix }];
seedKind: "string" | TypedSeedKind,
query: string,
mode: "exact" | "contains",
caseSensitive: boolean,
): string | undefined => {
const pending: PropertyFrame[] = [
{ current: value, path: prefix, inMemberArray: false },
];
const scalarPatterns =
seedKind === "string" ? null : SCALAR_PATTERNS[seedKind];
const memberPatterns =
seedKind === "string" ? null : MEMBER_PATTERNS[seedKind];
while (pending.length > 0) {
const next = pending.pop();
if (next === undefined) continue;
const { current, path, selectionPath } = next;
const { current, path, inMemberArray } = next;
if (typeof current === "string") {
output.push({
basis: "property",
field: path,
selectionPath,
value: current,
});
if (textMatches(current, query, mode, caseSensitive)) return path;
continue;
}
if (Array.isArray(current)) {
for (let index = current.length - 1; index >= 0; index -= 1) {
const item = current[index];
if (item !== undefined)
if (item === null || item === undefined) continue;
if (typeof item === "string") {
if (seedKind === "string" || inMemberArray)
pending.push({
current: item,
path: `${path}[${String(index)}]`,
inMemberArray,
});
continue;
}
if (typeof item === "object")
pending.push({
current: item,
path: `${path}[${String(index)}]`,
selectionPath,
inMemberArray,
});
}
continue;
}
if (current !== null && typeof current === "object") {
for (const key of Object.keys(current)
.sort(compareUnicodeCodePoints)
.reverse())
pending.push({
current: current[key] ?? null,
path: `${path}.${key}`,
selectionPath: `${selectionPath}.${key}`,
});
const keys = Object.keys(current);
if (keys.length > 1) keys.sort(compareUnicodeCodePoints);
for (let index = keys.length - 1; index >= 0; index -= 1) {
const key = keys[index]!;
const child = current[key];
if (child === null || child === undefined) continue;
if (typeof child === "string") {
if (scalarPatterns === null || keyMatchesAny(key, scalarPatterns))
pending.push({
current: child,
path: `${path}.${key}`,
inMemberArray: false,
});
continue;
}
if (Array.isArray(child)) {
if (child.length === 0) continue;
pending.push({
current: child,
path: `${path}.${key}`,
inMemberArray:
memberPatterns !== null && keyMatchesAny(key, memberPatterns),
});
continue;
}
if (typeof child === "object")
pending.push({
current: child,
path: `${path}.${key}`,
inMemberArray: false,
});
}
}
}
return output;
return undefined;
};
/** Normalize literal matching identically across application and semantic labels. */
@@ -275,8 +442,16 @@ export const normalizeFeatureSeedText = (
value: string,
caseSensitive: boolean,
): string => {
const normalizedValue = value.normalize(caseSensitive ? "NFC" : "NFKC");
return caseSensitive
? normalizedValue
: normalizedValue.toLocaleLowerCase("en-US");
let hasUpper = false;
for (let index = 0; index < value.length; index += 1) {
const code = value.charCodeAt(index);
if (code > 0x7f) {
const normalizedValue = value.normalize(caseSensitive ? "NFC" : "NFKC");
return caseSensitive
? normalizedValue
: normalizedValue.toLocaleLowerCase("en-US");
}
if (code >= 65 && code <= 90) hasUpper = true;
}
return caseSensitive || !hasUpper ? value : value.toLowerCase();
};