Skip to content
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

[Fiber] Optimize enableProfilerCommitHooks by Collecting Elapsed Effect Duration in Module Scope #30981

Merged
merged 8 commits into from
Sep 17, 2024

Conversation

sebmarkbage
Copy link
Collaborator

@sebmarkbage sebmarkbage commented Sep 16, 2024

Stacked on #30979.

The problem with the previous approach is that it recursively walked the tree up to propagate the resulting time from recording a layout effect.

Instead, we keep a running count of the effect duration on the module scope. Then we reset it when entering a nested Profiler and then we add its elapsed count when we exit the Profiler.

This also fixes a bug where we weren't previously including unmount times for some detached trees since they couldn't bubble up to find the profiler.

Follows the pattern of the others.
This can use commitHookEffectListUnmount helper. The net effect is that
all insertion effects unmount first and then all layout effects.

That's the order that happens for updates and mounts and mirrors
how generally different phase unmounts happen.
This wrapper adds the timings of layout effects.
When we enter or leaves a Profiler boundary we can collect the effect duration
elapsed within that scope and add it to the counter.

This avoids needing to search back up for every Fiber.
We infer if this was layout or passive duration when we propagate the number in the phase.

We can also just use the same start timer as for render since they don't overlap.
Copy link

vercel bot commented Sep 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 16, 2024 11:11pm

@react-sizebot
Copy link

