Skip to content

Commit 970e108

Browse files
author
Luca Forstner
authored
feat(core): Cache processed stacks for debug IDs (#7825)
1 parent 753a197 commit 970e108

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/core/src/utils/prepareEvent.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ClientOptions, Event, EventHint, StackParser } from '@sentry/types';
1+
import type { ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types';
22
import { dateTimestampInSeconds, GLOBAL_OBJ, normalize, resolvedSyncPromise, truncate, uuid4 } from '@sentry/utils';
33

44
import { DEFAULT_ENVIRONMENT } from '../constants';
@@ -118,6 +118,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void {
118118
}
119119
}
120120

121+
const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[]>>();
122+
121123
/**
122124
* Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.
123125
*/
@@ -128,9 +130,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void
128130
return;
129131
}
130132

133+
let debugIdStackFramesCache: Map<string, StackFrame[]>;
134+
const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser);
135+
if (cachedDebugIdStackFrameCache) {
136+
debugIdStackFramesCache = cachedDebugIdStackFrameCache;
137+
} else {
138+
debugIdStackFramesCache = new Map<string, StackFrame[]>();
139+
debugIdStackParserCache.set(stackParser, debugIdStackFramesCache);
140+
}
141+
131142
// Build a map of filename -> debug_id
132143
const filenameDebugIdMap = Object.keys(debugIdMap).reduce<Record<string, string>>((acc, debugIdStackTrace) => {
133-
const parsedStack = stackParser(debugIdStackTrace);
144+
let parsedStack: StackFrame[];
145+
const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace);
146+
if (cachedParsedStack) {
147+
parsedStack = cachedParsedStack;
148+
} else {
149+
parsedStack = stackParser(debugIdStackTrace);
150+
debugIdStackFramesCache.set(debugIdStackTrace, parsedStack);
151+
}
152+
134153
for (let i = parsedStack.length - 1; i >= 0; i--) {
135154
const stackFrame = parsedStack[i];
136155
if (stackFrame.filename) {

0 commit comments

Comments
 (0)