From 6d4f95e523b26a0ac16a083fa29ccf5eda24230a Mon Sep 17 00:00:00 2001 From: Karol Samborski Date: Fri, 22 Mar 2019 13:33:39 +0100 Subject: [PATCH 1/3] Cached file system operations --- src/index.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 52a2d077c..7a2732a34 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,8 +76,10 @@ export interface Options { * Track the project information. */ class MemoryCache { - fileContents = new Map() - fileVersions = new Map() + fileContents = new Map() + fileVersions = new Map() + fileChecks = new Map() + directoryChecks = new Map() constructor (public rootFileNames: string[] = []) { for (const fileName of rootFileNames) this.fileVersions.set(fileName, 1) @@ -171,6 +173,16 @@ export interface Register { getTypeInfo (code: string, fileName: string, position: number): TypeInfo } +function cached(store: Map, fun: (arg: string) => boolean): (arg: string) => boolean { + return (arg: string) => { + if (!store.has(arg)) { + store.set(arg, fun(arg)) + } + + return store.get(arg) || false + }; +} + /** * Register TypeScript compiler. */ @@ -290,11 +302,11 @@ export function register (opts: Options = {}): Register { return ts.ScriptSnapshot.fromString(contents) }, - fileExists: debugFn('fileExists', fileExists), + fileExists: cached(memoryCache.fileChecks, debugFn('fileExists', fileExists)), readFile: debugFn('readFile', readFile), readDirectory: debugFn('readDirectory', ts.sys.readDirectory), getDirectories: debugFn('getDirectories', ts.sys.getDirectories), - directoryExists: debugFn('directoryExists', ts.sys.directoryExists), + directoryExists: cached(memoryCache.directoryChecks, debugFn('directoryExists', ts.sys.directoryExists)), realpath: debugFn('realpath', ts.sys.realpath!), getNewLine: () => ts.sys.newLine, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, From ccb8949bba4d62b0f40864e257ddb38093269fd4 Mon Sep 17 00:00:00 2001 From: Karol Samborski Date: Fri, 22 Mar 2019 13:59:10 +0100 Subject: [PATCH 2/3] Fixed linter errors --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7a2732a34..42e29ab2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,9 +76,9 @@ export interface Options { * Track the project information. */ class MemoryCache { - fileContents = new Map() - fileVersions = new Map() - fileChecks = new Map() + fileContents = new Map() + fileVersions = new Map() + fileChecks = new Map() directoryChecks = new Map() constructor (public rootFileNames: string[] = []) { @@ -173,14 +173,14 @@ export interface Register { getTypeInfo (code: string, fileName: string, position: number): TypeInfo } -function cached(store: Map, fun: (arg: string) => boolean): (arg: string) => boolean { +function cached (store: Map, fun: (arg: string) => boolean): (arg: string) => boolean { return (arg: string) => { if (!store.has(arg)) { store.set(arg, fun(arg)) } return store.get(arg) || false - }; + } } /** From 42bfc9bf8a77f7245297cb45e0c0f9a8c5636839 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Sun, 14 Apr 2019 18:27:05 -0700 Subject: [PATCH 3/3] Update index.ts --- src/index.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 42e29ab2f..ba5f5c65c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,8 +78,6 @@ export interface Options { class MemoryCache { fileContents = new Map() fileVersions = new Map() - fileChecks = new Map() - directoryChecks = new Map() constructor (public rootFileNames: string[] = []) { for (const fileName of rootFileNames) this.fileVersions.set(fileName, 1) @@ -173,13 +171,15 @@ export interface Register { getTypeInfo (code: string, fileName: string, position: number): TypeInfo } -function cached (store: Map, fun: (arg: string) => boolean): (arg: string) => boolean { +function cachedLookup (fn: (arg: string) => boolean): (arg: string) => boolean { + const cache = new Map() + return (arg: string) => { - if (!store.has(arg)) { - store.set(arg, fun(arg)) + if (!cache.has(arg)) { + cache.set(arg, fn(arg)) } - return store.get(arg) || false + return cache.get(arg) || false } } @@ -302,11 +302,11 @@ export function register (opts: Options = {}): Register { return ts.ScriptSnapshot.fromString(contents) }, - fileExists: cached(memoryCache.fileChecks, debugFn('fileExists', fileExists)), + fileExists: cachedLookup(debugFn('fileExists', fileExists)), readFile: debugFn('readFile', readFile), readDirectory: debugFn('readDirectory', ts.sys.readDirectory), getDirectories: debugFn('getDirectories', ts.sys.getDirectories), - directoryExists: cached(memoryCache.directoryChecks, debugFn('directoryExists', ts.sys.directoryExists)), + directoryExists: cachedLookup(debugFn('directoryExists', ts.sys.directoryExists)), realpath: debugFn('realpath', ts.sys.realpath!), getNewLine: () => ts.sys.newLine, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,