Comparing: a99d8e8...0707abe

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 509.31 kB 508.55 kB = 91.10 kB 91.03 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB +0.05% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 514.23 kB 513.48 kB = 91.81 kB 91.74 kB
facebook-www/ReactDOM-prod.classic.js = 604.86 kB 602.92 kB = 106.86 kB 106.73 kB
facebook-www/ReactDOM-prod.modern.js = 581.13 kB 579.22 kB = 103.01 kB 102.86 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.js = 309.00 kB 308.37 kB = 54.65 kB 54.60 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.js = 306.59 kB 305.96 kB = 54.19 kB 54.14 kB
oss-stable-rc/react-test-renderer/cjs/react-test-renderer.production.js = 306.53 kB 305.89 kB = 54.17 kB 54.12 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.js = 306.53 kB 305.89 kB = 54.17 kB 54.12 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js = 961.37 kB 959.17 kB = 162.85 kB 162.59 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js = 961.87 kB 959.66 kB = 163.84 kB 163.58 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js = 944.93 kB 942.73 kB = 160.02 kB 159.77 kB
oss-experimental/react-art/cjs/react-art.production.js = 304.00 kB 303.28 kB = 52.22 kB 52.15 kB
oss-stable/react-art/cjs/react-art.production.js = 299.49 kB 298.77 kB = 51.47 kB 51.40 kB
oss-stable-rc/react-art/cjs/react-art.production.js = 299.42 kB 298.70 kB = 51.45 kB 51.38 kB
oss-stable-semver/react-art/cjs/react-art.production.js = 299.42 kB 298.70 kB = 51.45 kB 51.38 kB
oss-stable/react-dom/cjs/react-dom-profiling.development.js = 957.25 kB 954.91 kB = 162.01 kB 161.69 kB
oss-stable-rc/react-dom/cjs/react-dom-profiling.development.js = 957.15 kB 954.80 kB = 161.99 kB 161.66 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.development.js = 957.15 kB 954.80 kB = 161.99 kB 161.66 kB
oss-stable/react-dom/cjs/react-dom-client.development.js = 940.81 kB 938.48 kB = 159.18 kB 158.86 kB
oss-stable-rc/react-dom/cjs/react-dom-client.development.js = 940.71 kB 938.37 kB = 159.16 kB 158.84 kB
oss-stable-semver/react-dom/cjs/react-dom-client.development.js = 940.71 kB 938.37 kB = 159.16 kB 158.84 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js = 540.57 kB 539.22 kB = 96.44 kB 96.23 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.js = 411.54 kB 410.49 kB = 67.43 kB 67.23 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js = 641.18 kB 639.33 kB = 103.15 kB 102.92 kB
oss-stable/react-dom/cjs/react-dom-profiling.profiling.js = 540.57 kB 538.98 kB = 96.10 kB 95.82 kB
oss-stable-rc/react-dom/cjs/react-dom-profiling.profiling.js = 540.46 kB 538.87 kB = 96.08 kB 95.80 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.profiling.js = 540.46 kB 538.87 kB = 96.08 kB 95.80 kB
react-native/implementations/ReactFabric-profiling.js = 376.88 kB 375.73 kB = 65.36 kB 65.12 kB
react-native/implementations/ReactNativeRenderer-profiling.js = 386.57 kB 385.39 kB = 67.16 kB 66.91 kB
react-native/implementations/ReactNativeRenderer-dev.js = 643.17 kB 641.20 kB = 105.52 kB 105.27 kB
facebook-www/ReactDOMTesting-prod.classic.js = 619.60 kB 617.69 kB = 110.57 kB 110.44 kB
react-native/implementations/ReactFabric-dev.js = 631.98 kB 629.99 kB = 103.47 kB 103.23 kB
facebook-www/ReactDOMTesting-prod.modern.js = 595.86 kB 593.96 kB = 106.72 kB 106.56 kB
facebook-www/ReactDOM-prod.classic.js = 604.86 kB 602.92 kB = 106.86 kB 106.73 kB
oss-stable/react-reconciler/cjs/react-reconciler.development.js = 637.75 kB 635.70 kB = 102.44 kB 102.14 kB
oss-stable-rc/react-reconciler/cjs/react-reconciler.development.js = 637.73 kB 635.68 kB = 102.42 kB 102.12 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.development.js = 637.73 kB 635.68 kB = 102.42 kB 102.12 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.js = 411.74 kB 410.41 kB = 66.98 kB 66.73 kB
oss-stable-rc/react-reconciler/cjs/react-reconciler.profiling.js = 411.72 kB 410.39 kB = 66.96 kB 66.71 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.js = 411.72 kB 410.39 kB = 66.96 kB 66.71 kB
facebook-www/ReactDOM-prod.modern.js = 581.13 kB 579.22 kB = 103.01 kB 102.86 kB
facebook-www/ReactTestRenderer-dev.classic.js = 575.18 kB 573.29 kB = 94.01 kB 93.77 kB
facebook-www/ReactTestRenderer-dev.modern.js = 575.17 kB 573.28 kB = 94.01 kB 93.77 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js = 550.41 kB 548.53 kB = 89.93 kB 89.67 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js = 547.84 kB 545.97 kB = 89.54 kB 89.26 kB
oss-stable-rc/react-test-renderer/cjs/react-test-renderer.development.js = 547.78 kB 545.90 kB = 89.52 kB 89.24 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js = 547.78 kB 545.90 kB = 89.52 kB 89.24 kB
oss-experimental/react-art/cjs/react-art.development.js = 559.59 kB 557.67 kB = 90.60 kB 90.43 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-profiling.js = 346.37 kB 345.17 kB = 60.33 kB 60.12 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js = 581.18 kB 579.07 kB = 94.66 kB 94.40 kB
oss-stable/react-art/cjs/react-art.development.js = 555.23 kB 553.11 kB = 89.82 kB 89.57 kB
oss-stable-rc/react-art/cjs/react-art.development.js = 555.16 kB 553.05 kB = 89.80 kB 89.55 kB
oss-stable-semver/react-art/cjs/react-art.development.js = 555.16 kB 553.05 kB = 89.80 kB 89.55 kB
facebook-www/ReactReconciler-prod.classic.js = 464.29 kB 462.42 kB = 75.11 kB 75.01 kB
react-native/implementations/ReactFabric-prod.fb.js = 380.79 kB 379.25 kB = 66.29 kB 66.18 kB
facebook-www/ReactDOMTesting-dev.classic.js = 1,107.60 kB 1,103.06 kB = 186.53 kB 186.17 kB
facebook-www/ReactReconciler-prod.modern.js = 445.51 kB 443.64 kB = 72.30 kB 72.18 kB
facebook-www/ReactDOM-dev.classic.js = 1,090.70 kB 1,086.11 kB = 182.57 kB 182.20 kB
facebook-www/ReactDOMTesting-dev.modern.js = 1,071.72 kB 1,067.17 kB = 180.50 kB 180.06 kB
facebook-www/ReactDOM-dev.modern.js = 1,054.86 kB 1,050.26 kB = 176.62 kB 176.20 kB
react-native/implementations/ReactNativeRenderer-prod.fb.js = 386.66 kB 384.91 kB = 67.55 kB 67.39 kB
facebook-www/ReactART-prod.classic.js = 371.37 kB 369.67 kB = 62.77 kB 62.59 kB
facebook-www/ReactART-prod.modern.js = 354.34 kB 352.64 kB = 60.01 kB 59.85 kB
react-native/implementations/ReactFabric-dev.fb.js = 662.05 kB 658.40 kB = 108.02 kB 107.68 kB
facebook-www/ReactReconciler-dev.classic.js = 744.70 kB 740.52 kB = 118.53 kB 118.09 kB
facebook-www/ReactDOM-profiling.classic.js = 634.60 kB 630.97 kB = 111.13 kB 110.72 kB
facebook-www/ReactReconciler-dev.modern.js = 720.81 kB 716.62 kB = 114.45 kB 113.97 kB
react-native/implementations/ReactNativeRenderer-dev.fb.js = 669.66 kB 665.62 kB = 109.58 kB 109.16 kB
facebook-www/ReactDOM-profiling.modern.js = 610.25 kB 606.54 kB = 107.14 kB 106.70 kB
facebook-www/ReactART-dev.classic.js = 658.22 kB 654.06 kB = 105.27 kB 104.75 kB
react-native/implementations/ReactFabric-profiling.fb.js = 407.43 kB 404.83 kB = 70.43 kB 70.11 kB
facebook-www/ReactART-dev.modern.js = 635.75 kB 631.57 kB = 101.60 kB 101.04 kB
react-native/implementations/ReactNativeRenderer-profiling.fb.js = 413.36 kB 410.46 kB = 71.64 kB 71.25 kB

