mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
perf(javascript): cache ambient resolver and fix O(n^2) semantic hot paths (#13716)
This commit is contained in:
1 parent
aab47683f5
commit
0f987ff71e
6 files changed
+116
-67
No files matched your search
@@ -1703,8 +1703,11 @@ const resolveFileCandidates = (
|
||||
return null;
|
||||
};
|
||||
|
||||
const ASAR_SEGMENT_PATTERN = /\.asar\//iu;
|
||||
|
||||
/** Classify the extension as inventory does; retain exact archive occurrence bytes. */
|
||||
const runtimeArchiveRoot = (path: string): string | undefined => {
|
||||
if (!ASAR_SEGMENT_PATTERN.test(path)) return undefined;
|
||||
const components = path.split("/");
|
||||
const archive = components.findLastIndex(
|
||||
(component, index) =>
|
||||
@@ -2072,16 +2075,39 @@ const selectExportTarget = (
|
||||
* built-in resolver that owns installed-package imports and requires. The
|
||||
* supported Node runtimes also activate "module-sync" for both loaders.
|
||||
*/
|
||||
const EXPORT_CONDITIONS_DEFAULT: ReadonlySet<string> = new Set([
|
||||
"node",
|
||||
"node-addons",
|
||||
"module-sync",
|
||||
"default",
|
||||
]);
|
||||
const EXPORT_CONDITIONS_BY_KIND = new Map<string, ReadonlySet<string>>([
|
||||
[
|
||||
"import",
|
||||
new Set(["node", "node-addons", "module-sync", "import", "default"]),
|
||||
],
|
||||
[
|
||||
"require",
|
||||
new Set(["node", "node-addons", "module-sync", "require", "default"]),
|
||||
],
|
||||
[
|
||||
"compiler-dependent",
|
||||
new Set([
|
||||
"node",
|
||||
"node-addons",
|
||||
"module-sync",
|
||||
"compiler-dependent",
|
||||
"default",
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
const packageExportConditions = (
|
||||
moduleKind: ResolveArtifactPathInput["moduleKind"],
|
||||
): ReadonlySet<string> =>
|
||||
new Set([
|
||||
"node",
|
||||
"node-addons",
|
||||
"module-sync",
|
||||
...(moduleKind === undefined ? [] : [moduleKind]),
|
||||
"default",
|
||||
]);
|
||||
moduleKind === undefined
|
||||
? EXPORT_CONDITIONS_DEFAULT
|
||||
: (EXPORT_CONDITIONS_BY_KIND.get(moduleKind) ?? EXPORT_CONDITIONS_DEFAULT);
|
||||
|
||||
const notFoundCandidate = (): Extract<
|
||||
CandidateResolution,
|
||||
|
||||
@@ -228,10 +228,12 @@ export const analyzeParsedJavaScriptReferences = (
|
||||
const indexedAmbientWriteStates =
|
||||
new WeakSet<JavaScriptSemanticAnalysisState>();
|
||||
/** The ambient-write owner resolves the global roots a reference may alias. */
|
||||
const ambientRootPaths =
|
||||
(state: JavaScriptSemanticAnalysisState): SemanticAmbientRootPaths =>
|
||||
(root) =>
|
||||
createAmbientPathResolver(state, false)(root).paths;
|
||||
const ambientRootPaths = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): SemanticAmbientRootPaths => {
|
||||
const resolver = createAmbientPathResolver(state, false);
|
||||
return (root) => resolver(root).paths;
|
||||
};
|
||||
|
||||
function* indexSemanticAmbientWritesSteps(
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
@@ -1650,7 +1652,7 @@ function* indexPrototypeUncertaintySteps(
|
||||
return;
|
||||
if (
|
||||
(t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) &&
|
||||
parent.arguments.includes(node as t.Expression)
|
||||
parent.callee !== node
|
||||
)
|
||||
return;
|
||||
if (
|
||||
|
||||
@@ -737,7 +737,7 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
const recordBindingEffect = (
|
||||
records: JavaScriptSemanticMemberEffect[],
|
||||
effect: JavaScriptSemanticMemberEffect,
|
||||
): void => {
|
||||
): boolean => {
|
||||
const fields: {
|
||||
readonly [Field in keyof JavaScriptSemanticMemberEffect]-?: unknown;
|
||||
} = {
|
||||
@@ -758,9 +758,10 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
recorded = new Set();
|
||||
recordedBindingEffects.set(records, recorded);
|
||||
}
|
||||
if (recorded.has(key)) return;
|
||||
if (recorded.has(key)) return false;
|
||||
recorded.add(key);
|
||||
records.push(effect);
|
||||
return true;
|
||||
};
|
||||
// Ambient indexing owns aliases of global objects. Consume its canonical
|
||||
// writes instead of reparsing qualified or aliased process assignments.
|
||||
@@ -1148,11 +1149,6 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
// An alias restored from a saved reference can revisit the same binding
|
||||
// at an earlier capture or a shorter getter-receiver path. Only a
|
||||
// repeated capture and path closes a cycle.
|
||||
const captureSelectionIdentity = JSON.stringify([
|
||||
current.memberReadAt?.start,
|
||||
current.memberReadAt?.end,
|
||||
capturedSelectionKey(current.capturedSelections),
|
||||
]);
|
||||
// Ancestry closes repeated source identities before recursively selected
|
||||
// copy vectors grow; independent contexts remain distinct in coverage.
|
||||
const captureKey = JSON.stringify([
|
||||
@@ -1177,6 +1173,11 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
);
|
||||
const selector = path[selectorIndex];
|
||||
if (selector !== null && typeof selector === "object") {
|
||||
const captureSelectionIdentity = JSON.stringify([
|
||||
current.memberReadAt?.start,
|
||||
current.memberReadAt?.end,
|
||||
capturedSelectionKey(current.capturedSelections),
|
||||
]);
|
||||
const selectionIdentity = JSON.stringify([
|
||||
binding.bindingId,
|
||||
effect,
|
||||
@@ -1353,8 +1354,6 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
: undefined;
|
||||
const primitiveWrite =
|
||||
value?.status === "literal" || value?.status === "union";
|
||||
// The coverage index already owns binding-effect deduplication.
|
||||
recordedEffectCount += 1;
|
||||
const nested = new Set(current.bindings);
|
||||
nested.add(lifetime);
|
||||
nested.add(captureKey);
|
||||
@@ -1412,23 +1411,26 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
current.path.length > 1
|
||||
? (current.memberReadAt ?? current.originAt)
|
||||
: current.originAt;
|
||||
if (effect === "escape")
|
||||
recordBindingEffect(binding.escapedPaths, {
|
||||
path,
|
||||
node: mutation,
|
||||
receiverInitializers,
|
||||
...(receiverAt === undefined ? {} : { receiverAt }),
|
||||
});
|
||||
else
|
||||
recordBindingEffect(binding.memberWrites, {
|
||||
receiverIdentity: definiteReceiver ? "definite" : "possible",
|
||||
path,
|
||||
node: mutation,
|
||||
receiverInitializers,
|
||||
...(receiverAt === undefined ? {} : { receiverAt }),
|
||||
...(assignedSources === undefined ? {} : { assignedSources }),
|
||||
});
|
||||
clearSemanticValueCaches(state);
|
||||
const added =
|
||||
effect === "escape"
|
||||
? recordBindingEffect(binding.escapedPaths, {
|
||||
path,
|
||||
node: mutation,
|
||||
receiverInitializers,
|
||||
...(receiverAt === undefined ? {} : { receiverAt }),
|
||||
})
|
||||
: recordBindingEffect(binding.memberWrites, {
|
||||
receiverIdentity: definiteReceiver ? "definite" : "possible",
|
||||
path,
|
||||
node: mutation,
|
||||
receiverInitializers,
|
||||
...(receiverAt === undefined ? {} : { receiverAt }),
|
||||
...(assignedSources === undefined ? {} : { assignedSources }),
|
||||
});
|
||||
if (added) {
|
||||
recordedEffectCount += 1;
|
||||
clearSemanticValueCaches(state);
|
||||
}
|
||||
// Resolve a stored child at the receiver capture, before selecting
|
||||
// the final written slot. The source owner preserves replacements
|
||||
// and captures; a local container does not isolate its children.
|
||||
@@ -2606,6 +2608,10 @@ const observableMutationPaths = (
|
||||
link(expressionNode(source.node), observer);
|
||||
}
|
||||
}
|
||||
const expandedOrigins = new WeakMap<
|
||||
MutationObserverNode,
|
||||
Set<MutationObservableOrigin>
|
||||
>();
|
||||
const settle = (): void => {
|
||||
while (pendingExpressions.length > 0 || pending.length > 0) {
|
||||
while (pendingExpressions.length > 0) {
|
||||
@@ -2761,6 +2767,13 @@ const observableMutationPaths = (
|
||||
)
|
||||
add("array-prototype");
|
||||
} else {
|
||||
let expanded = expandedOrigins.get(observer);
|
||||
if (expanded === undefined) {
|
||||
expanded = new Set();
|
||||
expandedOrigins.set(observer, expanded);
|
||||
}
|
||||
if (expanded.has(origin)) continue;
|
||||
expanded.add(origin);
|
||||
for (const reference of referencedValues(
|
||||
origin,
|
||||
[key],
|
||||
|
||||
@@ -1701,23 +1701,26 @@ export const semanticBindingAtRead = (
|
||||
const { position, repeatingLoop } = bindingReadPosition(read, parents);
|
||||
// Coverage and latest-value selection are independent. A preceding write
|
||||
// can cover entry even when another write's order remains unknown.
|
||||
const initializerCoversRead = (
|
||||
entry: Parameters<typeof initializerPosition>[0],
|
||||
isReference: boolean,
|
||||
): boolean => {
|
||||
const source = initializerPosition(entry, parents, isReference);
|
||||
return (
|
||||
source !== null &&
|
||||
position !== null &&
|
||||
positionPrecedes(source, position) &&
|
||||
!initializerMayRetainEntry(entry, binding, parents)
|
||||
);
|
||||
};
|
||||
const hasDefiniteInitializer =
|
||||
position !== null &&
|
||||
[
|
||||
...binding.initializers,
|
||||
...binding.referenceInitializers.filter(({ assignment }) => assignment),
|
||||
].some((initializer) => {
|
||||
const source = initializerPosition(
|
||||
initializer,
|
||||
parents,
|
||||
binding.referenceInitializers.includes(initializer),
|
||||
);
|
||||
return (
|
||||
source !== null &&
|
||||
positionPrecedes(source, position) &&
|
||||
!initializerMayRetainEntry(initializer, binding, parents)
|
||||
);
|
||||
});
|
||||
(binding.initializers.some((entry) =>
|
||||
initializerCoversRead(entry, false),
|
||||
) ||
|
||||
binding.referenceInitializers.some(
|
||||
(entry) => entry.assignment && initializerCoversRead(entry, true),
|
||||
));
|
||||
if (
|
||||
!repeatingLoop &&
|
||||
origin !== undefined &&
|
||||
|
||||
@@ -1215,10 +1215,17 @@ const hasCommonJsModuleSource = (
|
||||
// created after exhaustion. Unnormalized paths can grow without a binding.
|
||||
let exhausted = false;
|
||||
let drained = 0;
|
||||
let pendingHead = 0;
|
||||
for (
|
||||
let next = pending.pop();
|
||||
next !== undefined;
|
||||
next = exhausted ? pending.shift() : pending.pop()
|
||||
next = exhausted
|
||||
? pendingHead < pending.length
|
||||
? pending[pendingHead++]
|
||||
: undefined
|
||||
: pendingHead < pending.length
|
||||
? pending.pop()
|
||||
: undefined
|
||||
) {
|
||||
const value = unwrapJavaScriptExpression(next.node).node;
|
||||
const { awaitDepths } = next;
|
||||
|
||||
@@ -538,22 +538,19 @@ export class JavaScriptSymbolicBindings {
|
||||
if (property !== undefined)
|
||||
return this.#objectPropertyValue(property, value.environment);
|
||||
if (key === "__proto__") {
|
||||
const prototypeSetter = [...value.node.properties]
|
||||
.reverse()
|
||||
.filter(
|
||||
(candidate): candidate is t.ObjectMethod | t.ObjectProperty =>
|
||||
!t.isSpreadElement(candidate),
|
||||
for (let i = value.node.properties.length - 1; i >= 0; i -= 1) {
|
||||
const candidate = value.node.properties[i]!;
|
||||
if (
|
||||
!t.isSpreadElement(candidate) &&
|
||||
isObjectPrototypeSetter(candidate)
|
||||
)
|
||||
.find((candidate) => isObjectPrototypeSetter(candidate));
|
||||
if (prototypeSetter !== undefined)
|
||||
return this.#objectPropertyValue(
|
||||
prototypeSetter,
|
||||
value.environment,
|
||||
);
|
||||
return this.#objectPropertyValue(candidate, value.environment);
|
||||
}
|
||||
}
|
||||
return symbolicUnknown("missing property");
|
||||
}
|
||||
for (const p of [...value.node.properties].reverse()) {
|
||||
for (let i = value.node.properties.length - 1; i >= 0; i -= 1) {
|
||||
const p = value.node.properties[i]!;
|
||||
if (t.isSpreadElement(p)) {
|
||||
const spread = this.evaluate(p.argument, value.environment);
|
||||
const found = this.property(spread, key, {
|
||||
@@ -687,12 +684,13 @@ export class JavaScriptSymbolicBindings {
|
||||
(t.isSpreadElement(p)
|
||||
? this.unknownMembers(
|
||||
this.evaluate(p.argument, value.environment),
|
||||
new Set(active),
|
||||
active,
|
||||
)
|
||||
: Number(semanticStaticPropertyKey(p.key, p.computed) === null)),
|
||||
0,
|
||||
);
|
||||
} finally {
|
||||
active.delete(value);
|
||||
this.#depth -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user