From f74d2bcc91dea50d3b301940b8adaca734487aeb Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 12 Apr 2023 11:17:22 +0000 Subject: [PATCH 1/2] feat(core): Cache processed stacks for debug IDs --- packages/core/src/utils/prepareEvent.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/core/src/utils/prepareEvent.ts b/packages/core/src/utils/prepareEvent.ts index f51b4afe4212..9ffbc4cc4a35 100644 --- a/packages/core/src/utils/prepareEvent.ts +++ b/packages/core/src/utils/prepareEvent.ts @@ -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'; @@ -114,6 +114,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void { } } +const debugIdStackParserCache = new Map>(); + /** * Applies debug metadata images to the event in order to apply source maps by looking up their debug ID. */ @@ -124,9 +126,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void return; } + let debugIdStackFramesCache: Map; + const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); + if (cachedDebugIdStackFrameCache) { + debugIdStackFramesCache = cachedDebugIdStackFrameCache; + } else { + debugIdStackFramesCache = new Map(); + debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); + } + // Build a map of filename -> debug_id const filenameDebugIdMap = Object.keys(debugIdMap).reduce>((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) { From 443cc1b7357325c5d50f3d43f66410879f30dd79 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 12 Apr 2023 13:07:29 +0000 Subject: [PATCH 2/2] Use weak map --- packages/core/src/utils/prepareEvent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/utils/prepareEvent.ts b/packages/core/src/utils/prepareEvent.ts index 9ffbc4cc4a35..71bbe004908b 100644 --- a/packages/core/src/utils/prepareEvent.ts +++ b/packages/core/src/utils/prepareEvent.ts @@ -114,7 +114,7 @@ function applyClientOptions(event: Event, options: ClientOptions): void { } } -const debugIdStackParserCache = new Map>(); +const debugIdStackParserCache = new WeakMap>(); /** * Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.