Skip to content

Commit de65590

Browse files
authored
feat(core): Further optimize debug ID parsing (#14365)
1 parent 09a31d1 commit de65590

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

packages/core/src/utils-hoist/debug-ids.ts

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import type { DebugImage, StackFrame, StackParser } from '@sentry/types';
1+
import type { DebugImage, StackParser } from '@sentry/types';
22
import { GLOBAL_OBJ } from './worldwide';
33

4-
const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[]>>();
4+
type StackString = string;
5+
type CachedResult = [string, string];
6+
7+
let parsedStackResults: Record<StackString, CachedResult> | undefined;
8+
let lastKeysCount: number | undefined;
9+
let cachedFilenameDebugIds: Record<string, string> | undefined;
510

611
/**
712
* Returns a map of filenames to debug identifiers.
@@ -12,38 +17,46 @@ export function getFilenameToDebugIdMap(stackParser: StackParser): Record<string
1217
return {};
1318
}
1419

15-
let debugIdStackFramesCache: Map<string, StackFrame[]>;
16-
const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser);
17-
if (cachedDebugIdStackFrameCache) {
18-
debugIdStackFramesCache = cachedDebugIdStackFrameCache;
19-
} else {
20-
debugIdStackFramesCache = new Map<string, StackFrame[]>();
21-
debugIdStackParserCache.set(stackParser, debugIdStackFramesCache);
20+
const debugIdKeys = Object.keys(debugIdMap);
21+
22+
// If the count of registered globals hasn't changed since the last call, we
23+
// can just return the cached result.
24+
if (cachedFilenameDebugIds && debugIdKeys.length === lastKeysCount) {
25+
return cachedFilenameDebugIds;
2226
}
2327

28+
lastKeysCount = debugIdKeys.length;
29+
2430
// Build a map of filename -> debug_id.
25-
return Object.keys(debugIdMap).reduce<Record<string, string>>((acc, debugIdStackTrace) => {
26-
let parsedStack: StackFrame[];
31+
cachedFilenameDebugIds = debugIdKeys.reduce<Record<string, string>>((acc, stackKey) => {
32+
if (!parsedStackResults) {
33+
parsedStackResults = {};
34+
}
35+
36+
const result = parsedStackResults[stackKey];
2737

28-
const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace);
29-
if (cachedParsedStack) {
30-
parsedStack = cachedParsedStack;
38+
if (result) {
39+
acc[result[0]] = result[1];
3140
} else {
32-
parsedStack = stackParser(debugIdStackTrace);
33-
debugIdStackFramesCache.set(debugIdStackTrace, parsedStack);
34-
}
41+
const parsedStack = stackParser(stackKey);
3542

36-
for (let i = parsedStack.length - 1; i >= 0; i--) {
37-
const stackFrame = parsedStack[i];
38-
const file = stackFrame && stackFrame.filename;
43+
for (let i = parsedStack.length - 1; i >= 0; i--) {
44+
const stackFrame = parsedStack[i];
45+
const filename = stackFrame && stackFrame.filename;
46+
const debugId = debugIdMap[stackKey];
3947

40-
if (stackFrame && file) {
41-
acc[file] = debugIdMap[debugIdStackTrace] as string;
42-
break;
48+
if (filename && debugId) {
49+
acc[filename] = debugId;
50+
parsedStackResults[stackKey] = [filename, debugId];
51+
break;
52+
}
4353
}
4454
}
55+
4556
return acc;
4657
}, {});
58+
59+
return cachedFilenameDebugIds;
4760
}
4861

4962
/**
@@ -55,6 +68,10 @@ export function getDebugImagesForResources(
5568
): DebugImage[] {
5669
const filenameDebugIdMap = getFilenameToDebugIdMap(stackParser);
5770

71+
if (!filenameDebugIdMap) {
72+
return [];
73+
}
74+
5875
const images: DebugImage[] = [];
5976
for (const path of resource_paths) {
6077
if (path && filenameDebugIdMap[path]) {

packages/core/src/utils/prepareEvent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export function applyDebugIds(event: Event, stackParser: StackParser): void {
179179
event!.exception!.values!.forEach(exception => {
180180
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
181181
exception.stacktrace!.frames!.forEach(frame => {
182-
if (frame.filename) {
182+
if (filenameDebugIdMap && frame.filename) {
183183
frame.debug_id = filenameDebugIdMap[frame.filename];
184184
}
185185
});

packages/node/src/integrations/anr/index.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as diagnosticsChannel from 'node:diagnostics_channel';
21
import { Worker } from 'node:worker_threads';
32
import { defineIntegration, getCurrentScope, getGlobalScope, getIsolationScope, mergeScopeData } from '@sentry/core';
43
import { GLOBAL_OBJ, getFilenameToDebugIdMap, logger } from '@sentry/core';
@@ -101,13 +100,6 @@ type AnrReturn = (options?: Partial<AnrIntegrationOptions>) => Integration & Anr
101100

102101
export const anrIntegration = defineIntegration(_anrIntegration) as AnrReturn;
103102

104-
function onModuleLoad(callback: () => void): void {
105-
// eslint-disable-next-line deprecation/deprecation
106-
diagnosticsChannel.channel('module.require.end').subscribe(() => callback());
107-
// eslint-disable-next-line deprecation/deprecation
108-
diagnosticsChannel.channel('module.import.asyncEnd').subscribe(() => callback());
109-
}
110-
111103
/**
112104
* Starts the ANR worker thread
113105
*
@@ -161,12 +153,6 @@ async function _startWorker(
161153
}
162154
}
163155

164-
let debugImages: Record<string, string> = getFilenameToDebugIdMap(initOptions.stackParser);
165-
166-
onModuleLoad(() => {
167-
debugImages = getFilenameToDebugIdMap(initOptions.stackParser);
168-
});
169-
170156
const worker = new Worker(new URL(`data:application/javascript;base64,${base64WorkerScript}`), {
171157
workerData: options,
172158
// We don't want any Node args to be passed to the worker
@@ -185,7 +171,7 @@ async function _startWorker(
185171
// serialized without making it a SerializedSession
186172
const session = currentSession ? { ...currentSession, toJSON: undefined } : undefined;
187173
// message the worker to tell it the main event loop is still running
188-
worker.postMessage({ session, debugImages });
174+
worker.postMessage({ session, debugImages: getFilenameToDebugIdMap(initOptions.stackParser) });
189175
} catch (_) {
190176
//
191177
}

0 commit comments

Comments
 (0)