Generated by 🚫 dangerJS against a41c35f

// If we refactor to preserve the unmounted Fiber tree we could fix this.
// The current implementation would require too much extra overhead to track this.
expect(call[2]).toBe(0); // durations
expect(call[2]).toBe(10100); // durations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sick

this.effectDuration = 0;
this.passiveEffectDuration = 0;
this.effectDuration = -0;
this.passiveEffectDuration = -0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebmarkbage sebmarkbage merged commit 4549be0 into facebook:main Sep 17, 2024
184 checks passed
github-actions bot pushed a commit that referenced this pull request Sep 17, 2024
…ct Duration in Module Scope (#30981)

Stacked on #30979.

The problem with the previous approach is that it recursively walked the
tree up to propagate the resulting time from recording a layout effect.

Instead, we keep a running count of the effect duration on the module
scope. Then we reset it when entering a nested Profiler and then we add
its elapsed count when we exit the Profiler.

This also fixes a bug where we weren't previously including unmount
times for some detached trees since they couldn't bubble up to find the
profiler.

DiffTrain build for commit 4549be0.
github-actions bot pushed a commit that referenced this pull request Sep 17, 2024
…ct Duration in Module Scope (#30981)

Stacked on #30979.

The problem with the previous approach is that it recursively walked the
tree up to propagate the resulting time from recording a layout effect.

Instead, we keep a running count of the effect duration on the module
scope. Then we reset it when entering a nested Profiler and then we add
its elapsed count when we exit the Profiler.

This also fixes a bug where we weren't previously including unmount
times for some detached trees since they couldn't bubble up to find the
profiler.

DiffTrain build for [4549be0](4549be0)
sebmarkbage added a commit that referenced this pull request Sep 17, 2024
Stacked on #30981. Same as #30967 but for effects.

This logs a tree of components using `performance.measure()`.

In addition to the previous render phase this logs one tree for each
commit phase:

- Mutation Phase
- Layout Effect
- Passive Unmounts
- Passive Mounts

I currently skip the Before Mutation phase since the snapshots are so
unusual it's not worth creating trees for those.

The mechanism is that I reuse the timings we track for
`enableProfilerCommitHooks`. I track first and last effect timestamp
within each component subtree. Then on the way up do we log the entry.
This means that we don't include overhead to find our way down to a
component and that we don't need to add any additional overhead by
reading timestamps.

To ensure that the entries get ordered correctly we need to ensure that
the start time of each parent is slightly before the inner one.
github-actions bot pushed a commit that referenced this pull request Sep 17, 2024
Stacked on #30981. Same as #30967 but for effects.

This logs a tree of components using `performance.measure()`.

In addition to the previous render phase this logs one tree for each
commit phase:

- Mutation Phase
- Layout Effect
- Passive Unmounts
- Passive Mounts

I currently skip the Before Mutation phase since the snapshots are so
unusual it's not worth creating trees for those.

The mechanism is that I reuse the timings we track for
`enableProfilerCommitHooks`. I track first and last effect timestamp
within each component subtree. Then on the way up do we log the entry.
This means that we don't include overhead to find our way down to a
component and that we don't need to add any additional overhead by
reading timestamps.

To ensure that the entries get ordered correctly we need to ensure that
the start time of each parent is slightly before the inner one.

DiffTrain build for [e1c2090](e1c2090)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants