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

lib: cache parsed source maps to reduce memory footprint #46225

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
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