perf(javascript): share one lexical index between static and semantic analysis (#14148)

* perf(javascript): share one lexical index between static and semantic analysis

The static and semantic phases each built a private semantic state for
the same parsed program, so definitions, ambient writes and
member-mutation facts were derived twice per file.

Sharing the state requires separating what it holds. Index facts are
shared. Memoized query results are declared through one owner so a new
consumer or index step starts without them. Observations (resource
limits and returned-reference widenings) are recorded per derivation:
index steps record into the index's own log and each consumer starts
from that log, so the semantic phase reports exactly what a private
index would have.

The worker builds one index per parsed file and the semantic phase
continues from it when its resolution mode matches the static phase.

Refs #14147

* fix(javascript): let a shared lexical index continue only one semantic phase

The semantic phase appends its module links and callable facts to the
state it continues from, so a second phase from the same index reported
each import link twice. Admission is now consumed by the first phase and
a second is rejected like any other index that does not match.
This commit is contained in:
morluto authored and GitHub committed 2026-11-23 23:20:26 +00:00
1 parent 03f68dbcba
commit 24d2c4f16b
29 files changed
+579 -147

No files matched your search

@@ -6,7 +6,11 @@ import {
} from "../domain/javascript/javascriptStaticAnalysis.js";
import type { JavaScriptStaticAnalysis } from "../domain/javascript/javascriptStaticAnalysisTypes.js";
import { isJavaScriptMemberMutationRefusal } from "../domain/javascript/javascriptSemanticWorkAllowance.js";
import { analyzeParsedJavaScriptSemanticsSteps } from "../domain/javascript/javascriptSemanticAnalysis.js";
import {
analyzeParsedJavaScriptSemanticsSteps,
indexJavaScriptBindingsWithReceiversSteps,
type JavaScriptReceiverIndex,
} from "../domain/javascript/javascriptSemanticAnalysis.js";
import { failedJavaScriptStaticAnalysis } from "../domain/javascript/javascriptStaticAnalysisHelpers.js";
import { projectJavaScriptModuleSemantics } from "../domain/javascript/javascriptModuleSemanticIr.js";
import {
@@ -22,6 +26,9 @@ import {
} from "../javascript/analysis/JavaScriptAnalysisWorkerServer.js";
import { errorMessage } from "../kernel/errorMessage.js";
/** The resolution mode `indexJavaScriptBindingsWithReceiversSteps` indexes under. */
const STATIC_PHASE_RESOLUTION_MODE = "import";
const analyze: JavaScriptSourceWorkerAnalyzer = async (
file,
capacity,
@@ -51,7 +58,7 @@ const analyze: JavaScriptSourceWorkerAnalyzer = async (
// Structure-only keeps the full static facts and skips semantic analysis;
// no semantic result budget is consumed.
if (static_ === null || purpose === "structure-only") return null;
const { resultBudgetBytes, callPaths } = static_;
const { resultBudgetBytes, callPaths, lexicalIndex } = static_;
if (resultBudgetBytes === 0)
throw new AnalysisResourceConstraintError(
"analyze_javascript_application",
@@ -61,13 +68,22 @@ const analyze: JavaScriptSourceWorkerAnalyzer = async (
{ measuredExhaustion: true },
);
await callbacks.phase("semantic");
const resolutionMode = moduleResolutionModeForJavaScriptReference(
file.path,
"static-import",
);
const ir = await completeJavaScriptAnalysisSteps(
analyzeParsedJavaScriptSemanticsSteps(
parsed,
moduleResolutionModeForJavaScriptReference(file.path, "static-import"),
resolutionMode,
capacity.nodes,
precision,
memberMutationAllowance,
// The static phase indexes under "import"; any other mode changes the
// definitions themselves, so the semantic phase collects its own.
resolutionMode === STATIC_PHASE_RESOLUTION_MODE
? lexicalIndex
: undefined,
),
);
parsed = null;
@@ -111,6 +127,8 @@ const staticCheckpoint = async (
): Promise<{
readonly resultBudgetBytes: number;
readonly callPaths: JavaScriptStaticAnalysis["electron"]["call_paths"];
/** The index this job's static phase built; absent for reused facts. */
readonly lexicalIndex?: JavaScriptReceiverIndex;
} | null> => {
const reused =
purpose === "preload-discovery" ? undefined : callbacks.reusedStatic();
@@ -121,13 +139,25 @@ const staticCheckpoint = async (
: { resultBudgetBytes, callPaths: reused.call_paths };
}
let facts: JavaScriptStaticAnalysis;
let lexicalIndex: JavaScriptReceiverIndex;
try {
// One lexical index per parsed file: the static phase reads it here and
// the semantic phase continues from it instead of rebuilding it.
lexicalIndex = await completeJavaScriptAnalysisSteps(
indexJavaScriptBindingsWithReceiversSteps(
parsed.program,
undefined,
precision,
memberMutationAllowance,
),
);
facts = await completeJavaScriptAnalysisSteps(
analyzeParsedJavaScriptStaticSourceSteps(
source,
parsed,
precision,
memberMutationAllowance,
lexicalIndex,
),
);
} catch (cause: unknown) {
@@ -168,7 +198,11 @@ const staticCheckpoint = async (
const resultBudgetBytes = await callbacks.checkpoint(facts);
return facts.parse_status === "failed"
? null
: { resultBudgetBytes, callPaths: facts.electron.call_paths };
: {
resultBudgetBytes,
callPaths: facts.electron.call_paths,
lexicalIndex,
};
};
try {