GeezerVison is showing github.com — Store interleaved updates on separate queue until end of render (#20615) · react/react@f15f8f6
File treeExpand file treeCollapse file tree Open diff view settings packages/react-reconciler/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberClassComponent.new.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -213,7 +213,7 @@ const classComponentUpdater = { | 213 | 213 | update.callback = callback; | 214 | 214 | } | 215 | 215 |
| 216 | | - enqueueUpdate(fiber, update); | | 216 | + enqueueUpdate(fiber, update, lane); | 217 | 217 | scheduleUpdateOnFiber(fiber, lane, eventTime); | 218 | 218 |
| 219 | 219 | if (__DEV__) { | @@ -245,7 +245,7 @@ const classComponentUpdater = { | 245 | 245 | update.callback = callback; | 246 | 246 | } | 247 | 247 |
| 248 | | - enqueueUpdate(fiber, update); | | 248 | + enqueueUpdate(fiber, update, lane); | 249 | 249 | scheduleUpdateOnFiber(fiber, lane, eventTime); | 250 | 250 |
| 251 | 251 | if (__DEV__) { | @@ -276,7 +276,7 @@ const classComponentUpdater = { | 276 | 276 | update.callback = callback; | 277 | 277 | } | 278 | 278 |
| 279 | | - enqueueUpdate(fiber, update); | | 279 | + enqueueUpdate(fiber, update, lane); | 280 | 280 | scheduleUpdateOnFiber(fiber, lane, eventTime); | 281 | 281 |
| 282 | 282 | if (__DEV__) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.new.js +59-13Lines changed: 59 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,6 +75,7 @@ import { | 75 | 75 | warnIfNotCurrentlyActingUpdatesInDev, | 76 | 76 | warnIfNotScopedWithMatchingAct, | 77 | 77 | markSkippedUpdateLanes, | | 78 | + isInterleavedUpdate, | 78 | 79 | } from './ReactFiberWorkLoop.new'; | 79 | 80 |
| 80 | 81 | import invariant from 'shared/invariant'; | @@ -104,6 +105,7 @@ import {logStateUpdateScheduled} from './DebugTracing'; | 104 | 105 | import {markStateUpdateScheduled} from './SchedulingProfiler'; | 105 | 106 | import {CacheContext} from './ReactFiberCacheComponent.new'; | 106 | 107 | import {createUpdate, enqueueUpdate} from './ReactUpdateQueue.new'; | | 108 | +import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.new'; | 107 | 109 |
| 108 | 110 | const {ReactCurrentDispatcher, ReactCurrentBatchConfig} = ReactSharedInternals; | 109 | 111 |
| @@ -116,8 +118,9 @@ type Update<S, A> = {| | 116 | 118 | priority?: ReactPriorityLevel, | 117 | 119 | |}; | 118 | 120 |
| 119 | | -type UpdateQueue<S, A> = {| | | 121 | +export type UpdateQueue<S, A> = {| | 120 | 122 | pending: Update<S, A> | null, | | 123 | + interleaved: Update<S, A> | null, | 121 | 124 | dispatch: (A => mixed) | null, | 122 | 125 | lastRenderedReducer: ((S, A) => S) | null, | 123 | 126 | lastRenderedState: S | null, | @@ -650,6 +653,7 @@ function mountReducer<S, I, A>( | 650 | 653 | hook.memoizedState = hook.baseState = initialState; | 651 | 654 | const queue = (hook.queue = { | 652 | 655 | pending: null, | | 656 | + interleaved: null, | 653 | 657 | dispatch: null, | 654 | 658 | lastRenderedReducer: reducer, | 655 | 659 | lastRenderedState: (initialState: any), | @@ -792,6 +796,23 @@ function updateReducer<S, I, A>( | 792 | 796 | queue.lastRenderedState = newState; | 793 | 797 | } | 794 | 798 |
| | 799 | + // Interleaved updates are stored on a separate queue. We aren't going to | | 800 | + // process them during this render, but we do need to track which lanes | | 801 | + // are remaining. | | 802 | + const lastInterleaved = queue.interleaved; | | 803 | + if (lastInterleaved !== null) { | | 804 | + let interleaved = lastInterleaved; | | 805 | + do { | | 806 | + const interleavedLane = interleaved.lane; | | 807 | + currentlyRenderingFiber.lanes = mergeLanes( | | 808 | + currentlyRenderingFiber.lanes, | | 809 | + interleavedLane, | | 810 | + ); | | 811 | + markSkippedUpdateLanes(interleavedLane); | | 812 | + interleaved = ((interleaved: any).next: Update<S, A>); | | 813 | + } while (interleaved !== lastInterleaved); | | 814 | + } | | 815 | + | 795 | 816 | const dispatch: Dispatch<A> = (queue.dispatch: any); | 796 | 817 | return [hook.memoizedState, dispatch]; | 797 | 818 | } | @@ -1080,6 +1101,7 @@ function useMutableSource<Source, Snapshot>( | 1080 | 1101 | // including any interleaving updates that occur. | 1081 | 1102 | const newQueue = { | 1082 | 1103 | pending: null, | | 1104 | + interleaved: null, | 1083 | 1105 | dispatch: null, | 1084 | 1106 | lastRenderedReducer: basicStateReducer, | 1085 | 1107 | lastRenderedState: snapshot, | @@ -1135,6 +1157,7 @@ function mountState<S>( | 1135 | 1157 | hook.memoizedState = hook.baseState = initialState; | 1136 | 1158 | const queue = (hook.queue = { | 1137 | 1159 | pending: null, | | 1160 | + interleaved: null, | 1138 | 1161 | dispatch: null, | 1139 | 1162 | lastRenderedReducer: basicStateReducer, | 1140 | 1163 | lastRenderedState: (initialState: any), | @@ -1812,7 +1835,7 @@ function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T) { | 1812 | 1835 | cache: seededCache, | 1813 | 1836 | }; | 1814 | 1837 | refreshUpdate.payload = payload; | 1815 | | - enqueueUpdate(provider, refreshUpdate); | | 1838 | + enqueueUpdate(provider, refreshUpdate, lane); | 1816 | 1839 | return; | 1817 | 1840 | } | 1818 | 1841 | } | @@ -1847,17 +1870,6 @@ function dispatchAction<S, A>( | 1847 | 1870 | next: (null: any), | 1848 | 1871 | }; | 1849 | 1872 |
| 1850 | | - // Append the update to the end of the list. | 1851 | | - const pending = queue.pending; | 1852 | | - if (pending === null) { | 1853 | | - // This is the first update. Create a circular list. | 1854 | | - update.next = update; | 1855 | | - } else { | 1856 | | - update.next = pending.next; | 1857 | | - pending.next = update; | 1858 | | - } | 1859 | | - queue.pending = update; | 1860 | | - | 1861 | 1873 | const alternate = fiber.alternate; | 1862 | 1874 | if ( | 1863 | 1875 | fiber === currentlyRenderingFiber || | @@ -1867,7 +1879,41 @@ function dispatchAction<S, A>( | 1867 | 1879 | // queue -> linked list of updates. After this render pass, we'll restart | 1868 | 1880 | // and apply the stashed updates on top of the work-in-progress hook. | 1869 | 1881 | didScheduleRenderPhaseUpdateDuringThisPass = didScheduleRenderPhaseUpdate = true; | | 1882 | + const pending = queue.pending; | | 1883 | + if (pending === null) { | | 1884 | + // This is the first update. Create a circular list. | | 1885 | + update.next = update; | | 1886 | + } else { | | 1887 | + update.next = pending.next; | | 1888 | + pending.next = update; | | 1889 | + } | | 1890 | + queue.pending = update; | 1870 | 1891 | } else { | | 1892 | + if (isInterleavedUpdate(fiber, lane)) { | | 1893 | + const interleaved = queue.interleaved; | | 1894 | + if (interleaved === null) { | | 1895 | + // This is the first update. Create a circular list. | | 1896 | + update.next = update; | | 1897 | + // At the end of the current render, this queue's interleaved updates will | | 1898 | + // be transfered to the pending queue. | | 1899 | + pushInterleavedQueue(queue); | | 1900 | + } else { | | 1901 | + update.next = interleaved.next; | | 1902 | + interleaved.next = update; | | 1903 | + } | | 1904 | + queue.interleaved = update; | | 1905 | + } else { | | 1906 | + const pending = queue.pending; | | 1907 | + if (pending === null) { | | 1908 | + // This is the first update. Create a circular list. | | 1909 | + update.next = update; | | 1910 | + } else { | | 1911 | + update.next = pending.next; | | 1912 | + pending.next = update; | | 1913 | + } | | 1914 | + queue.pending = update; | | 1915 | + } | | 1916 | + | 1871 | 1917 | if ( | 1872 | 1918 | fiber.lanes === NoLanes && | 1873 | 1919 | (alternate === null || alternate.lanes === NoLanes) | |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Facebook, Inc. and its affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +import type {UpdateQueue as HookQueue} from './ReactFiberHooks.new'; | | 11 | +import type {SharedQueue as ClassQueue} from './ReactUpdateQueue.new'; | | 12 | + | | 13 | +// An array of all update queues that received updates during the current | | 14 | +// render. When this render exits, either because it finishes or because it is | | 15 | +// interrupted, the interleaved updates will be transfered onto the main part | | 16 | +// of the queue. | | 17 | +let interleavedQueues: Array< | | 18 | + HookQueue<any, any> | ClassQueue<any>, | | 19 | +> | null = null; | | 20 | + | | 21 | +export function pushInterleavedQueue( | | 22 | + queue: HookQueue<any, any> | ClassQueue<any>, | | 23 | +) { | | 24 | + if (interleavedQueues === null) { | | 25 | + interleavedQueues = [queue]; | | 26 | + } else { | | 27 | + interleavedQueues.push(queue); | | 28 | + } | | 29 | +} | | 30 | + | | 31 | +export function enqueueInterleavedUpdates() { | | 32 | + // Transfer the interleaved updates onto the main queue. Each queue has a | | 33 | + // `pending` field and an `interleaved` field. When they are not null, they | | 34 | + // point to the last node in a circular linked list. We need to append the | | 35 | + // interleaved list to the end of the pending list by joining them into a | | 36 | + // single, circular list. | | 37 | + if (interleavedQueues !== null) { | | 38 | + for (let i = 0; i < interleavedQueues.length; i++) { | | 39 | + const queue = interleavedQueues[i]; | | 40 | + const lastInterleavedUpdate = queue.interleaved; | | 41 | + if (lastInterleavedUpdate !== null) { | | 42 | + queue.interleaved = null; | | 43 | + const firstInterleavedUpdate = lastInterleavedUpdate.next; | | 44 | + const lastPendingUpdate = queue.pending; | | 45 | + if (lastPendingUpdate !== null) { | | 46 | + const firstPendingUpdate = lastPendingUpdate.next; | | 47 | + lastPendingUpdate.next = (firstInterleavedUpdate: any); | | 48 | + lastInterleavedUpdate.next = (firstPendingUpdate: any); | | 49 | + } | | 50 | + queue.pending = (lastInterleavedUpdate: any); | | 51 | + } | | 52 | + } | | 53 | + interleavedQueues = null; | | 54 | + } | | 55 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberNewContext.new.js +21-6Lines changed: 21 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import type {ReactContext} from 'shared/ReactTypes'; | 11 | 11 | import type {Fiber, ContextDependency} from './ReactInternalTypes'; | 12 | 12 | import type {StackCursor} from './ReactFiberStack.new'; | 13 | 13 | import type {Lanes} from './ReactFiberLane.new'; | | 14 | +import type {SharedQueue} from './ReactUpdateQueue.new'; | 14 | 15 |
| 15 | 16 | import {isPrimaryRenderer} from './ReactFiberHostConfig'; | 16 | 17 | import {createCursor, push, pop} from './ReactFiberStack.new'; | @@ -31,7 +32,7 @@ import { | 31 | 32 |
| 32 | 33 | import invariant from 'shared/invariant'; | 33 | 34 | import is from 'shared/objectIs'; | 34 | | -import {createUpdate, enqueueUpdate, ForceUpdate} from './ReactUpdateQueue.new'; | | 35 | +import {createUpdate, ForceUpdate} from './ReactUpdateQueue.new'; | 35 | 36 | import {markWorkInProgressReceivedUpdate} from './ReactFiberBeginWork.new'; | 36 | 37 | import {enableSuspenseServerRenderer} from 'shared/ReactFeatureFlags'; | 37 | 38 |
| @@ -211,16 +212,30 @@ export function propagateContextChange<T>( | 211 | 212 |
| 212 | 213 | if (fiber.tag === ClassComponent) { | 213 | 214 | // Schedule a force update on the work-in-progress. | 214 | | - const update = createUpdate( | 215 | | - NoTimestamp, | 216 | | - pickArbitraryLane(renderLanes), | 217 | | - ); | | 215 | + const lane = pickArbitraryLane(renderLanes); | | 216 | + const update = createUpdate(NoTimestamp, lane); | 218 | 217 | update.tag = ForceUpdate; | 219 | 218 | // TODO: Because we don't have a work-in-progress, this will add the | 220 | 219 | // update to the current fiber, too, which means it will persist even if | 221 | 220 | // this render is thrown away. Since it's a race condition, not sure it's | 222 | 221 | // worth fixing. | 223 | | - enqueueUpdate(fiber, update); | | 222 | + | | 223 | + // Inlined `enqueueUpdate` to remove interleaved update check | | 224 | + const updateQueue = fiber.updateQueue; | | 225 | + if (updateQueue === null) { | | 226 | + // Only occurs if the fiber has been unmounted. | | 227 | + } else { | | 228 | + const sharedQueue: SharedQueue<any> = (updateQueue: any).shared; | | 229 | + const pending = sharedQueue.pending; | | 230 | + if (pending === null) { | | 231 | + // This is the first update. Create a circular list. | | 232 | + update.next = update; | | 233 | + } else { | | 234 | + update.next = pending.next; | | 235 | + pending.next = update; | | 236 | + } | | 237 | + sharedQueue.pending = update; | | 238 | + } | 224 | 239 | } | 225 | 240 | fiber.lanes = mergeLanes(fiber.lanes, renderLanes); | 226 | 241 | const alternate = fiber.alternate; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.new.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -314,7 +314,7 @@ export function updateContainer( | 314 | 314 | update.callback = callback; | 315 | 315 | } | 316 | 316 |
| 317 | | - enqueueUpdate(current, update); | | 317 | + enqueueUpdate(current, update, lane); | 318 | 318 | scheduleUpdateOnFiber(current, lane, eventTime); | 319 | 319 |
| 320 | 320 | return lane; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberThrow.new.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -293,7 +293,7 @@ function throwException( | 293 | 293 | // prevent a bail out. | 294 | 294 | const update = createUpdate(NoTimestamp, SyncLane); | 295 | 295 | update.tag = ForceUpdate; | 296 | | - enqueueUpdate(sourceFiber, update); | | 296 | + enqueueUpdate(sourceFiber, update, SyncLane); | 297 | 297 | } | 298 | 298 | } | 299 | 299 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -204,6 +204,7 @@ import { | 204 | 204 | pop as popFromStack, | 205 | 205 | createCursor, | 206 | 206 | } from './ReactFiberStack.new'; | | 207 | +import {enqueueInterleavedUpdates} from './ReactFiberInterleavedUpdates.new'; | 207 | 208 |
| 208 | 209 | import { | 209 | 210 | markNestedUpdateScheduled, | @@ -534,6 +535,7 @@ export function scheduleUpdateOnFiber( | 534 | 535 | } | 535 | 536 | } | 536 | 537 |
| | 538 | + // TODO: Consolidate with `isInterleavedUpdate` check | 537 | 539 | if (root === workInProgressRoot) { | 538 | 540 | // Received an update to a tree that's in the middle of rendering. Mark | 539 | 541 | // that there was an interleaved update work on this root. Unless the | @@ -671,6 +673,22 @@ function markUpdateLaneFromFiberToRoot( | 671 | 673 | } | 672 | 674 | } | 673 | 675 |
| | 676 | +export function isInterleavedUpdate(fiber: Fiber, lane: Lane) { | | 677 | + return ( | | 678 | + // TODO: Optimize slightly by comparing to root that fiber belongs to. | | 679 | + // Requires some refactoring. Not a big deal though since it's rare for | | 680 | + // concurrent apps to have more than a single root. | | 681 | + workInProgressRoot !== null && | | 682 | + (fiber.mode & BlockingMode) !== NoMode && | | 683 | + // If this is a render phase update (i.e. UNSAFE_componentWillReceiveProps), | | 684 | + // then don't treat this as an interleaved update. This pattern is | | 685 | + // accompanied by a warning but we haven't fully deprecated it yet. We can | | 686 | + // remove once the deferRenderPhaseUpdateToNextBatch flag is enabled. | | 687 | + (deferRenderPhaseUpdateToNextBatch || | | 688 | + (executionContext & RenderContext) === NoContext) | | 689 | + ); | | 690 | +} | | 691 | + | 674 | 692 | // Use this function to schedule a task for a root. There's only one task per | 675 | 693 | // root; if a task was already scheduled, we'll check to make sure the priority | 676 | 694 | // of the existing task is the same as the priority of the next level that the | @@ -1352,6 +1370,8 @@ function prepareFreshStack(root: FiberRoot, lanes: Lanes) { | 1352 | 1370 | workInProgressRootUpdatedLanes = NoLanes; | 1353 | 1371 | workInProgressRootPingedLanes = NoLanes; | 1354 | 1372 |
| | 1373 | + enqueueInterleavedUpdates(); | | 1374 | + | 1355 | 1375 | if (enableSchedulerTracing) { | 1356 | 1376 | spawnedWorkDuringRender = null; | 1357 | 1377 | } | @@ -2282,7 +2302,7 @@ function captureCommitPhaseErrorOnRoot( | 2282 | 2302 | ) { | 2283 | 2303 | const errorInfo = createCapturedValue(error, sourceFiber); | 2284 | 2304 | const update = createRootErrorUpdate(rootFiber, errorInfo, (SyncLane: Lane)); | 2285 | | - enqueueUpdate(rootFiber, update); | | 2305 | + enqueueUpdate(rootFiber, update, (SyncLane: Lane)); | 2286 | 2306 | const eventTime = requestEventTime(); | 2287 | 2307 | const root = markUpdateLaneFromFiberToRoot(rootFiber, (SyncLane: Lane)); | 2288 | 2308 | if (root !== null) { | @@ -2319,7 +2339,7 @@ export function captureCommitPhaseError(sourceFiber: Fiber, error: mixed) { | 2319 | 2339 | errorInfo, | 2320 | 2340 | (SyncLane: Lane), | 2321 | 2341 | ); | 2322 | | - enqueueUpdate(fiber, update); | | 2342 | + enqueueUpdate(fiber, update, (SyncLane: Lane)); | 2323 | 2343 | const eventTime = requestEventTime(); | 2324 | 2344 | const root = markUpdateLaneFromFiberToRoot(fiber, (SyncLane: Lane)); | 2325 | 2345 | if (root !== null) { | |
|