From 2a581ed95381e461b084b83770a257ebdde3159e Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Mon, 23 Nov 2026 14:12:22 +0000 Subject: [PATCH] perf(javascript): build loader context keys without JSON serialization Every module-source work item serialized its read site and capture boundaries to JSON to key its context. Build the same key by concatenation; absent and null positions still share a key, and absent captures still differ from an empty capture list. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- .../javascriptSemanticRequireOrigin.ts | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/domain/javascript/javascriptSemanticRequireOrigin.ts b/src/domain/javascript/javascriptSemanticRequireOrigin.ts index e21ec1cb57b..9f13468d635 100644 --- a/src/domain/javascript/javascriptSemanticRequireOrigin.ts +++ b/src/domain/javascript/javascriptSemanticRequireOrigin.ts @@ -1035,19 +1035,24 @@ type ModuleObjectSource = { | undefined; }; -/** Both loader queries retain the same read and shallow-copy selection owner. */ -const loaderSourceContextKey = (source: ModuleObjectSource): string => - JSON.stringify([ - source.eligibility, - source.readAt?.start, - source.readAt?.end, - source.capturedSelections?.map(({ at, depth, ownEnumerable }) => [ - at.start, - at.end, - depth, - ownEnumerable, - ]), - ]); +/** A source position, with null and absent positions sharing one key. */ +const positionKey = (position: number | null | undefined): string => + position === null || position === undefined ? "-" : String(position); + +/** + * Both loader queries retain the same read and shallow-copy selection owner. + * Absent captures differ from an empty capture list. This hot key is built + * without JSON serialization. + */ +const loaderSourceContextKey = (source: ModuleObjectSource): string => { + let key = `${source.eligibility === true ? "e" : "-"}|${positionKey(source.readAt?.start)}|${positionKey(source.readAt?.end)}|`; + const captures = source.capturedSelections; + if (captures === undefined) return `${key}-`; + key += "["; + for (const { at, depth, ownEnumerable } of captures) + key += `${positionKey(at.start)},${positionKey(at.end)},${String(depth)},${ownEnumerable === true ? "o" : "-"};`; + return key; +}; const inheritLoaderSourceContext = ( current: ModuleObjectSource, @@ -1289,7 +1294,7 @@ const walkCommonJsModuleSource = ( const byMode = contexts.get(value) ?? new Map }>(); - const modeKey = JSON.stringify([awaitDepths, loaderSourceContextKey(next)]); + const modeKey = `${awaitDepths.join(",")}#${loaderSourceContextKey(next)}`; let context = byMode.get(modeKey); if (context === undefined) { context = { identities: new Map() };