Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache programs #202

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@andrewbranch/untar.js": "^1.0.3",
"cjs-module-lexer": "^1.2.3",
"fflate": "^0.8.2",
"lru-cache": "^11.0.1",
"semver": "^7.5.4",
"typescript": "5.6.1-rc",
"validate-npm-package-name": "^5.0.0"
Expand Down
28 changes: 18 additions & 10 deletions packages/core/src/internal/multiCompilerHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts from "typescript";
import { LRUCache } from "lru-cache";
import type { ModuleKind } from "../types.js";
import type { Package } from "../createPackage.js";

Expand Down Expand Up @@ -37,6 +38,7 @@ const getCanonicalFileName = ts.createGetCanonicalFileName(false);
const toPath = (fileName: string) => ts.toPath(fileName, "/", getCanonicalFileName);

export class CompilerHostWrapper {
private programCache = new LRUCache<string, ts.Program>({ max: 2 });
private compilerHost: ts.CompilerHost;
private compilerOptions: ts.CompilerOptions;
private normalModuleResolutionCache: ts.ModuleResolutionCache;
Expand Down Expand Up @@ -162,12 +164,17 @@ export class CompilerHostWrapper {
return `${resolutionMode ?? 1}:${+!!noDtsResolution}:${+!!allowJs}:${moduleSpecifier}`;
}

private getProgram(rootNames: readonly string[], options: ts.CompilerOptions) {
const key = programKey(rootNames, options);
let program = this.programCache.get(key);
if (!program) {
this.programCache.set(key, (program = ts.createProgram({ rootNames, options, host: this.compilerHost })));
}
return program;
}

createPrimaryProgram(rootName: string) {
const program = ts.createProgram({
rootNames: [rootName],
options: this.compilerOptions,
host: this.compilerHost,
});
const program = this.getProgram([rootName], this.compilerOptions);

program.resolvedModules?.forEach((cache, path) => {
let ownCache = this.resolvedModules.get(path);
Expand Down Expand Up @@ -199,11 +206,8 @@ export class CompilerHostWrapper {
) {
throw new Error("Cannot override resolution-affecting options for host due to potential cache polution");
}
return ts.createProgram({
rootNames,
options: extraOptions ? { ...this.compilerOptions, ...extraOptions } : this.compilerOptions,
host: this.compilerHost,
});
const options = extraOptions ? { ...this.compilerOptions, ...extraOptions } : this.compilerOptions;
return this.getProgram(rootNames, options);
}

getResolvedModule(sourceFile: ts.SourceFile, moduleName: string, resolutionMode: ts.ResolutionMode) {
Expand Down Expand Up @@ -304,3 +308,7 @@ class TraceCollector {
this.traces.length = 0;
}
}

function programKey(rootNames: readonly string[], options: ts.CompilerOptions) {
return JSON.stringify([rootNames, Object.entries(options).sort(([k1], [k2]) => k1.localeCompare(k2))]);
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading