Skip to content

Commit 6fce3b8

Browse files
committed
fix: preserve profile command limit semantics
1 parent 4cae176 commit 6fce3b8

3 files changed

Lines changed: 24 additions & 22 deletions

File tree

packages/agent-react-devtools/src/cli.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import { writeFileSync } from 'node:fs';
2424
import { resolve } from 'node:path';
2525
import type { IpcCommand } from './types.js';
2626

27+
function getCandidateLimit(limit: number): number {
28+
return Math.max(limit, Math.min(60, Math.max(20, limit * 3)));
29+
}
30+
2731
function usage(): string {
2832
return `Usage: devtools <command> [options]
2933
@@ -377,7 +381,12 @@ async function main(): Promise<void> {
377381

378382
if (cmd0 === 'profile' && cmd1 === 'slow') {
379383
const limit = parseNumericFlag(flags, 'limit');
380-
const resp = await sendCommand({ type: 'profile-slow', limit });
384+
const visibleLimit = limit ?? 10;
385+
const resp = await sendCommand({
386+
type: 'profile-slow',
387+
limit,
388+
candidateLimit: getCandidateLimit(visibleLimit),
389+
});
381390
if (resp.ok) {
382391
console.log(formatSlowest(resp.data as any, limit));
383392
} else {
@@ -389,7 +398,12 @@ async function main(): Promise<void> {
389398

390399
if (cmd0 === 'profile' && cmd1 === 'rerenders') {
391400
const limit = parseNumericFlag(flags, 'limit');
392-
const resp = await sendCommand({ type: 'profile-rerenders', limit });
401+
const visibleLimit = limit ?? 10;
402+
const resp = await sendCommand({
403+
type: 'profile-rerenders',
404+
limit,
405+
candidateLimit: getCandidateLimit(visibleLimit),
406+
});
393407
if (resp.ok) {
394408
console.log(formatRerenders(resp.data as any, limit));
395409
} else {

packages/agent-react-devtools/src/daemon.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ const DEFAULT_STATE_DIR = path.join(
1313
);
1414

1515
let STATE_DIR = DEFAULT_STATE_DIR;
16-
const PROFILE_READ_CANDIDATE_MULTIPLIER = 3;
17-
const PROFILE_READ_MIN_CANDIDATES = 20;
18-
const PROFILE_READ_MAX_CANDIDATES = 60;
1916
const PROFILE_READ_ENRICH_CONCURRENCY = 5;
2017
const PROFILE_READ_ENRICH_TIMEOUT_MS = 1000;
2118

@@ -98,17 +95,6 @@ async function enrichProfileMetadataOnDemand(
9895
}));
9996
}
10097

101-
function getCandidateLimit(limit?: number): number {
102-
if (limit === undefined) return PROFILE_READ_MAX_CANDIDATES;
103-
return Math.max(
104-
limit,
105-
Math.min(
106-
PROFILE_READ_MAX_CANDIDATES,
107-
Math.max(PROFILE_READ_MIN_CANDIDATES, limit * PROFILE_READ_CANDIDATE_MULTIPLIER),
108-
),
109-
);
110-
}
111-
11298
class Daemon {
11399
private ipcServer: net.Server | null = null;
114100
private bridge: DevToolsBridge;
@@ -352,18 +338,20 @@ class Daemon {
352338
}
353339

354340
case 'profile-slow': {
355-
const candidateLimit = getCandidateLimit(cmd.limit);
341+
const requestedLimit = cmd.limit;
342+
const candidateLimit = Math.max(requestedLimit ?? 10, cmd.candidateLimit ?? requestedLimit ?? 10);
356343
const candidates = this.profiler.getSlowest(this.tree, candidateLimit);
357344
await enrichProfileMetadataOnDemand(candidates, this.tree, this.bridge, this.profiler);
358-
const slowest = this.profiler.getSlowest(this.tree, candidateLimit);
345+
const slowest = this.profiler.getSlowest(this.tree, requestedLimit ?? candidateLimit);
359346
return { ok: true, data: slowest };
360347
}
361348

362349
case 'profile-rerenders': {
363-
const candidateLimit = getCandidateLimit(cmd.limit);
350+
const requestedLimit = cmd.limit;
351+
const candidateLimit = Math.max(requestedLimit ?? 10, cmd.candidateLimit ?? requestedLimit ?? 10);
364352
const candidates = this.profiler.getMostRerenders(this.tree, candidateLimit);
365353
await enrichProfileMetadataOnDemand(candidates, this.tree, this.bridge, this.profiler);
366-
const rerenders = this.profiler.getMostRerenders(this.tree, candidateLimit);
354+
const rerenders = this.profiler.getMostRerenders(this.tree, requestedLimit ?? candidateLimit);
367355
return { ok: true, data: rerenders };
368356
}
369357

packages/agent-react-devtools/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ export type IpcCommand =
206206
| { type: 'profile-start'; name?: string }
207207
| { type: 'profile-stop' }
208208
| { type: 'profile-report'; componentId: number | string }
209-
| { type: 'profile-slow'; limit?: number }
210-
| { type: 'profile-rerenders'; limit?: number }
209+
| { type: 'profile-slow'; limit?: number; candidateLimit?: number }
210+
| { type: 'profile-rerenders'; limit?: number; candidateLimit?: number }
211211
| { type: 'profile-timeline'; limit?: number; offset?: number; sort?: 'duration' | 'timeline' }
212212
| { type: 'profile-commit'; index: number; limit?: number }
213213
| { type: 'profile-export' }

0 commit comments

Comments
 (0)