Skip to content

Commit 25d30ae

Browse files
committed
fix: use map instead of queue
1 parent f0be011 commit 25d30ae

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

packages/browser/src/profiling/hubextensions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
JSSelfProfilerConstructor,
1111
ProcessedJSSelfProfile,
1212
} from './jsSelfProfiling';
13-
import { addToProfileQueue, isValidSampleRate } from './utils';
13+
import { addProfileToMap,isValidSampleRate } from './utils';
1414

1515
export const MAX_PROFILE_DURATION_MS = 30_000;
1616
// Keep a flag value to avoid re-initializing the profiler constructor. If it fails
@@ -222,7 +222,8 @@ export function wrapTransactionWithProfiling(transaction: Transaction): Transact
222222
return null;
223223
}
224224

225-
addToProfileQueue({ ...p, profile_id: profileId });
225+
226+
addProfileToMap({...p, profile_id: profileId });
226227
return null;
227228
})
228229
.catch(error => {

packages/browser/src/profiling/integration.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
addProfilesToEnvelope,
1010
createProfilingEvent,
1111
findProfiledTransactionsFromEnvelope,
12-
PROFILE_QUEUE,
12+
PROFILE_MAP,
1313
} from './utils';
1414

1515
/**
@@ -39,8 +39,7 @@ export class BrowserProfilingIntegration implements Integration {
3939

4040
client.on('beforeEnvelope', (envelope): void => {
4141
// if not profiles are in queue, there is nothing to add to the envelope.
42-
43-
if (!PROFILE_QUEUE.length) {
42+
if (!PROFILE_MAP.size) {
4443
return;
4544
}
4645

@@ -56,7 +55,7 @@ export class BrowserProfilingIntegration implements Integration {
5655
profiledTransaction &&
5756
profiledTransaction.contexts &&
5857
profiledTransaction.contexts['profile'] &&
59-
profiledTransaction.contexts['profile']['profile_id'];
58+
profiledTransaction.contexts['profile']['profile_id'] as string
6059

6160
if (!profile_id) {
6261
throw new TypeError('[Profiling] cannot find profile for a transaction without a profile context');
@@ -67,23 +66,15 @@ export class BrowserProfilingIntegration implements Integration {
6766
delete profiledTransaction.contexts.profile;
6867
}
6968

70-
// We need to find both a profile and a transaction event for the same profile_id.
71-
const profileIndex = PROFILE_QUEUE.findIndex(p => p.profile_id === profile_id);
72-
if (profileIndex === -1) {
73-
__DEBUG_BUILD__ && logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`);
74-
continue;
75-
}
76-
77-
const cpuProfile = PROFILE_QUEUE[profileIndex];
78-
if (!cpuProfile) {
69+
const profile = PROFILE_MAP.get(profile_id);
70+
if (!profile) {
7971
__DEBUG_BUILD__ && logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`);
8072
continue;
8173
}
8274

83-
// Remove the profile from the queue.
84-
PROFILE_QUEUE.splice(profileIndex, 1);
85-
const profileEvent = createProfilingEvent(cpuProfile, profiledTransaction as ProfiledEvent);
86-
75+
PROFILE_MAP.delete(profile_id);
76+
const profileEvent = createProfilingEvent(profile, profiledTransaction as ProfiledEvent);
77+
8778
if (profileEvent) {
8879
profilesToAddToEnvelope.push(profileEvent);
8980
}

packages/browser/src/profiling/utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,16 @@ export function createProfilingEvent(profile: ProcessedJSSelfProfile, event: Pro
451451
return createProfilePayload(event, profile);
452452
}
453453

454-
const MAX_PROFILE_QUEUE_SIZE = 50;
455-
export const PROFILE_QUEUE: ProcessedJSSelfProfile[] = [];
454+
455+
export const PROFILE_MAP: Map<string, ProcessedJSSelfProfile> = new Map();
456456
/**
457-
* Adds the profile to the queue of profiles to be sent
457+
*
458458
*/
459-
export function addToProfileQueue(profile: ProcessedJSSelfProfile): void {
460-
PROFILE_QUEUE.push(profile);
459+
export function addProfileToMap(profile: ProcessedJSSelfProfile): void{
460+
PROFILE_MAP.set(profile.profile_id, profile);
461461

462-
// We only want to keep the last n profiles in the queue.
463-
if (PROFILE_QUEUE.length > MAX_PROFILE_QUEUE_SIZE) {
464-
PROFILE_QUEUE.shift();
462+
if(PROFILE_MAP.size > 30){
463+
const last: string = [...PROFILE_MAP.keys()].pop() as string;
464+
PROFILE_MAP.delete(last);
465465
}
466-
}
466+
}

0 commit comments

Comments
 (0)