Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

perf(ingest): memoize module resolution

KageBinaryc43303e56bb0
diff --git a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
index 8f1be5a8..0eedd9a7 100644
--- a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
+++ b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
@@ -207,6 +207,35 @@ describe("createTypeScriptModuleResolver", () => {
expect(resolve("src/consumer.js", "services/worker")).toEqual(["src/services/worker.js"]);
});
+ it("does not serve one caller's language preference to another in the same directory", () => {
+ // Guards the memo key: results are cached, and callers in the same folder
+ // share a config but not a preference order. Keying on the directory alone
+ // would hand the .js answer to the .ts caller, or whichever ran first.
+ const root = workspace();
+ const config = write(
+ root,
+ "jsconfig.json",
+ JSON.stringify({ compilerOptions: { baseUrl: "src" } }),
+ );
+ const jsConsumer = write(root, "src/consumer.js");
+ const tsConsumer = write(root, "src/consumer.ts");
+ const javascript = write(root, "src/services/worker.js");
+ const typescript = write(root, "src/services/worker.ts");
+ const resolve = createTypeScriptModuleResolver(root, [
+ config,
+ jsConsumer,
+ tsConsumer,
+ javascript,
+ typescript,
+ ]);
+
+ // Order matters: whichever runs first would poison a directory-only key.
+ expect(resolve("src/consumer.js", "services/worker")).toEqual(["src/services/worker.js"]);
+ expect(resolve("src/consumer.ts", "services/worker")).toEqual(["src/services/worker.ts"]);
+ // And repeated asks, which is what the cache exists for, stay stable.
+ expect(resolve("src/consumer.js", "services/worker")).toEqual(["src/services/worker.js"]);
+ });
+
it("matches mapped paths case-insensitively on case-insensitive filesystems", () => {
const root = workspace();
const config = write(
diff --git a/ix-cli/src/cli/ts-module-resolution.ts b/ix-cli/src/cli/ts-module-resolution.ts
index f50e295d..397dc7af 100644
--- a/ix-cli/src/cli/ts-module-resolution.ts
+++ b/ix-cli/src/cli/ts-module-resolution.ts
@@ -290,10 +290,17 @@ export function createTypeScriptModuleResolver(
.filter((config): config is LoadedConfig => config !== undefined)
.sort((a, b) => b.dir.length - a.dir.length);
- return (sourcePath: string, rawSpecifier: string, kind = "runtime"): string[] | undefined => {
- const specifier = rawSpecifier.split(/[?#]/, 1)[0];
- if (!specifier || specifier.startsWith(".") || specifier.startsWith("/")) return undefined;
- const normalizedSource = normalizePath(sourcePath);
+ // Ingestion asks the same question repeatedly: resolution runs per bound
+ // relationship, and index.ts issues two full lookups of the same triple for
+ // each one. Only three things about a call change the answer — see the key.
+ const resolutionCache = new Map<string, string[] | undefined>();
+
+ const resolveUncached = (
+ sourcePath: string,
+ normalizedSource: string,
+ specifier: string,
+ kind: "runtime" | "types",
+ ): string[] | undefined => {
const config = configs.find((candidate) => isWithinDir(normalizedSource, candidate.dir));
// Only an *explicit* `paths` mapping licenses an authoritative "no match".
// A bare `baseUrl` — and a catch-all `"*"` rule, which is the same thing
@@ -329,4 +336,25 @@ export function createTypeScriptModuleResolver(
}
return mappedExplicitly ? [] : undefined;
};
+
+ return (sourcePath: string, rawSpecifier: string, kind = "runtime"): string[] | undefined => {
+ const specifier = rawSpecifier.split(/[?#]/, 1)[0];
+ if (!specifier || specifier.startsWith(".") || specifier.startsWith("/")) return undefined;
+ const normalizedSource = normalizePath(sourcePath);
+ // Config selection reads only the source *directory*; candidate ordering
+ // additionally reads the source *extension* (a .js caller prefers .js over
+ // .ts). Nothing else about the path is consulted, so this key is exact —
+ // keying on the directory alone would serve a .js caller's answer to a .ts
+ // caller in the same folder and silently flip the preference order.
+ const cacheKey = [
+ nodePath.posix.dirname(normalizedSource),
+ nodePath.posix.extname(normalizedSource).toLowerCase(),
+ specifier,
+ kind,
+ ].join("");
+ if (resolutionCache.has(cacheKey)) return resolutionCache.get(cacheKey);
+ const result = resolveUncached(sourcePath, normalizedSource, specifier, kind);
+ resolutionCache.set(cacheKey, result);
+ return result;
+ };
}