Optimize sourcemap application more (#25425)

* Optimize sourcemap application more

* Remove test-only memory hog sourceMapDecodedMappings field

* Update for style, remove unused function that triggers warnings in node 10

* Avoid all raw buffer constructor calls

* Small TDZ fix
This commit is contained in:
Wesley Wigham
2018-07-05 15:12:10 -07:00
committed by GitHub
parent 065e695a28
commit 5b92678285
11 changed files with 36 additions and 49 deletions

View File

@@ -156,7 +156,6 @@ namespace ts {
sourceMapNames: [],
sourceMapMappings: "",
sourceMapSourcesContent: compilerOptions.inlineSources ? [] : undefined,
sourceMapDecodedMappings: []
};
// Normalize source root and make sure it has trailing "/" so that it can be used to combine paths with the
@@ -299,7 +298,6 @@ namespace ts {
}
lastEncodedSourceMapSpan = lastRecordedSourceMapSpan;
sourceMapData.sourceMapDecodedMappings.push(lastEncodedSourceMapSpan);
}
/**
@@ -393,24 +391,29 @@ namespace ts {
const sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir;
const resolvedPathCache = createMap<string>();
sourcemaps.calculateDecodedMappings(originalMap, (raw): void => {
const absolutePathCache = createMap<string>();
const sourcemapIterator = sourcemaps.decodeMappings(originalMap);
for (let { value: raw, done } = sourcemapIterator.next(); !done; { value: raw, done } = sourcemapIterator.next()) {
const pathCacheKey = "" + raw.sourceIndex;
// Apply offsets to each position and fixup source entries
const rawPath = originalMap.sources[raw.sourceIndex];
const relativePath = originalMap.sourceRoot ? combinePaths(originalMap.sourceRoot, rawPath) : rawPath;
const combinedPath = combinePaths(getDirectoryPath(node.sourceMapPath!), relativePath);
if (!resolvedPathCache.has(combinedPath)) {
resolvedPathCache.set(combinedPath, getRelativePathToDirectoryOrUrl(
if (!resolvedPathCache.has(pathCacheKey)) {
const rawPath = originalMap.sources[raw.sourceIndex];
const relativePath = originalMap.sourceRoot ? combinePaths(originalMap.sourceRoot, rawPath) : rawPath;
const combinedPath = combinePaths(getDirectoryPath(node.sourceMapPath!), relativePath);
const resolvedPath = getRelativePathToDirectoryOrUrl(
sourcesDirectoryPath,
combinedPath,
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true
));
);
resolvedPathCache.set(pathCacheKey, resolvedPath);
absolutePathCache.set(pathCacheKey, getNormalizedAbsolutePath(resolvedPath, sourcesDirectoryPath));
}
const resolvedPath = resolvedPathCache.get(combinedPath)!;
const absolutePath = getNormalizedAbsolutePath(resolvedPath, sourcesDirectoryPath);
const resolvedPath = resolvedPathCache.get(pathCacheKey)!;
const absolutePath = absolutePathCache.get(pathCacheKey)!;
// tslint:disable-next-line:no-null-keyword
setupSourceEntry(absolutePath, originalMap.sourcesContent ? originalMap.sourcesContent[raw.sourceIndex] : null); // TODO: Lookup content for inlining?
setupSourceEntry(absolutePath, originalMap.sourcesContent ? originalMap.sourcesContent[raw.sourceIndex] : null, resolvedPath); // TODO: Lookup content for inlining?
const newIndex = sourceMapData.sourceMapSources.indexOf(resolvedPath);
// Then reencode all the updated spans into the overall map
encodeLastRecordedSourceMapSpan();
@@ -420,7 +423,7 @@ namespace ts {
emittedColumn: raw.emittedLine === 0 ? (raw.emittedColumn + firstLineColumnOffset) : raw.emittedColumn,
sourceIndex: newIndex,
};
});
}
// And actually emit the text these sourcemaps are for
return emitCallback(hint, node);
}
@@ -519,17 +522,19 @@ namespace ts {
setupSourceEntry(sourceFile.fileName, sourceFile.text);
}
function setupSourceEntry(fileName: string, content: string | null) {
// Add the file to tsFilePaths
// If sourceroot option: Use the relative path corresponding to the common directory path
// otherwise source locations relative to map file location
const sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir;
function setupSourceEntry(fileName: string, content: string | null, source?: string) {
if (!source) {
// Add the file to tsFilePaths
// If sourceroot option: Use the relative path corresponding to the common directory path
// otherwise source locations relative to map file location
const sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir;
const source = getRelativePathToDirectoryOrUrl(sourcesDirectoryPath,
fileName,
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true);
source = getRelativePathToDirectoryOrUrl(sourcesDirectoryPath,
fileName,
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true);
}
sourceMapSourceIndex = sourceMapData.sourceMapSources.indexOf(source);
if (sourceMapSourceIndex === -1) {

View File

@@ -190,7 +190,7 @@ namespace ts.sourcemaps {
};
}
export function calculateDecodedMappings<T>(map: SourceMapData, processPosition: (position: RawSourceMapPosition) => T, host?: { log?(s: string): void }): T[] {
function calculateDecodedMappings<T>(map: SourceMapData, processPosition: (position: RawSourceMapPosition) => T, host?: { log?(s: string): void }): T[] {
const decoder = decodeMappings(map);
const positions = arrayFrom(decoder, processPosition);
if (decoder.error) {

View File

@@ -2844,7 +2844,6 @@ namespace ts {
inputSourceFileNames: string[]; // Input source file (which one can use on program to get the file), 1:1 mapping with the sourceMapSources list
sourceMapNames?: string[]; // Source map's names field - list of names that can be indexed in this source map
sourceMapMappings: string; // Source map's mapping field - encoded source map spans
sourceMapDecodedMappings: SourceMapSpan[]; // Raw source map spans that were encoded into the sourceMapMappings
}
/** Return code used by getEmitOutput function to indicate status of the function */