GeezerVison is showing github.com — Experiment: Unsuspend all lanes on update (#20660) · react/react@d13f5b9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.new.js +34-6Lines changed: 34 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,10 @@ export type Lane = number; | 36 | 36 | export type LaneMap<T> = Array<T>; | 37 | 37 |
| 38 | 38 | import invariant from 'shared/invariant'; | 39 | | -import {enableCache} from 'shared/ReactFeatureFlags'; | | 39 | +import { | | 40 | + enableCache, | | 41 | + enableTransitionEntanglement, | | 42 | +} from 'shared/ReactFeatureFlags'; | 40 | 43 |
| 41 | 44 | import { | 42 | 45 | ImmediatePriority as ImmediateSchedulerPriority, | @@ -306,9 +309,15 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes { | 306 | 309 | return NoLanes; | 307 | 310 | } | 308 | 311 |
| 309 | | - // If there are higher priority lanes, we'll include them even if they | 310 | | - // are suspended. | 311 | | - nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes); | | 312 | + if (enableTransitionEntanglement) { | | 313 | + // We don't need to include higher priority lanes, because in this | | 314 | + // experiment we always unsuspend all transitions whenever we receive | | 315 | + // an update. | | 316 | + } else { | | 317 | + // If there are higher priority lanes, we'll include them even if they | | 318 | + // are suspended. | | 319 | + nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes); | | 320 | + } | 312 | 321 |
| 313 | 322 | // If we're already in the middle of a render, switching lanes will interrupt | 314 | 323 | // it and we'll lose our progress. We should only do this if the new lanes are | @@ -673,8 +682,27 @@ export function markRootUpdated( | 673 | 682 | // Unsuspend any update at equal or lower priority. | 674 | 683 | const higherPriorityLanes = updateLane - 1; // Turns 0b1000 into 0b0111 | 675 | 684 |
| 676 | | - root.suspendedLanes &= higherPriorityLanes; | 677 | | - root.pingedLanes &= higherPriorityLanes; | | 685 | + if (enableTransitionEntanglement) { | | 686 | + // If there are any suspended transitions, it's possible this new update | | 687 | + // could unblock them. Clear the suspended lanes so that we can try rendering | | 688 | + // them again. | | 689 | + // | | 690 | + // TODO: We really only need to unsuspend only lanes that are in the | | 691 | + // `subtreeLanes` of the updated fiber, or the update lanes of the return | | 692 | + // path. This would exclude suspended updates in an unrelated sibling tree, | | 693 | + // since there's no way for this update to unblock it. | | 694 | + // | | 695 | + // We don't do this if the incoming update is idle, because we never process | | 696 | + // idle updates until after all the regular updates have finished; there's no | | 697 | + // way it could unblock a transition. | | 698 | + if ((updateLane & IdleLanes) === NoLanes) { | | 699 | + root.suspendedLanes = NoLanes; | | 700 | + root.pingedLanes = NoLanes; | | 701 | + } | | 702 | + } else { | | 703 | + root.suspendedLanes &= higherPriorityLanes; | | 704 | + root.pingedLanes &= higherPriorityLanes; | | 705 | + } | 678 | 706 |
| 679 | 707 | const eventTimes = root.eventTimes; | 680 | 708 | const index = laneToIndex(updateLane); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.old.js +34-6Lines changed: 34 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,10 @@ export type Lane = number; | 36 | 36 | export type LaneMap<T> = Array<T>; | 37 | 37 |
| 38 | 38 | import invariant from 'shared/invariant'; | 39 | | -import {enableCache} from 'shared/ReactFeatureFlags'; | | 39 | +import { | | 40 | + enableCache, | | 41 | + enableTransitionEntanglement, | | 42 | +} from 'shared/ReactFeatureFlags'; | 40 | 43 |
| 41 | 44 | import { | 42 | 45 | ImmediatePriority as ImmediateSchedulerPriority, | @@ -306,9 +309,15 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes { | 306 | 309 | return NoLanes; | 307 | 310 | } | 308 | 311 |
| 309 | | - // If there are higher priority lanes, we'll include them even if they | 310 | | - // are suspended. | 311 | | - nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes); | | 312 | + if (enableTransitionEntanglement) { | | 313 | + // We don't need to include higher priority lanes, because in this | | 314 | + // experiment we always unsuspend all transitions whenever we receive | | 315 | + // an update. | | 316 | + } else { | | 317 | + // If there are higher priority lanes, we'll include them even if they | | 318 | + // are suspended. | | 319 | + nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes); | | 320 | + } | 312 | 321 |
| 313 | 322 | // If we're already in the middle of a render, switching lanes will interrupt | 314 | 323 | // it and we'll lose our progress. We should only do this if the new lanes are | @@ -673,8 +682,27 @@ export function markRootUpdated( | 673 | 682 | // Unsuspend any update at equal or lower priority. | 674 | 683 | const higherPriorityLanes = updateLane - 1; // Turns 0b1000 into 0b0111 | 675 | 684 |
| 676 | | - root.suspendedLanes &= higherPriorityLanes; | 677 | | - root.pingedLanes &= higherPriorityLanes; | | 685 | + if (enableTransitionEntanglement) { | | 686 | + // If there are any suspended transitions, it's possible this new update | | 687 | + // could unblock them. Clear the suspended lanes so that we can try rendering | | 688 | + // them again. | | 689 | + // | | 690 | + // TODO: We really only need to unsuspend only lanes that are in the | | 691 | + // `subtreeLanes` of the updated fiber, or the update lanes of the return | | 692 | + // path. This would exclude suspended updates in an unrelated sibling tree, | | 693 | + // since there's no way for this update to unblock it. | | 694 | + // | | 695 | + // We don't do this if the incoming update is idle, because we never process | | 696 | + // idle updates until after all the regular updates have finished; there's no | | 697 | + // way it could unblock a transition. | | 698 | + if ((updateLane & IdleLanes) === NoLanes) { | | 699 | + root.suspendedLanes = NoLanes; | | 700 | + root.pingedLanes = NoLanes; | | 701 | + } | | 702 | + } else { | | 703 | + root.suspendedLanes &= higherPriorityLanes; | | 704 | + root.pingedLanes &= higherPriorityLanes; | | 705 | + } | 678 | 706 |
| 679 | 707 | const eventTimes = root.eventTimes; | 680 | 708 | const index = laneToIndex(updateLane); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactExpiration-test.js +13-7Lines changed: 13 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -733,13 +733,19 @@ describe('ReactExpiration', () => { | 733 | 733 | // Both normal pri updates should have expired. | 734 | 734 | expect(Scheduler).toFlushExpired([ | 735 | 735 | 'Sibling', | 736 | | - // Note: we also flushed the high pri update here, because in the | 737 | | - // current implementation, once we pick the next lanes to work on, we | 738 | | - // entangle it with all pending at equal or higher priority. We could | 739 | | - // feasibly change this heuristic so that the high pri update doesn't | 740 | | - // render until after the expired updates have finished. But the | 741 | | - // important thing in this test is that the normal updates expired. | 742 | | - 'High pri: 1', | | 736 | + gate(flags => flags.enableTransitionEntanglement) | | 737 | + ? // Notice that the high pri update didn't flush yet. Expiring one lane | | 738 | + // doesn't affect other lanes. (Unless they are intentionally | | 739 | + // entangled, like we do for overlapping transitions that affect the | | 740 | + // same state.) | | 741 | + 'High pri: 0' | | 742 | + : // In the current implementation, once we pick the next lanes to work | | 743 | + // on, we entangle it with all pending at equal or higher priority. | | 744 | + // We could feasibly change this heuristic so that the high pri | | 745 | + // update doesn't render until after the expired updates have | | 746 | + // finished. But the important thing in this test is that the normal | | 747 | + // updates expired. | | 748 | + 'High pri: 1', | 743 | 749 | 'Normal pri: 2', | 744 | 750 | 'Sibling', | 745 | 751 | ]); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSuspenseList-test.js +13-24Lines changed: 13 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2443,41 +2443,30 @@ describe('ReactSuspenseList', () => { | 2443 | 2443 |
| 2444 | 2444 | expect(ReactNoop).toMatchRenderedOutput(null); | 2445 | 2445 |
| 2446 | | - ReactNoop.act(() => { | | 2446 | + await ReactNoop.act(async () => { | 2447 | 2447 | // Add a few items at the end. | 2448 | 2448 | updateLowPri(true); | 2449 | 2449 |
| 2450 | 2450 | // Flush partially through. | 2451 | 2451 | expect(Scheduler).toFlushAndYieldThrough(['B', 'C']); | 2452 | 2452 |
| 2453 | 2453 | // Schedule another update at higher priority. | 2454 | | - Scheduler.unstable_runWithPriority( | 2455 | | - Scheduler.unstable_UserBlockingPriority, | 2456 | | - () => updateHighPri(true), | 2457 | | - ); | | 2454 | + ReactNoop.flushSync(() => updateHighPri(true)); | 2458 | 2455 |
| 2459 | 2456 | // That will intercept the previous render. | 2460 | | - }); | | 2457 | + expect(Scheduler).toHaveYielded([ | | 2458 | + 'Suspend! [A]', | | 2459 | + 'Loading A', | | 2460 | + // Re-render at forced. | | 2461 | + 'Suspend! [A]', | | 2462 | + 'Loading A', | | 2463 | + ]); | | 2464 | + expect(ReactNoop).toMatchRenderedOutput(<span>Loading A</span>); | 2461 | 2465 |
| 2462 | | - jest.runAllTimers(); | 2463 | | - | 2464 | | - expect(Scheduler).toHaveYielded([ | 2465 | | - // First attempt at high pri. | 2466 | | - 'Suspend! [A]', | 2467 | | - 'Loading A', | 2468 | | - // Re-render at forced. | 2469 | | - 'Suspend! [A]', | 2470 | | - 'Loading A', | 2471 | | - // We auto-commit this on DEV. | 2472 | 2466 | // Try again on low-pri. | 2473 | | - 'Suspend! [A]', | 2474 | | - 'Loading A', | 2475 | | - // Re-render at forced. | 2476 | | - 'Suspend! [A]', | 2477 | | - 'Loading A', | 2478 | | - ]); | 2479 | | - | 2480 | | - expect(ReactNoop).toMatchRenderedOutput(<span>Loading A</span>); | | 2467 | + expect(Scheduler).toFlushAndYield(['Suspend! [A]', 'Loading A']); | | 2468 | + expect(ReactNoop).toMatchRenderedOutput(<span>Loading A</span>); | | 2469 | + }); | 2481 | 2470 |
| 2482 | 2471 | await AsyncA.resolve(); | 2483 | 2472 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactFeatureFlags.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -149,3 +149,6 @@ export const enableUseRefAccessWarning = false; | 149 | 149 | export const enableRecursiveCommitTraversal = false; | 150 | 150 |
| 151 | 151 | export const disableSchedulerTimeoutInWorkLoop = false; | | 152 | + | | 153 | +// Experiment to simplify/improve how transitions are scheduled | | 154 | +export const enableTransitionEntanglement = false; |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,7 @@ export const enableUseRefAccessWarning = false; | 58 | 58 |
| 59 | 59 | export const enableRecursiveCommitTraversal = false; | 60 | 60 | export const disableSchedulerTimeoutInWorkLoop = false; | | 61 | +export const enableTransitionEntanglement = false; | 61 | 62 |
| 62 | 63 | // Flow magic to verify the exports of this file match the original version. | 63 | 64 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-oss.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,7 @@ export const enableUseRefAccessWarning = false; | 57 | 57 |
| 58 | 58 | export const enableRecursiveCommitTraversal = false; | 59 | 59 | export const disableSchedulerTimeoutInWorkLoop = false; | | 60 | +export const enableTransitionEntanglement = false; | 60 | 61 |
| 61 | 62 | // Flow magic to verify the exports of this file match the original version. | 62 | 63 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,7 @@ export const enableUseRefAccessWarning = false; | 57 | 57 |
| 58 | 58 | export const enableRecursiveCommitTraversal = false; | 59 | 59 | export const disableSchedulerTimeoutInWorkLoop = false; | | 60 | +export const enableTransitionEntanglement = false; | 60 | 61 |
| 61 | 62 | // Flow magic to verify the exports of this file match the original version. | 62 | 63 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.native.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,7 @@ export const enableUseRefAccessWarning = false; | 57 | 57 |
| 58 | 58 | export const enableRecursiveCommitTraversal = false; | 59 | 59 | export const disableSchedulerTimeoutInWorkLoop = false; | | 60 | +export const enableTransitionEntanglement = false; | 60 | 61 |
| 61 | 62 | // Flow magic to verify the exports of this file match the original version. | 62 | 63 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.www.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,7 @@ export const enableUseRefAccessWarning = false; | 57 | 57 |
| 58 | 58 | export const enableRecursiveCommitTraversal = false; | 59 | 59 | export const disableSchedulerTimeoutInWorkLoop = false; | | 60 | +export const enableTransitionEntanglement = false; | 60 | 61 |
| 61 | 62 | // Flow magic to verify the exports of this file match the original version. | 62 | 63 | // eslint-disable-next-line no-unused-vars | |
|