Repository navigation
fix(react-devtools): count render work across sibling subtrees #5427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PLASMA-FR
wants to merge
1
commit into
callstack:main
Choose a base branch
from
PLASMA-FR:fix/react-profile-sibling-work-5401
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−45
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,7 +46,7 @@ export type ReactProfileAnalysis = { | |||||
| commitCount: number; | ||||||
| renderMs: number; | ||||||
| slowCommitCount: number; | ||||||
| worstCommit: { index: number; durationMs: number } | null; | ||||||
| worstCommit: Pick<CommitProfile, 'index' | 'durationMs' | 'workMs'> | null; | ||||||
| naming: RawReactProfile['naming']; | ||||||
| /** Gaps in the evidence behind the findings, such as uninspected prop values. */ | ||||||
| notes: string[]; | ||||||
|
|
@@ -182,7 +182,7 @@ type EagerMount = { | |||||
| index: number; | ||||||
| count: number; | ||||||
| costMs: number; | ||||||
| durationMs: number; | ||||||
| workMs: number; | ||||||
| mounted: number; | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -201,12 +201,12 @@ function eagerMountFindings(profile: RawReactProfile, commits: CommitProfile[]): | |||||
| group.actualMs += render.actualMs; | ||||||
| groups.set(key, group); | ||||||
| } | ||||||
| const durationMs = commits[index]!.durationMs; | ||||||
| const workMs = commits[index]!.workMs; | ||||||
| for (const [key, group] of groups) { | ||||||
| if (group.count < EAGER_MOUNT_INSTANCES) continue; | ||||||
| const costMs = Math.min(group.actualMs, durationMs); | ||||||
| const costMs = Math.min(group.actualMs, workMs); | ||||||
| const entry = mountsByComponent.get(key) ?? { component: group.component, mounts: [] }; | ||||||
| entry.mounts.push({ index, count: group.count, costMs, durationMs, mounted }); | ||||||
| entry.mounts.push({ index, count: group.count, costMs, workMs, mounted }); | ||||||
| mountsByComponent.set(key, entry); | ||||||
| } | ||||||
| }); | ||||||
|
|
@@ -219,7 +219,7 @@ function eagerMountFindings(profile: RawReactProfile, commits: CommitProfile[]): | |||||
| kind: 'eager-mount' as const, | ||||||
| ...about(component), | ||||||
| costMs, | ||||||
| evidence: `${worst.count} instances mounted at once in commit #${worst.index}, ${ms(worst.costMs)} of its ${ms(worst.durationMs)} (${worst.mounted} components mounted)${repeated}`, | ||||||
| evidence: `${worst.count} instances mounted at once in commit #${worst.index}, ${ms(worst.costMs)} of its ${ms(worst.workMs)} (${worst.mounted} components mounted)${repeated}`, | ||||||
| fix: 'virtualize the list (FlatList/FlashList)', | ||||||
| }; | ||||||
| }); | ||||||
|
|
@@ -246,7 +246,7 @@ function chainedUpdaters( | |||||
| return componentsOf(profile, chained).filter((updater) => !isFrameworkPrimitive(updater.name)); | ||||||
| } | ||||||
|
|
||||||
| function effectChainFindings(profile: RawReactProfile): Finding[] { | ||||||
| function effectChainFindings(profile: RawReactProfile, commits: CommitProfile[]): Finding[] { | ||||||
| const chains = new Map<string, { component: ComponentId; count: number; costMs: number }>(); | ||||||
| for (let index = 1; index < profile.commits.length; index += 1) { | ||||||
| const previous = profile.commits[index - 1]!; | ||||||
|
|
@@ -257,7 +257,7 @@ function effectChainFindings(profile: RawReactProfile): Finding[] { | |||||
| const key = componentKey(updater); | ||||||
| const chain = chains.get(key) ?? { component: updater, count: 0, costMs: 0 }; | ||||||
| chain.count += 1; | ||||||
| chain.costMs += commit.duration; | ||||||
| chain.costMs += commits[index]!.workMs; | ||||||
| chains.set(key, chain); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -275,7 +275,7 @@ function effectChainFindings(profile: RawReactProfile): Finding[] { | |||||
| const STORM_MIN_COMMITS = 20; | ||||||
| const STORM_MIN_RATE_PER_SECOND = 20; | ||||||
|
|
||||||
| function renderStormFindings(profile: RawReactProfile): Finding[] { | ||||||
| function renderStormFindings(profile: RawReactProfile, commits: CommitProfile[]): Finding[] { | ||||||
| const byUpdater = new Map<string, { component: ComponentId; indexes: number[] }>(); | ||||||
| profile.commits.forEach((commit, index) => { | ||||||
| for (const updater of componentsOf(profile, commit.updaters)) { | ||||||
|
|
@@ -293,7 +293,7 @@ function renderStormFindings(profile: RawReactProfile): Finding[] { | |||||
| const spanSeconds = Math.max((last - first) / 1000, 0.001); | ||||||
| const rate = (indexes.length - 1) / spanSeconds; | ||||||
| if (rate < STORM_MIN_RATE_PER_SECOND) continue; | ||||||
| const costMs = indexes.reduce((sum, index) => sum + profile.commits[index]!.duration, 0); | ||||||
| const costMs = indexes.reduce((sum, index) => sum + commits[index]!.workMs, 0); | ||||||
| findings.push({ | ||||||
| kind: 'render-storm', | ||||||
| ...about(component), | ||||||
|
|
@@ -366,8 +366,7 @@ function explainedMs( | |||||
| /** The app component to cut work under: the updater when it holds the commit, else the deepest holder. */ | ||||||
| function subtreeOwner(commit: CommitProfile): CommitComponent | undefined { | ||||||
| const covering = commit.components.filter( | ||||||
| (entry) => | ||||||
| !isFrameworkPrimitive(entry.name) && entry.actualMs >= commit.durationMs * SUBTREE_SHARE, | ||||||
| (entry) => !isFrameworkPrimitive(entry.name) && entry.actualMs >= commit.workMs * SUBTREE_SHARE, | ||||||
| ); | ||||||
| return ( | ||||||
| covering.find((entry) => commit.updaters.some((updater) => sameComponent(updater, entry))) ?? | ||||||
|
|
@@ -396,8 +395,8 @@ function slowCommitFinding(commit: CommitProfile): Finding { | |||||
| return { | ||||||
| kind: 'slow-commit', | ||||||
| ...(component ? about(component) : { component: `#${commit.index}` }), | ||||||
| costMs: commit.durationMs, | ||||||
| evidence: `commit #${commit.index} took ${ms(commit.durationMs)} rendering ${commit.rendered} components${mounted}${inside}${trigger}; most time in ${heaviest.join(', ')}`, | ||||||
| costMs: commit.workMs, | ||||||
| evidence: `commit #${commit.index} took ${ms(commit.workMs)} rendering ${commit.rendered} components${mounted}${inside}${trigger}; most time in ${heaviest.join(', ')}`, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Suggested change
|
||||||
| fix: `render less ${owner ? `under ${owner.name} ` : ''}per update (memoize, virtualize, or defer)`, | ||||||
| }; | ||||||
| } | ||||||
|
|
@@ -409,13 +408,13 @@ function slowCommitFindings( | |||||
| ): Finding[] { | ||||||
| const explained = explanations(earlier); | ||||||
| return commits | ||||||
| .filter((commit) => commit.durationMs >= SLOW_COMMIT_MS) | ||||||
| .filter((commit) => commit.workMs >= SLOW_COMMIT_MS) | ||||||
| .filter( | ||||||
| (commit) => | ||||||
| explainedMs(profile, profile.commits[commit.index]!, explained) < | ||||||
| commit.durationMs * EXPLAINED_SHARE, | ||||||
| commit.workMs * EXPLAINED_SHARE, | ||||||
| ) | ||||||
| .sort((a, b) => b.durationMs - a.durationMs) | ||||||
| .sort((a, b) => b.workMs - a.workMs) | ||||||
| .slice(0, SLOW_COMMIT_FINDING_LIMIT) | ||||||
| .map(slowCommitFinding); | ||||||
| } | ||||||
|
|
@@ -443,9 +442,9 @@ export function analyzeReactProfile( | |||||
| ): ReactProfileAnalysis { | ||||||
| const profile = chronological(recorded); | ||||||
| const { components, commits } = summarizeRenders(profile, propKindsById); | ||||||
| const slowCommits = commits.filter((commit) => commit.durationMs > FRAME_BUDGET_MS); | ||||||
| const slowCommits = commits.filter((commit) => commit.workMs > FRAME_BUDGET_MS); | ||||||
| const worst = commits.reduce<CommitProfile | null>( | ||||||
| (current, commit) => (current && current.durationMs >= commit.durationMs ? current : commit), | ||||||
| (current, commit) => (current && current.workMs >= commit.workMs ? current : commit), | ||||||
| null, | ||||||
| ); | ||||||
|
|
||||||
|
|
@@ -464,10 +463,10 @@ export function analyzeReactProfile( | |||||
| findings.push(...perComponent); | ||||||
| } | ||||||
| findings.push(...eagerMountFindings(profile, commits)); | ||||||
| findings.push(...effectChainFindings(profile)); | ||||||
| findings.push(...renderStormFindings(profile)); | ||||||
| findings.push(...effectChainFindings(profile, commits)); | ||||||
| findings.push(...renderStormFindings(profile, commits)); | ||||||
| findings.push(...slowCommitFindings(profile, commits, findings)); | ||||||
| const renderMs = commits.reduce((sum, commit) => sum + commit.durationMs, 0); | ||||||
| const renderMs = commits.reduce((sum, commit) => sum + commit.workMs, 0); | ||||||
| const costFloorMs = Math.max(MIN_FINDING_COST_MS, renderMs * MIN_FINDING_COST_SHARE); | ||||||
| const significant = findings | ||||||
| .filter((finding) => finding.costMs >= costFloorMs) | ||||||
|
|
@@ -478,7 +477,9 @@ export function analyzeReactProfile( | |||||
| commitCount: commits.length, | ||||||
| renderMs, | ||||||
| slowCommitCount: slowCommits.length, | ||||||
| worstCommit: worst ? { index: worst.index, durationMs: worst.durationMs } : null, | ||||||
| worstCommit: worst | ||||||
| ? { index: worst.index, durationMs: worst.durationMs, workMs: worst.workMs } | ||||||
| : null, | ||||||
| naming: profile.naming, | ||||||
| notes, | ||||||
| components, | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Each matched updater receives the full commit
workMs, so a commit with multiple updaters charges the same sibling work to every component's finding. Attribute work to the relevant updater or avoid per-component cost attribution when the commit has multiple updaters.Prompt for AI agents