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

perf(sourcemap): lazy compute decoded mappings #5075

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import type {
} from './rollup/types';
import { EMPTY_OBJECT } from './utils/blank';
import { BuildPhase } from './utils/buildPhase';
import { decodedSourcemap, resetSourcemapCache } from './utils/decodedSourcemap';
import { getId } from './utils/getId';
import { getNewSet, getOrCreate } from './utils/getOrCreate';
import { getOriginalLocation } from './utils/getOriginalLocation';
Expand Down Expand Up @@ -817,8 +818,18 @@ export default class Module {

this.info.code = code;
this.originalCode = originalCode;
this.originalSourcemap = originalSourcemap;
this.sourcemapChain = sourcemapChain;

// We need to call decodedSourcemap on the input in case they were hydrated from json in the cache and don't
// have the lazy evaluation cache configured. Right now this isn't enforced by the type system because the
// RollupCache stores `ExistingDecodedSourcemap` instead of `ExistingRawSourcemap`
this.originalSourcemap = decodedSourcemap(originalSourcemap);
this.sourcemapChain = sourcemapChain.map(mapOrMissing =>
mapOrMissing.missing ? mapOrMissing : decodedSourcemap(mapOrMissing)
);

// If coming from cache and this value is already fully decoded, we want to re-encode here to save memory.
resetSourcemapCache(this.originalSourcemap, this.sourcemapChain);

if (transformFiles) {
this.transformFiles = transformFiles;
}
Expand Down
5 changes: 2 additions & 3 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type SourceMapSegment =

export interface ExistingDecodedSourceMap {
file?: string;
mappings: SourceMapSegment[][];
readonly mappings: SourceMapSegment[][];
names: string[];
sourceRoot?: string;
sources: string[];
Expand All @@ -74,11 +74,10 @@ export interface ExistingRawSourceMap {

export type DecodedSourceMapOrMissing =
| {
mappings?: never;
missing: true;
plugin: string;
}
| ExistingDecodedSourceMap;
| (ExistingDecodedSourceMap & { missing?: false });
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved

export interface SourceMap {
file: string;
Expand Down
9 changes: 7 additions & 2 deletions src/utils/collapseSourcemaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
LogHandler,
SourceMapSegment
} from '../rollup/types';
import { decodedSourcemap, resetSourcemapCache } from './decodedSourcemap';
import { LOGLEVEL_WARN } from './logging';
import { error, logConflictingSourcemapSources, logSourcemapBroken } from './logs';
import { basename, dirname, relative, resolve } from './path';
Expand Down Expand Up @@ -150,7 +151,7 @@ class Link {

function getLinkMap(log: LogHandler) {
return function linkMap(source: Source | Link, map: DecodedSourceMapOrMissing): Link {
if (map.mappings) {
if (!map.missing) {
return new Link(map, [source]);
}

Expand Down Expand Up @@ -224,6 +225,10 @@ export function collapseSourcemaps(

sourcesContent = (excludeContent ? null : sourcesContent) as string[];

for (const module of modules) {
resetSourcemapCache(module.originalSourcemap, module.sourcemapChain);
}

return new SourceMap({ file, mappings, names, sources, sourcesContent });
}

Expand All @@ -246,5 +251,5 @@ export function collapseSourcemap(
getLinkMap(log)
) as Link;
const map = source.traceMappings();
return { version: 3, ...map };
return decodedSourcemap({ version: 3, ...map });
}
77 changes: 74 additions & 3 deletions src/utils/decodedSourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
import { decode } from '@jridgewell/sourcemap-codec';
import { decode, encode } from '@jridgewell/sourcemap-codec';
import type {
DecodedSourceMapOrMissing,
ExistingDecodedSourceMap,
ExistingRawSourceMap,
SourceMapInput
} from '../rollup/types';

type Input = SourceMapInput | ExistingDecodedSourceMap | undefined;

interface CachedSourcemapData {
encodedMappings: string | undefined;
decodedMappings: ExistingDecodedSourceMap['mappings'] | undefined;
}

const sourceMapCache = new WeakMap<ExistingDecodedSourceMap, CachedSourcemapData>();
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved

/**
* This clears the decoded array and falls back to the encoded string form.
* Sourcemap mappings arrays can be very large and holding on to them for longer
* than is necessary leads to poor heap utilization.
*/
function resetCacheToEncoded(cache: CachedSourcemapData) {
if (cache.encodedMappings === undefined && cache.decodedMappings) {
cache.encodedMappings = encode(cache.decodedMappings);
}
cache.decodedMappings = undefined;
}

export function resetSourcemapCache(
map: ExistingDecodedSourceMap | null,
sourcemapChain?: DecodedSourceMapOrMissing[]
) {
if (map) {
const cache = sourceMapCache.get(map);
if (cache) {
resetCacheToEncoded(cache);
}
}

if (!sourcemapChain) {
return;
}

for (const map of sourcemapChain) {
if (map.missing) continue;

resetSourcemapCache(map);
}
}

export function decodedSourcemap(map: null | undefined): null;
export function decodedSourcemap(map: Exclude<Input, null | undefined>): ExistingDecodedSourceMap;
export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null;
export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null {
if (!map) return null;

Expand All @@ -22,7 +67,33 @@ export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null {
};
}

const mappings = typeof map.mappings === 'string' ? decode(map.mappings) : map.mappings;
const originalMappings = map.mappings;
const isAlreadyDecoded = typeof originalMappings !== 'string';
const cache = {
decodedMappings: isAlreadyDecoded ? originalMappings : undefined,
encodedMappings: isAlreadyDecoded ? undefined : originalMappings
};

const decodedMap = {
...(map as ExistingRawSourceMap | ExistingDecodedSourceMap),
// By moving mappings behind an accessor, we can avoid unneeded computation for cases
// where the mappings field is never actually accessed. This appears to greatly reduce
// the overhead of sourcemap decoding in terms of both compute time and memory usage.
get mappings() {
if (cache.decodedMappings) {
return cache.decodedMappings;
}
// If decodedMappings doesn't exist then encodedMappings should.
// The only scenario where cache.encodedMappings should be undefined is if the map
// this was constructed from was already decoded, or if mappings was set to a new
// decoded string. In either case, this line shouldn't get hit.
cache.decodedMappings = decode(cache.encodedMappings!);
cache.encodedMappings = undefined;
return cache.decodedMappings;
}
};

sourceMapCache.set(decodedMap, cache);

return { ...(map as ExistingRawSourceMap | ExistingDecodedSourceMap), mappings };
return decodedMap;
}
2 changes: 1 addition & 1 deletion src/utils/getOriginalLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function getOriginalLocation(
location: { column: number; line: number }
): { column: number; line: number } {
const filteredSourcemapChain = sourcemapChain.filter(
(sourcemap): sourcemap is ExistingDecodedSourceMap => !!sourcemap.mappings
(sourcemap): sourcemap is ExistingDecodedSourceMap => !sourcemap.missing
);
traceSourcemap: while (filteredSourcemapChain.length > 0) {
const sourcemap = filteredSourcemapChain.pop()!;
Expand Down