Skip to content

Commit

Permalink
lib: cache parsed source maps to reduce memory footprint
Browse files Browse the repository at this point in the history
This also improves performance to map the stack trace when the
`Error.stack` is accessed.

PR-URL: #46225
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
legendecas authored Jan 22, 2023
1 parent 312393f commit 651c09c
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const {

function ObjectGetValueSafe(obj, key) {
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
if (desc === undefined) {
return undefined;
}
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
}

Expand Down Expand Up @@ -141,6 +144,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
const url = data ? null : sourceMapURL;
if (cjsModuleInstance) {
getCjsSourceMapCache().set(cjsModuleInstance, {
__proto__: null,
filename,
lineLengths: lineLengths(content),
data,
Expand All @@ -149,6 +153,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
});
} else if (isGeneratedSource) {
const entry = {
__proto__: null,
lineLengths: lineLengths(content),
data,
url,
Expand All @@ -162,6 +167,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
// If there is no cjsModuleInstance and is not generated source assume we are in a
// "modules/esm" context.
const entry = {
__proto__: null,
lineLengths: lineLengths(content),
data,
url,
Expand Down Expand Up @@ -296,6 +302,7 @@ function sourceMapCacheToObject() {
function appendCJSCache(obj) {
for (const value of getCjsSourceMapCache()) {
obj[ObjectGetValueSafe(value, 'filename')] = {
__proto__: null,
lineLengths: ObjectGetValueSafe(value, 'lineLengths'),
data: ObjectGetValueSafe(value, 'data'),
url: ObjectGetValueSafe(value, 'url')
Expand All @@ -310,22 +317,25 @@ function findSourceMap(sourceURL) {
if (!SourceMap) {
SourceMap = require('internal/source_map/source_map').SourceMap;
}
let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (sourceMap === undefined) {
let entry = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (entry === undefined) {
for (const value of getCjsSourceMapCache()) {
const filename = ObjectGetValueSafe(value, 'filename');
const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL');
if (sourceURL === filename || sourceURL === cachedSourceURL) {
sourceMap = {
data: ObjectGetValueSafe(value, 'data')
};
entry = value;
}
}
}
if (sourceMap && sourceMap.data) {
return new SourceMap(sourceMap.data);
if (entry === undefined) {
return undefined;
}
let sourceMap = ObjectGetValueSafe(entry, 'sourceMap');
if (sourceMap === undefined) {
sourceMap = new SourceMap(ObjectGetValueSafe(entry, 'data'));
entry.sourceMap = sourceMap;
}
return undefined;
return sourceMap;
}

module.exports = {
Expand Down

0 comments on commit 651c09c

Please sign in to comment.