Skip to content

Commit d5925cc

Browse files
committed
Optimize external source maps without full cache
1 parent a0d457e commit d5925cc

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/compiler/emitter.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ namespace ts {
862862
let sourceMapGenerator: SourceMapGenerator | undefined;
863863
let sourceMapSource: SourceMapSource;
864864
let sourceMapSourceIndex = -1;
865+
let mostRecentlyAddedSourceMapSource: SourceMapSource;
866+
let mostRecentlyAddedSourceMapSourceIndex = -1;
865867

866868
// Comments
867869
let containerPos = -1;
@@ -5337,9 +5339,10 @@ namespace ts {
53375339
function emitSourcePos(source: SourceMapSource, pos: number) {
53385340
if (source !== sourceMapSource) {
53395341
const savedSourceMapSource = sourceMapSource;
5342+
const savedSourceMapSourceIndex = sourceMapSourceIndex;
53405343
setSourceMapSource(source);
53415344
emitPos(pos);
5342-
setSourceMapSource(savedSourceMapSource);
5345+
resetSourceMapSource(savedSourceMapSource, savedSourceMapSourceIndex);
53435346
}
53445347
else {
53455348
emitPos(pos);
@@ -5386,6 +5389,13 @@ namespace ts {
53865389

53875390
sourceMapSource = source;
53885391

5392+
if (source === mostRecentlyAddedSourceMapSource) {
5393+
// Fast path for when the new source map is the most recently added, in which case
5394+
// we use its captured index without going through the source map generator.
5395+
sourceMapSourceIndex = mostRecentlyAddedSourceMapSourceIndex;
5396+
return;
5397+
}
5398+
53895399
if (isJsonSourceMapSource(source)) {
53905400
return;
53915401
}
@@ -5394,6 +5404,14 @@ namespace ts {
53945404
if (printerOptions.inlineSources) {
53955405
sourceMapGenerator!.setSourceContent(sourceMapSourceIndex, source.text);
53965406
}
5407+
5408+
mostRecentlyAddedSourceMapSource = source;
5409+
mostRecentlyAddedSourceMapSourceIndex = sourceMapSourceIndex;
5410+
}
5411+
5412+
function resetSourceMapSource(source: SourceMapSource, sourceIndex: number) {
5413+
sourceMapSource = source;
5414+
sourceMapSourceIndex = sourceIndex;
53975415
}
53985416

53995417
function isJsonSourceMapSource(sourceFile: SourceMapSource) {

src/compiler/sourcemap.ts

-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace ts {
1313
const rawSources: string[] = [];
1414
const sources: string[] = [];
1515
const sourceToSourceIndexMap = new Map<string, number>();
16-
const fileNameToSourceIndexMap = new Map<string, number>();
1716
let sourcesContent: (string | null)[] | undefined;
1817

1918
const names: string[] = [];
@@ -52,10 +51,6 @@ namespace ts {
5251

5352
function addSource(fileName: string) {
5453
enter();
55-
const sourceIndexByFileName = fileNameToSourceIndexMap.get(fileName);
56-
if (sourceIndexByFileName !== undefined) {
57-
return sourceIndexByFileName;
58-
}
5954
const source = getRelativePathToDirectoryOrUrl(sourcesDirectoryPath,
6055
fileName,
6156
host.getCurrentDirectory(),
@@ -69,7 +64,6 @@ namespace ts {
6964
rawSources.push(fileName);
7065
sourceToSourceIndexMap.set(source, sourceIndex);
7166
}
72-
fileNameToSourceIndexMap.set(fileName, sourceIndex);
7367
exit();
7468
return sourceIndex;
7569
}

0 commit comments

Comments
 (0)