Skip to content

Commit

Permalink
feat(core): Cache processed stacks for debug IDs (#7825)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Apr 14, 2023
1 parent 753a197 commit 970e108
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/core/src/utils/prepareEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientOptions, Event, EventHint, StackParser } from '@sentry/types';
import type { ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types';
import { dateTimestampInSeconds, GLOBAL_OBJ, normalize, resolvedSyncPromise, truncate, uuid4 } from '@sentry/utils';

import { DEFAULT_ENVIRONMENT } from '../constants';
Expand Down Expand Up @@ -118,6 +118,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void {
}
}

const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[]>>();

/**
* Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.
*/
Expand All @@ -128,9 +130,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void
return;
}

let debugIdStackFramesCache: Map<string, StackFrame[]>;
const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser);
if (cachedDebugIdStackFrameCache) {
debugIdStackFramesCache = cachedDebugIdStackFrameCache;
} else {
debugIdStackFramesCache = new Map<string, StackFrame[]>();
debugIdStackParserCache.set(stackParser, debugIdStackFramesCache);
}

// Build a map of filename -> debug_id
const filenameDebugIdMap = Object.keys(debugIdMap).reduce<Record<string, string>>((acc, debugIdStackTrace) => {
const parsedStack = stackParser(debugIdStackTrace);
let parsedStack: StackFrame[];
const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace);
if (cachedParsedStack) {
parsedStack = cachedParsedStack;
} else {
parsedStack = stackParser(debugIdStackTrace);
debugIdStackFramesCache.set(debugIdStackTrace, parsedStack);
}

for (let i = parsedStack.length - 1; i >= 0; i--) {
const stackFrame = parsedStack[i];
if (stackFrame.filename) {
Expand Down

0 comments on commit 970e108

Please sign in to comment.