Skip to content

Optimize source mapping into external source map sources #40055

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

Merged
Merged
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
6 changes: 6 additions & 0 deletions src/compiler/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace ts {
const rawSources: string[] = [];
const sources: string[] = [];
const sourceToSourceIndexMap = new Map<string, number>();
const fileNameToSourceIndexMap = new Map<string, number>();
let sourcesContent: (string | null)[] | undefined;

const names: string[] = [];
Expand Down Expand Up @@ -51,6 +52,10 @@ namespace ts {

function addSource(fileName: string) {
enter();
const sourceIndexByFileName = fileNameToSourceIndexMap.get(fileName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation note: this could alternatively be done without a full cache, using just a single MRU fileName/sourceIndex. That does however require that the second call to setSourceMapSource in emitSourcePos has to take an additional parameter to not even call addSource (which is safe as reverting to the prior source map source is known to have been added) as otherwise 2 MRU items are needed:

function emitSourcePos(source: SourceMapSource, pos: number) {
if (source !== sourceMapSource) {
const savedSourceMapSource = sourceMapSource;
setSourceMapSource(source);
emitPos(pos);
setSourceMapSource(savedSourceMapSource);
}
else {
emitPos(pos);
}
}

This alternative would also mitigate the issue (I verified that) as it's typical for a single external source map source to be applied on nodes without interference from nodes that have a different external source map source, so a single MRU cache entry is sufficient.

if (sourceIndexByFileName !== undefined) {
return sourceIndexByFileName;
}
const source = getRelativePathToDirectoryOrUrl(sourcesDirectoryPath,
fileName,
host.getCurrentDirectory(),
Expand All @@ -64,6 +69,7 @@ namespace ts {
rawSources.push(fileName);
sourceToSourceIndexMap.set(source, sourceIndex);
}
fileNameToSourceIndexMap.set(fileName, sourceIndex);
exit();
return sourceIndex;
}
Expand Down