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

Optimization: cache results between TypeScript projects #182

Merged
merged 3 commits into from
Oct 18, 2022
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
3 changes: 2 additions & 1 deletion src/CommandLineOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function checkIndexParser(

// defaults
checkIndexParser([], {
progressBar: true,
Copy link
Contributor

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?

Copy link
Member Author

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.

cwd: process.cwd(),
inferTsconfig: false,
output: 'index.scip',
Expand All @@ -35,3 +34,5 @@ checkIndexParser(['--cwd', 'qux'], { cwd: 'qux' })
checkIndexParser(['--yarn-workspaces'], { yarnWorkspaces: true })
checkIndexParser(['--infer-tsconfig'], { inferTsconfig: true })
checkIndexParser(['--no-progress-bar'], { progressBar: false })
checkIndexParser(['--progress-bar'], { progressBar: true })
checkIndexParser(['--no-global-caches'], { globalCaches: false })
19 changes: 18 additions & 1 deletion src/CommandLineOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Command } from 'commander'
// eslint-disable-next-line id-length
import ts from 'typescript'

import packageJson from '../package.json'

Expand All @@ -10,6 +12,7 @@ export interface MultiProjectOptions {
progressBar: boolean
yarnWorkspaces: boolean
yarnBerryWorkspaces: boolean
globalCaches: boolean
cwd: string
output: string
indexedProjects: Set<string>
Expand All @@ -22,6 +25,15 @@ export interface ProjectOptions extends MultiProjectOptions {
writeIndex: (index: lsif.lib.codeintel.lsiftyped.Index) => void
}

/** Cached values */
export interface GlobalCache {
sources: Map<
string,
[ts.SourceFile | undefined, ts.ScriptTarget | ts.CreateSourceFileOptions]
>
parsedCommandLines: Map<string, ts.ParsedCommandLine>
}

export function mainCommand(
indexAction: (projects: string[], otpions: MultiProjectOptions) => void
): Command {
Expand All @@ -47,7 +59,12 @@ export function mainCommand(
false
)
.option('--output <path>', 'path to the output file', 'index.scip')
.option('--no-progress-bar', 'whether to disable the progress bar')
.option('--progress-bar', 'whether to enable a rich progress bar')
.option('--no-progress-bar', 'whether to disable the rich progress bar')
.option(
'--no-global-caches',
'whether to disable global caches between TypeScript projects'
)
.argument('[projects...]')
.action((parsedProjects, parsedOptions) => {
indexAction(
Expand Down
124 changes: 98 additions & 26 deletions src/ProjectIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
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
}
const fromCache = cache.parsedCommandLines.get(fileName)
if (fromCache !== undefined) {
return fromCache
}
const result = hostCopy.getParsedCommandLine(fileName)
if (result !== undefined) {
// Don't cache undefined results even if they could be cached
// theoretically. The big performance gains from this cache come from
// caching non-undefined results.
cache.parsedCommandLines.set(fileName, result)
}
return result
}
host.getSourceFile = (
fileName,
languageVersion,
onError,
shouldCreateNewSourceFile
) => {
const fromCache = cache.sources.get(fileName)
if (fromCache !== undefined) {
const [sourceFile, cachedLanguageVersion] = fromCache
if (isSameLanguageVersion(languageVersion, cachedLanguageVersion)) {
return sourceFile
}
}
const result = hostCopy.getSourceFile(
fileName,
languageVersion,
onError,
shouldCreateNewSourceFile
)
if (result !== undefined) {
// Don't cache undefined results even if they could be cached
// theoretically. The big performance gains from this cache come from
// caching non-undefined results.
cache.sources.set(fileName, [result, languageVersion])
}
return result
}
return host
}

export class ProjectIndexer {
private options: ProjectOptions
private program: ts.Program
Expand All @@ -20,10 +76,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)
}
Expand All @@ -47,24 +105,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
Expand Down Expand Up @@ -102,7 +160,7 @@ export class ProjectIndexer {
)
}
}
jobs.terminate()
jobs?.terminate()
const elapsed = Date.now() - startTimestamp
if (!this.options.progressBar && lastWrite > startTimestamp) {
process.stdout.write('\n')
Expand All @@ -113,10 +171,24 @@ export class ProjectIndexer {
}
}

function writableNoopStream(): WritableStream {
return new WritableStream({
write(_unused1, _unused2, callback) {
setImmediate(callback)
},
})
function isSameLanguageVersion(
a: ts.ScriptTarget | ts.CreateSourceFileOptions,
b: ts.ScriptTarget | ts.CreateSourceFileOptions
): boolean {
if (typeof a === 'number' && typeof b === 'number') {
return a === b
}
if (typeof a === 'number' || typeof b === 'number') {
// Different shape: one is ts.ScriptTarget, the other is
// ts.CreateSourceFileOptions
return false
}
return (
a.languageVersion === b.languageVersion &&
a.impliedNodeFormat === b.impliedNodeFormat
// Ignore setExternalModuleIndicator even if that increases the risk of a
// false positive. A local experiment revealed that we never get a cache hit
// if we compare setExternalModuleIndicator since it's function with a
// unique reference on every `CompilerHost.getSourceFile` callback.
)
}
1 change: 1 addition & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ for (const snapshotDirectory of snapshotDirectories) {
yarnBerryWorkspaces: false,
progressBar: false,
indexedProjects: new Set(),
globalCaches: true,
})
if (inferTsconfig) {
fs.rmSync(tsconfigJsonPath)
Expand Down
39 changes: 26 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as ts from 'typescript'
import packageJson from '../package.json'

import {
GlobalCache,
mainCommand,
MultiProjectOptions,
ProjectOptions,
Expand Down Expand Up @@ -48,6 +49,11 @@ export function indexCommand(
documentCount += index.documents.length
fs.writeSync(output, index.serializeBinary())
}

const cache: GlobalCache = {
sources: new Map(),
parsedCommandLines: new Map(),
}
try {
writeIndex(
new lsiftyped.Index({
Expand All @@ -67,12 +73,15 @@ export function indexCommand(
// they can have dependencies.
for (const projectRoot of projects) {
const projectDisplayName = projectRoot === '.' ? options.cwd : projectRoot
indexSingleProject({
...options,
projectRoot,
projectDisplayName,
writeIndex,
})
indexSingleProject(
{
...options,
projectRoot,
projectDisplayName,
writeIndex,
},
cache
)
}
} finally {
fs.close(output)
Expand All @@ -96,10 +105,11 @@ function makeAbsolutePath(cwd: string, relativeOrAbsolutePath: string): string {
return path.resolve(cwd, relativeOrAbsolutePath)
}

function indexSingleProject(options: ProjectOptions): void {
function indexSingleProject(options: ProjectOptions, cache: GlobalCache): void {
if (options.indexedProjects.has(options.projectRoot)) {
return
}

options.indexedProjects.add(options.projectRoot)
let config = ts.parseCommandLine(
['-p', options.projectRoot],
Expand All @@ -125,15 +135,18 @@ function indexSingleProject(options: ProjectOptions): void {
}

for (const projectReference of config.projectReferences || []) {
indexSingleProject({
...options,
projectRoot: projectReference.path,
projectDisplayName: projectReference.path,
})
indexSingleProject(
{
...options,
projectRoot: projectReference.path,
projectDisplayName: projectReference.path,
},
cache
)
}

if (config.fileNames.length > 0) {
new ProjectIndexer(config, options).index()
new ProjectIndexer(config, options, cache).index()
}
}

Expand Down