Skip to content

Commit dc00321

Browse files
legendecasaduh95
authored andcommitted
lib: optimize prepareStackTrace on builtin frames
Only invalidates source map lookup cache when a new source map is found. This improves when user codes interleave with builtin functions, like `array.map`. PR-URL: #56299 Refs: #56296 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent df06524 commit dc00321

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

benchmark/fixtures/simple-error-stack.js

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/fixtures/simple-error-stack.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
66

77
function simpleErrorStack() {
8-
try {
9-
(lorem as any).BANG();
10-
} catch (e) {
11-
return e.stack;
12-
}
8+
[1].map(() => {
9+
try {
10+
(lorem as any).BANG();
11+
} catch (e) {
12+
return e.stack;
13+
}
14+
})
1315
}
1416

1517
export {

lib/internal/source_map/prepare_stack_trace.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ function prepareStackTraceWithSourceMaps(error, trace) {
5353
const sm = fileName === lastFileName ?
5454
lastSourceMap :
5555
findSourceMap(fileName);
56-
lastSourceMap = sm;
57-
lastFileName = fileName;
56+
// Only when a source map is found, cache it for the next iteration.
57+
// This is a performance optimization to avoid interleaving with JS builtin function
58+
// invalidating the cache.
59+
// - at myFunc (file:///path/to/file.js:1:2)
60+
// - at Array.map (<anonymous>)
61+
// - at myFunc (file:///path/to/file.js:3:4)
5862
if (sm) {
63+
lastSourceMap = sm;
64+
lastFileName = fileName;
5965
return `${kStackLineAt}${serializeJSStackFrame(sm, callSite, trace[i + 1])}`;
6066
}
6167
} catch (err) {

lib/internal/source_map/source_map_cache.js

+5
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ function findSourceMap(sourceURL) {
347347
return undefined;
348348
}
349349

350+
// No source maps for builtin modules.
351+
if (sourceURL.startsWith('node:')) {
352+
return undefined;
353+
}
354+
350355
SourceMap ??= require('internal/source_map/source_map').SourceMap;
351356
try {
352357
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {

0 commit comments

Comments
 (0)