@@ -2,13 +2,17 @@ import {
22 bump ,
33 chronological ,
44 commitRenders ,
5- componentNames ,
5+ componentKey ,
6+ componentsOf ,
67 formatMs ,
78 FRAME_BUDGET_MS ,
89 isFrameworkPrimitive ,
10+ sameComponent ,
911 summarizeRenders ,
1012 type CommitComponent ,
1113 type CommitProfile ,
14+ type ComponentId ,
15+ type ComponentKind ,
1216 type ComponentProfile ,
1317 type PropKindsById ,
1418 type RawProfileCommit ,
@@ -28,14 +32,15 @@ export type FindingKind =
2832export type Finding = {
2933 kind : FindingKind ;
3034 component : string ;
35+ /** Absent only for a slow commit that no app component accounts for. */
36+ componentKind ?: ComponentKind ;
3137 /** Estimated render time the pattern cost during the profile. */
3238 costMs : number ;
3339 evidence : string ;
3440 fix : string ;
3541} ;
3642
3743export type ReactProfileAnalysis = {
38- version : 1 ;
3944 wallMs : number ;
4045 commitCount : number ;
4146 renderMs : number ;
@@ -70,6 +75,17 @@ function costShare(entry: ComponentProfile, renders: number): number {
7075
7176const ms = formatMs ;
7277
78+ function about ( component : ComponentId ) : Pick < Finding , 'component' | 'componentKind' > {
79+ return { component : component . name , componentKind : component . kind } ;
80+ }
81+
82+ export function isFindingFor ( finding : Finding , component : ComponentId ) : boolean {
83+ return (
84+ finding . componentKind !== undefined &&
85+ sameComponent ( { name : finding . component , kind : finding . componentKind } , component )
86+ ) ;
87+ }
88+
7389function plural ( count : number , noun : string ) : string {
7490 return `${ count } ${ noun } ${ count === 1 ? '' : 's' } ` ;
7591}
@@ -85,7 +101,7 @@ function slowRenderFinding(entry: ComponentProfile): Finding | null {
85101 if ( entry . instances > 20 && avg < 4 ) return null ;
86102 return {
87103 kind : 'slow-render' ,
88- component : entry . name ,
104+ ... about ( entry ) ,
89105 costMs : entry . selfMs ,
90106 evidence : `${ ms ( avg ) } per render (max ${ ms ( entry . maxSelfMs ) } ), ${ plural ( entry . renders , 'render' ) } ${ instancesNote ( entry ) } ` ,
91107 fix : 'memoize or move the expensive work out of render' ,
@@ -118,15 +134,15 @@ function memoDefeatedFinding(entry: ComponentProfile): Finding | null {
118134 if ( identities . length === 0 ) {
119135 return {
120136 kind : 'memo-defeated' ,
121- component : entry . name ,
137+ ... about ( entry ) ,
122138 costMs : costShare ( entry , entry . causes . props ) ,
123139 evidence : `memo bypassed on ${ entry . causes . props } /${ updates } updates; changed ${ unstable . map ( ( key ) => `${ key } ${ changes ( key ) } ` ) . join ( ', ' ) } ${ from } (value kinds not inspected)` ,
124140 fix : `check whether those props are recreated on each render${ inOwner } ; stabilize them with useCallback/useMemo` ,
125141 } ;
126142 }
127143 return {
128144 kind : 'memo-defeated' ,
129- component : entry . name ,
145+ ... about ( entry ) ,
130146 costMs : costShare ( entry , entry . causes . props ) ,
131147 evidence : `memo bypassed on ${ entry . causes . props } /${ updates } updates; new ${ identities . map ( ( key ) => `${ key } (${ entry . propKinds [ key ] } ) ${ changes ( key ) } ` ) . join ( ', ' ) } ${ from } ` ,
132148 fix : `wrap those props in useCallback/useMemo${ inOwner } ` ,
@@ -138,7 +154,7 @@ function contextCascadeFinding(entry: ComponentProfile, triggers: string[]): Fin
138154 if ( updates < 10 || entry . causes . context < updates * 0.5 ) return null ;
139155 return {
140156 kind : 'context-cascade' ,
141- component : entry . name ,
157+ ... about ( entry ) ,
142158 costMs : costShare ( entry , entry . causes . context ) ,
143159 evidence : `${ entry . causes . context } context-driven renders${ instancesNote ( entry ) } ${ triggers . length ? ` after updates in ${ triggers . join ( ', ' ) } ` : '' } ` ,
144160 fix : 'split the context so consumers subscribe only to what they read, or memoize the provider value' ,
@@ -151,7 +167,7 @@ function parentCascadeFinding(entry: ComponentProfile): Finding | null {
151167 const owner = topOwner ( entry ) ;
152168 return {
153169 kind : 'parent-cascade' ,
154- component : entry . name ,
170+ ... about ( entry ) ,
155171 costMs : costShare ( entry , entry . causes . parent ) ,
156172 evidence : `${ entry . causes . parent } /${ updates } re-renders with unchanged props, state, and context${ owner ? ` when ${ owner } rendered` : '' } ${ instancesNote ( entry ) } ` ,
157173 fix : `wrap ${ entry . name } in React.memo or move state below ${ owner ?? 'its parent' } ` ,
@@ -170,36 +186,37 @@ type EagerMount = {
170186} ;
171187
172188function eagerMountFindings ( profile : RawReactProfile , commits : CommitProfile [ ] ) : Finding [ ] {
173- const mountsByName = new Map < string , EagerMount [ ] > ( ) ;
189+ const mountsByComponent = new Map < string , { component : ComponentId ; mounts : EagerMount [ ] } > ( ) ;
174190 profile . commits . forEach ( ( commit , index ) => {
175- const groups = new Map < string , { count : number ; actualMs : number } > ( ) ;
191+ const groups = new Map < string , { component : ComponentId ; count : number ; actualMs : number } > ( ) ;
176192 let mounted = 0 ;
177193 for ( const render of commitRenders ( profile , commit ) ) {
178194 if ( ! render . causes . includes ( 'mount' ) ) continue ;
179195 mounted += 1 ;
180196 if ( isFrameworkPrimitive ( render . name ) ) continue ;
181- const group = groups . get ( render . name ) ?? { count : 0 , actualMs : 0 } ;
197+ const key = componentKey ( render ) ;
198+ const group = groups . get ( key ) ?? { component : render , count : 0 , actualMs : 0 } ;
182199 group . count += 1 ;
183200 group . actualMs += render . actualMs ;
184- groups . set ( render . name , group ) ;
201+ groups . set ( key , group ) ;
185202 }
186203 const durationMs = commits [ index ] ! . durationMs ;
187- for ( const [ name , group ] of groups ) {
204+ for ( const [ key , group ] of groups ) {
188205 if ( group . count < EAGER_MOUNT_INSTANCES ) continue ;
189206 const costMs = Math . min ( group . actualMs , durationMs ) ;
190- const mounts = mountsByName . get ( name ) ?? [ ] ;
191- mounts . push ( { index, count : group . count , costMs, durationMs, mounted } ) ;
192- mountsByName . set ( name , mounts ) ;
207+ const entry = mountsByComponent . get ( key ) ?? { component : group . component , mounts : [ ] } ;
208+ entry . mounts . push ( { index, count : group . count , costMs, durationMs, mounted } ) ;
209+ mountsByComponent . set ( key , entry ) ;
193210 }
194211 } ) ;
195- return [ ...mountsByName . entries ( ) ] . map ( ( [ name , mounts ] ) => {
212+ return [ ...mountsByComponent . values ( ) ] . map ( ( { component , mounts } ) => {
196213 const worst = mounts . reduce ( ( a , b ) => ( b . costMs > a . costMs ? b : a ) ) ;
197214 const costMs = mounts . reduce ( ( sum , mount ) => sum + mount . costMs , 0 ) ;
198215 const repeated =
199216 mounts . length > 1 ? `; ${ mounts . length } such commits, ${ ms ( costMs ) } in all` : '' ;
200217 return {
201218 kind : 'eager-mount' as const ,
202- component : name ,
219+ ... about ( component ) ,
203220 costMs,
204221 evidence : `${ worst . count } instances mounted at once in commit #${ worst . index } , ${ ms ( worst . costMs ) } of its ${ ms ( worst . durationMs ) } (${ worst . mounted } components mounted)${ repeated } ` ,
205222 fix : 'virtualize the list (FlatList/FlashList)' ,
@@ -215,38 +232,39 @@ function eagerMountFindings(profile: RawReactProfile, commits: CommitProfile[]):
215232const CHAINED_COMMIT_GAP_MS = FRAME_BUDGET_MS ;
216233
217234/** App components that rendered in `previous` without triggering it, then triggered `commit`. */
218- function chainedUpdaterNames (
235+ function chainedUpdaters (
219236 profile : RawReactProfile ,
220237 previous : RawProfileCommit ,
221238 commit : RawProfileCommit ,
222- ) : string [ ] {
239+ ) : ComponentId [ ] {
223240 const renderedBefore = new Set ( previous . fibers . filter ( ( _ , offset ) => offset % 3 === 0 ) ) ;
224241 const triggeredBefore = new Set ( previous . updaters ) ;
225242 const chained = commit . updaters . filter (
226243 ( updater ) => renderedBefore . has ( updater ) && ! triggeredBefore . has ( updater ) ,
227244 ) ;
228- return componentNames ( profile , chained ) . filter ( ( name ) => ! isFrameworkPrimitive ( name ) ) ;
245+ return componentsOf ( profile , chained ) . filter ( ( updater ) => ! isFrameworkPrimitive ( updater . name ) ) ;
229246}
230247
231248function effectChainFindings ( profile : RawReactProfile ) : Finding [ ] {
232- const chains = new Map < string , { count : number ; costMs : number } > ( ) ;
249+ const chains = new Map < string , { component : ComponentId ; count : number ; costMs : number } > ( ) ;
233250 for ( let index = 1 ; index < profile . commits . length ; index += 1 ) {
234251 const previous = profile . commits [ index - 1 ] ! ;
235252 const commit = profile . commits [ index ] ! ;
236253 const gap = commit . timestamp - commit . duration - previous . timestamp ;
237254 if ( gap > CHAINED_COMMIT_GAP_MS ) continue ;
238- for ( const name of chainedUpdaterNames ( profile , previous , commit ) ) {
239- const chain = chains . get ( name ) ?? { count : 0 , costMs : 0 } ;
255+ for ( const updater of chainedUpdaters ( profile , previous , commit ) ) {
256+ const key = componentKey ( updater ) ;
257+ const chain = chains . get ( key ) ?? { component : updater , count : 0 , costMs : 0 } ;
240258 chain . count += 1 ;
241259 chain . costMs += commit . duration ;
242- chains . set ( name , chain ) ;
260+ chains . set ( key , chain ) ;
243261 }
244262 }
245- return [ ...chains . entries ( ) ]
246- . filter ( ( [ , chain ] ) => chain . count >= 3 )
247- . map ( ( [ name , chain ] ) => ( {
263+ return [ ...chains . values ( ) ]
264+ . filter ( ( chain ) => chain . count >= 3 )
265+ . map ( ( chain ) => ( {
248266 kind : 'effect-chain' as const ,
249- component : name ,
267+ ... about ( chain . component ) ,
250268 costMs : chain . costMs ,
251269 evidence : `${ plural ( chain . count , 'follow-up commit' ) } it triggered right after rendering, likely state set in an effect (${ ms ( chain . costMs ) } )` ,
252270 fix : 'if an effect copies derived state, compute it during render (useMemo) instead' ,
@@ -257,17 +275,18 @@ const STORM_MIN_COMMITS = 20;
257275const STORM_MIN_RATE_PER_SECOND = 20 ;
258276
259277function renderStormFindings ( profile : RawReactProfile ) : Finding [ ] {
260- const byUpdater = new Map < string , number [ ] > ( ) ;
278+ const byUpdater = new Map < string , { component : ComponentId ; indexes : number [ ] } > ( ) ;
261279 profile . commits . forEach ( ( commit , index ) => {
262- for ( const name of componentNames ( profile , commit . updaters ) ) {
263- const indexes = byUpdater . get ( name ) ?? [ ] ;
264- indexes . push ( index ) ;
265- byUpdater . set ( name , indexes ) ;
280+ for ( const updater of componentsOf ( profile , commit . updaters ) ) {
281+ const key = componentKey ( updater ) ;
282+ const entry = byUpdater . get ( key ) ?? { component : updater , indexes : [ ] } ;
283+ entry . indexes . push ( index ) ;
284+ byUpdater . set ( key , entry ) ;
266285 }
267286 } ) ;
268287 const findings : Finding [ ] = [ ] ;
269- for ( const [ name , indexes ] of byUpdater ) {
270- if ( indexes . length < STORM_MIN_COMMITS || isFrameworkPrimitive ( name ) ) continue ;
288+ for ( const { component , indexes } of byUpdater . values ( ) ) {
289+ if ( indexes . length < STORM_MIN_COMMITS || isFrameworkPrimitive ( component . name ) ) continue ;
271290 const first = profile . commits [ indexes [ 0 ] ! ] ! . timestamp ;
272291 const last = profile . commits [ indexes . at ( - 1 ) ! ] ! . timestamp ;
273292 const spanSeconds = Math . max ( ( last - first ) / 1000 , 0.001 ) ;
@@ -276,7 +295,7 @@ function renderStormFindings(profile: RawReactProfile): Finding[] {
276295 const costMs = indexes . reduce ( ( sum , index ) => sum + profile . commits [ index ] ! . duration , 0 ) ;
277296 findings . push ( {
278297 kind : 'render-storm' ,
279- component : name ,
298+ ... about ( component ) ,
280299 costMs,
281300 evidence : `${ indexes . length } commits in ${ spanSeconds . toFixed ( 1 ) } s (${ Math . round ( rate ) } /s, ${ ms ( costMs ) } rendering)` ,
282301 fix : 'throttle the updates or animate outside React state (Animated/Reanimated)' ,
@@ -297,47 +316,48 @@ const SELF_COST_FINDINGS = new Set<FindingKind>(['slow-render', 'effect-chain',
297316
298317/** Render time in a commit that earlier findings already account for. */
299318function explainedMs ( commit : CommitProfile , earlier : Finding [ ] ) : number {
300- const bySelf = new Set (
301- earlier . filter ( ( f ) => SELF_COST_FINDINGS . has ( f . kind ) ) . map ( ( f ) => f . component ) ,
302- ) ;
303- const bySubtree = new Set (
304- earlier . filter ( ( f ) => ! SELF_COST_FINDINGS . has ( f . kind ) ) . map ( ( f ) => f . component ) ,
305- ) ;
306319 let total = 0 ;
307- for ( const entry of commit . top ) {
308- if ( bySubtree . has ( entry . name ) ) total += entry . actualMs ;
309- else if ( bySelf . has ( entry . name ) ) total += entry . selfMs ;
320+ for ( const entry of commit . components ) {
321+ const findings = earlier . filter ( ( finding ) => isFindingFor ( finding , entry ) ) ;
322+ if ( findings . some ( ( finding ) => ! SELF_COST_FINDINGS . has ( finding . kind ) ) ) total += entry . actualMs ;
323+ else if ( findings . length > 0 ) total += entry . selfMs ;
310324 }
311325 return total ;
312326}
313327
314328/** The app component to cut work under: the updater when it holds the commit, else the deepest holder. */
315329function subtreeOwner ( commit : CommitProfile ) : CommitComponent | undefined {
316- const covering = commit . top . filter (
330+ const covering = commit . components . filter (
317331 ( entry ) =>
318332 ! isFrameworkPrimitive ( entry . name ) && entry . actualMs >= commit . durationMs * SUBTREE_SHARE ,
319333 ) ;
320334 return (
321- covering . find ( ( entry ) => commit . updaters . includes ( entry . name ) ) ??
335+ covering . find ( ( entry ) => commit . updaters . some ( ( updater ) => sameComponent ( updater , entry ) ) ) ??
322336 covering . sort ( ( a , b ) => a . actualMs - b . actualMs ) [ 0 ]
323337 ) ;
324338}
325339
326340function slowCommitFinding ( commit : CommitProfile ) : Finding {
327341 const owner = subtreeOwner ( commit ) ;
328- const heaviest = [ ...commit . top ]
329- . sort ( ( a , b ) => b . selfMs - a . selfMs )
342+ const heaviest = commit . components
330343 . slice ( 0 , 3 )
331344 . map (
332345 ( entry ) =>
333346 `${ entry . name } ${ entry . instances > 1 ? ` x${ entry . instances } ` : '' } ${ ms ( entry . selfMs ) } ` ,
334347 ) ;
335348 const mounted = commit . mounts > 0 ? ` (${ commit . mounts } mounted)` : '' ;
336- const inside = owner && ! commit . updaters . includes ( owner . name ) ? ` inside ${ owner . name } ` : '' ;
337- const trigger = commit . updaters . length > 0 ? ` after ${ commit . updaters . join ( ', ' ) } updated` : '' ;
349+ const inside =
350+ owner && ! commit . updaters . some ( ( updater ) => sameComponent ( updater , owner ) )
351+ ? ` inside ${ owner . name } `
352+ : '' ;
353+ const trigger =
354+ commit . updaters . length > 0
355+ ? ` after ${ commit . updaters . map ( ( updater ) => updater . name ) . join ( ', ' ) } updated`
356+ : '' ;
357+ const component = owner ?? commit . updaters [ 0 ] ;
338358 return {
339359 kind : 'slow-commit' ,
340- component : owner ?. name ?? commit . updaters [ 0 ] ?? `#${ commit . index } ` ,
360+ ... ( component ? about ( component ) : { component : `#${ commit . index } ` } ) ,
341361 costMs : commit . durationMs ,
342362 evidence : `commit #${ commit . index } took ${ ms ( commit . durationMs ) } rendering ${ commit . rendered } components${ mounted } ${ inside } ${ trigger } ; most time in ${ heaviest . join ( ', ' ) } ` ,
343363 fix : `render less ${ owner ? `under ${ owner . name } ` : '' } per update (memoize, virtualize, or defer)` ,
@@ -353,18 +373,18 @@ function slowCommitFindings(commits: CommitProfile[], earlier: Finding[]): Findi
353373 . map ( slowCommitFinding ) ;
354374}
355375
356- function contextTriggers ( profile : RawReactProfile , name : string ) : string [ ] {
376+ function contextTriggers ( profile : RawReactProfile , component : ComponentId ) : string [ ] {
357377 const triggers : Record < string , number > = { } ;
358378 for ( const commit of profile . commits ) {
359379 let contextRender = false ;
360380 for ( const render of commitRenders ( profile , commit ) ) {
361- if ( render . name === name && render . causes . includes ( 'context' ) ) {
381+ if ( sameComponent ( render , component ) && render . causes . includes ( 'context' ) ) {
362382 contextRender = true ;
363383 break ;
364384 }
365385 }
366386 if ( ! contextRender ) continue ;
367- for ( const name of componentNames ( profile , commit . updaters ) ) bump ( triggers , name ) ;
387+ for ( const updater of componentsOf ( profile , commit . updaters ) ) bump ( triggers , updater . name ) ;
368388 }
369389 return topKeys ( triggers , 1 ) . slice ( 0 , 2 ) ;
370390}
@@ -387,10 +407,7 @@ export function analyzeReactProfile(
387407 if ( isFrameworkPrimitive ( entry . name ) ) continue ;
388408 const cascades = [
389409 memoDefeatedFinding ( entry ) ,
390- contextCascadeFinding (
391- entry ,
392- entry . causes . context > 0 ? contextTriggers ( profile , entry . name ) : [ ] ,
393- ) ,
410+ contextCascadeFinding ( entry , entry . causes . context > 0 ? contextTriggers ( profile , entry ) : [ ] ) ,
394411 parentCascadeFinding ( entry ) ,
395412 ] . filter ( ( finding ) : finding is Finding => finding !== null ) ;
396413 // A cascade explains many cheap renders; only a render that alone overruns a frame is also slow.
@@ -410,7 +427,6 @@ export function analyzeReactProfile(
410427 . sort ( ( a , b ) => b . costMs - a . costMs ) ;
411428
412429 return {
413- version : 1 ,
414430 wallMs : profile . stoppedAt - profile . startedAt ,
415431 commitCount : commits . length ,
416432 renderMs,
0 commit comments