-
Notifications
You must be signed in to change notification settings - Fork 17
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
Optimization: cache results between TypeScript projects #182
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,53 @@ | ||
import * as path from 'path' | ||
import { Writable as WritableStream } from 'stream' | ||
|
||
import prettyMilliseconds from 'pretty-ms' | ||
import ProgressBar from 'progress' | ||
import * as ts from 'typescript' | ||
|
||
import { ProjectOptions } from './CommandLineOptions' | ||
import { GlobalCache, ProjectOptions } from './CommandLineOptions' | ||
import { FileIndexer } from './FileIndexer' | ||
import { Input } from './Input' | ||
import * as lsif from './lsif' | ||
import { LsifSymbol } from './LsifSymbol' | ||
import { Packages } from './Packages' | ||
|
||
function createCompilerHost( | ||
cache: GlobalCache, | ||
compilerOptions: ts.CompilerOptions, | ||
projectOptions: ProjectOptions | ||
): ts.CompilerHost { | ||
const host = ts.createCompilerHost(compilerOptions) | ||
if (!projectOptions.globalCaches) { | ||
return host | ||
} | ||
const hostCopy = { ...host } | ||
host.getParsedCommandLine = (fileName: string) => { | ||
if (!hostCopy.getParsedCommandLine) { | ||
return undefined | ||
} | ||
if (cache.parsedCommandLines.has(fileName)) { | ||
return cache.parsedCommandLines.get(fileName) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand all the cases here; is it possible that the cache has a hit for this even though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be misunderstanding your comment, but we guard against |
||
const result = hostCopy.getParsedCommandLine(fileName) | ||
if (result !== undefined) { | ||
cache.parsedCommandLines.set(fileName, result) | ||
} | ||
return result | ||
} | ||
host.getSourceFile = (fileName, languageVersion) => { | ||
const fromCache = cache.sources.get(fileName) | ||
if (fromCache !== undefined) { | ||
return fromCache | ||
} | ||
const result = hostCopy.getSourceFile(fileName, languageVersion) | ||
if (result !== undefined) { | ||
cache.sources.set(fileName, result) | ||
} | ||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Maybe add a comment here or near the cache definition which says "It is safe use the file name as a key here because we assume that a source file only ever belongs to a single project, and is only ever type-checked with a single language version". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call! I added an additional check to additionally only cache when |
||
} | ||
return host | ||
} | ||
|
||
export class ProjectIndexer { | ||
private options: ProjectOptions | ||
private program: ts.Program | ||
|
@@ -20,10 +56,12 @@ export class ProjectIndexer { | |
private packages: Packages | ||
constructor( | ||
public readonly config: ts.ParsedCommandLine, | ||
options: ProjectOptions | ||
options: ProjectOptions, | ||
cache: GlobalCache | ||
) { | ||
this.options = options | ||
this.program = ts.createProgram(config.fileNames, config.options) | ||
const host = createCompilerHost(cache, config.options, options) | ||
this.program = ts.createProgram(config.fileNames, config.options, host) | ||
this.checker = this.program.getTypeChecker() | ||
this.packages = new Packages(options.projectRoot) | ||
} | ||
|
@@ -47,24 +85,24 @@ export class ProjectIndexer { | |
) | ||
} | ||
|
||
const jobs = new ProgressBar( | ||
` ${this.options.projectDisplayName} [:bar] :current/:total :title`, | ||
{ | ||
total: filesToIndex.length, | ||
renderThrottle: 100, | ||
incomplete: '_', | ||
complete: '#', | ||
width: 20, | ||
clear: true, | ||
stream: this.options.progressBar | ||
? process.stderr | ||
: writableNoopStream(), | ||
} | ||
) | ||
const jobs: ProgressBar | undefined = !this.options.progressBar | ||
? undefined | ||
: new ProgressBar( | ||
` ${this.options.projectDisplayName} [:bar] :current/:total :title`, | ||
{ | ||
total: filesToIndex.length, | ||
renderThrottle: 100, | ||
incomplete: '_', | ||
complete: '#', | ||
width: 20, | ||
clear: true, | ||
stream: process.stderr, | ||
} | ||
) | ||
let lastWrite = startTimestamp | ||
for (const [index, sourceFile] of filesToIndex.entries()) { | ||
const title = path.relative(this.options.cwd, sourceFile.fileName) | ||
jobs.tick({ title }) | ||
jobs?.tick({ title }) | ||
if (!this.options.progressBar) { | ||
const now = Date.now() | ||
const elapsed = now - lastWrite | ||
|
@@ -102,7 +140,7 @@ export class ProjectIndexer { | |
) | ||
} | ||
} | ||
jobs.terminate() | ||
jobs?.terminate() | ||
const elapsed = Date.now() - startTimestamp | ||
if (!this.options.progressBar && lastWrite > startTimestamp) { | ||
process.stdout.write('\n') | ||
|
@@ -112,11 +150,3 @@ export class ProjectIndexer { | |
) | ||
} | ||
} | ||
|
||
function writableNoopStream(): WritableStream { | ||
return new WritableStream({ | ||
write(_unused1, _unused2, callback) { | ||
setImmediate(callback) | ||
}, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this get removed? Shouldn't there be an extra
globalCaches: true
here instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to mention it in the description but this PR also disables the fancy progress bar by default. I didn't measure any performance difference with it enabled/disabled, but I found it distracting while testing projects. scip-typescript runs >99% of the time in CI where you should disable it by default anyways and I'm guessing most people are not doing it.