GeezerVison is showing github.com — Use Global Render Timeout for CPU Suspense (#19643) · react/react@d6e4338
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/ReactFiberBeginWork.new.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2644,7 +2644,6 @@ function initSuspenseListRenderState( | 2644 | 2644 | renderingStartTime: 0, | 2645 | 2645 | last: lastContentRow, | 2646 | 2646 | tail: tail, | 2647 | | - tailExpiration: 0, | 2648 | 2647 | tailMode: tailMode, | 2649 | 2648 | lastEffect: lastEffectBeforeRendering, | 2650 | 2649 | }: SuspenseListRenderState); | @@ -2655,7 +2654,6 @@ function initSuspenseListRenderState( | 2655 | 2654 | renderState.renderingStartTime = 0; | 2656 | 2655 | renderState.last = lastContentRow; | 2657 | 2656 | renderState.tail = tail; | 2658 | | - renderState.tailExpiration = 0; | 2659 | 2657 | renderState.tailMode = tailMode; | 2660 | 2658 | renderState.lastEffect = lastEffectBeforeRendering; | 2661 | 2659 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.old.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2635,7 +2635,6 @@ function initSuspenseListRenderState( | 2635 | 2635 | renderingStartTime: 0, | 2636 | 2636 | last: lastContentRow, | 2637 | 2637 | tail: tail, | 2638 | | - tailExpiration: 0, | 2639 | 2638 | tailMode: tailMode, | 2640 | 2639 | lastEffect: lastEffectBeforeRendering, | 2641 | 2640 | }: SuspenseListRenderState); | @@ -2646,7 +2645,6 @@ function initSuspenseListRenderState( | 2646 | 2645 | renderState.renderingStartTime = 0; | 2647 | 2646 | renderState.last = lastContentRow; | 2648 | 2647 | renderState.tail = tail; | 2649 | | - renderState.tailExpiration = 0; | 2650 | 2648 | renderState.tailMode = tailMode; | 2651 | 2649 | renderState.lastEffect = lastEffectBeforeRendering; | 2652 | 2650 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.new.js +31-18Lines changed: 31 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -137,9 +137,10 @@ import { | 137 | 137 | renderDidSuspendDelayIfPossible, | 138 | 138 | renderHasNotSuspendedYet, | 139 | 139 | popRenderLanes, | | 140 | + getRenderTargetTime, | 140 | 141 | } from './ReactFiberWorkLoop.new'; | 141 | 142 | import {createFundamentalStateInstance} from './ReactFiberFundamental.new'; | 142 | | -import {OffscreenLane} from './ReactFiberLane'; | | 143 | +import {OffscreenLane, SomeRetryLane} from './ReactFiberLane'; | 143 | 144 | import {resetChildFibers} from './ReactChildFiber.new'; | 144 | 145 | import {createScopeInstance} from './ReactFiberScope.new'; | 145 | 146 | import {transferActualDuration} from './ReactProfilerTimer.new'; | @@ -1076,6 +1077,29 @@ function completeWork( | 1076 | 1077 | row = row.sibling; | 1077 | 1078 | } | 1078 | 1079 | } | | 1080 | + | | 1081 | + if (renderState.tail !== null && now() > getRenderTargetTime()) { | | 1082 | + // We have already passed our CPU deadline but we still have rows | | 1083 | + // left in the tail. We'll just give up further attempts to render | | 1084 | + // the main content and only render fallbacks. | | 1085 | + workInProgress.effectTag |= DidCapture; | | 1086 | + didSuspendAlready = true; | | 1087 | + | | 1088 | + cutOffTailIfNeeded(renderState, false); | | 1089 | + | | 1090 | + // Since nothing actually suspended, there will nothing to ping this | | 1091 | + // to get it started back up to attempt the next item. While in terms | | 1092 | + // of priority this work has the same priority as this current render, | | 1093 | + // it's not part of the same transition once the transition has | | 1094 | + // committed. If it's sync, we still want to yield so that it can be | | 1095 | + // painted. Conceptually, this is really the same as pinging. | | 1096 | + // We can use any RetryLane even if it's the one currently rendering | | 1097 | + // since we're leaving it behind on this node. | | 1098 | + workInProgress.lanes = SomeRetryLane; | | 1099 | + if (enableSchedulerTracing) { | | 1100 | + markSpawnedWork(SomeRetryLane); | | 1101 | + } | | 1102 | + } | 1079 | 1103 | } else { | 1080 | 1104 | cutOffTailIfNeeded(renderState, false); | 1081 | 1105 | } | @@ -1117,10 +1141,11 @@ function completeWork( | 1117 | 1141 | return null; | 1118 | 1142 | } | 1119 | 1143 | } else if ( | 1120 | | - // The time it took to render last row is greater than time until | 1121 | | - // the expiration. | | 1144 | + // The time it took to render last row is greater than the remaining | | 1145 | + // time we have to render. So rendering one more row would likely | | 1146 | + // exceed it. | 1122 | 1147 | now() * 2 - renderState.renderingStartTime > | 1123 | | - renderState.tailExpiration && | | 1148 | + getRenderTargetTime() && | 1124 | 1149 | renderLanes !== OffscreenLane | 1125 | 1150 | ) { | 1126 | 1151 | // We have now passed our CPU deadline and we'll just give up further | @@ -1136,9 +1161,9 @@ function completeWork( | 1136 | 1161 | // them, then they really have the same priority as this render. | 1137 | 1162 | // So we'll pick it back up the very next render pass once we've had | 1138 | 1163 | // an opportunity to yield for paint. | 1139 | | - workInProgress.lanes = renderLanes; | | 1164 | + workInProgress.lanes = SomeRetryLane; | 1140 | 1165 | if (enableSchedulerTracing) { | 1141 | | - markSpawnedWork(renderLanes); | | 1166 | + markSpawnedWork(SomeRetryLane); | 1142 | 1167 | } | 1143 | 1168 | } | 1144 | 1169 | } | @@ -1163,18 +1188,6 @@ function completeWork( | 1163 | 1188 |
| 1164 | 1189 | if (renderState.tail !== null) { | 1165 | 1190 | // We still have tail rows to render. | 1166 | | - if (renderState.tailExpiration === 0) { | 1167 | | - // Heuristic for how long we're willing to spend rendering rows | 1168 | | - // until we just give up and show what we have so far. | 1169 | | - const TAIL_EXPIRATION_TIMEOUT_MS = 500; | 1170 | | - renderState.tailExpiration = now() + TAIL_EXPIRATION_TIMEOUT_MS; | 1171 | | - // TODO: This is meant to mimic the train model or JND but this | 1172 | | - // is a per component value. It should really be since the start | 1173 | | - // of the total render or last commit. Consider using something like | 1174 | | - // globalMostRecentFallbackTime. That doesn't account for being | 1175 | | - // suspended for part of the time or when it's a new render. | 1176 | | - // It should probably use a global start time value instead. | 1177 | | - } | 1178 | 1191 | // Pop a row. | 1179 | 1192 | const next = renderState.tail; | 1180 | 1193 | renderState.rendering = next; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.old.js +38-22Lines changed: 38 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -135,9 +135,10 @@ import { | 135 | 135 | renderDidSuspendDelayIfPossible, | 136 | 136 | renderHasNotSuspendedYet, | 137 | 137 | popRenderLanes, | | 138 | + getRenderTargetTime, | 138 | 139 | } from './ReactFiberWorkLoop.old'; | 139 | 140 | import {createFundamentalStateInstance} from './ReactFiberFundamental.old'; | 140 | | -import {OffscreenLane} from './ReactFiberLane'; | | 141 | +import {OffscreenLane, SomeRetryLane} from './ReactFiberLane'; | 141 | 142 | import {resetChildFibers} from './ReactChildFiber.old'; | 142 | 143 | import {createScopeInstance} from './ReactFiberScope.old'; | 143 | 144 | import {transferActualDuration} from './ReactProfilerTimer.old'; | @@ -1049,6 +1050,29 @@ function completeWork( | 1049 | 1050 | row = row.sibling; | 1050 | 1051 | } | 1051 | 1052 | } | | 1053 | + | | 1054 | + if (renderState.tail !== null && now() > getRenderTargetTime()) { | | 1055 | + // We have already passed our CPU deadline but we still have rows | | 1056 | + // left in the tail. We'll just give up further attempts to render | | 1057 | + // the main content and only render fallbacks. | | 1058 | + workInProgress.effectTag |= DidCapture; | | 1059 | + didSuspendAlready = true; | | 1060 | + | | 1061 | + cutOffTailIfNeeded(renderState, false); | | 1062 | + | | 1063 | + // Since nothing actually suspended, there will nothing to ping this | | 1064 | + // to get it started back up to attempt the next item. While in terms | | 1065 | + // of priority this work has the same priority as this current render, | | 1066 | + // it's not part of the same transition once the transition has | | 1067 | + // committed. If it's sync, we still want to yield so that it can be | | 1068 | + // painted. Conceptually, this is really the same as pinging. | | 1069 | + // We can use any RetryLane even if it's the one currently rendering | | 1070 | + // since we're leaving it behind on this node. | | 1071 | + workInProgress.lanes = SomeRetryLane; | | 1072 | + if (enableSchedulerTracing) { | | 1073 | + markSpawnedWork(SomeRetryLane); | | 1074 | + } | | 1075 | + } | 1052 | 1076 | } else { | 1053 | 1077 | cutOffTailIfNeeded(renderState, false); | 1054 | 1078 | } | @@ -1090,10 +1114,11 @@ function completeWork( | 1090 | 1114 | return null; | 1091 | 1115 | } | 1092 | 1116 | } else if ( | 1093 | | - // The time it took to render last row is greater than time until | 1094 | | - // the expiration. | | 1117 | + // The time it took to render last row is greater than the remaining | | 1118 | + // time we have to render. So rendering one more row would likely | | 1119 | + // exceed it. | 1095 | 1120 | now() * 2 - renderState.renderingStartTime > | 1096 | | - renderState.tailExpiration && | | 1121 | + getRenderTargetTime() && | 1097 | 1122 | renderLanes !== OffscreenLane | 1098 | 1123 | ) { | 1099 | 1124 | // We have now passed our CPU deadline and we'll just give up further | @@ -1105,13 +1130,16 @@ function completeWork( | 1105 | 1130 | cutOffTailIfNeeded(renderState, false); | 1106 | 1131 |
| 1107 | 1132 | // Since nothing actually suspended, there will nothing to ping this | 1108 | | - // to get it started back up to attempt the next item. If we can show | 1109 | | - // them, then they really have the same priority as this render. | 1110 | | - // So we'll pick it back up the very next render pass once we've had | 1111 | | - // an opportunity to yield for paint. | 1112 | | - workInProgress.lanes = renderLanes; | | 1133 | + // to get it started back up to attempt the next item. While in terms | | 1134 | + // of priority this work has the same priority as this current render, | | 1135 | + // it's not part of the same transition once the transition has | | 1136 | + // committed. If it's sync, we still want to yield so that it can be | | 1137 | + // painted. Conceptually, this is really the same as pinging. | | 1138 | + // We can use any RetryLane even if it's the one currently rendering | | 1139 | + // since we're leaving it behind on this node. | | 1140 | + workInProgress.lanes = SomeRetryLane; | 1113 | 1141 | if (enableSchedulerTracing) { | 1114 | | - markSpawnedWork(renderLanes); | | 1142 | + markSpawnedWork(SomeRetryLane); | 1115 | 1143 | } | 1116 | 1144 | } | 1117 | 1145 | } | @@ -1136,18 +1164,6 @@ function completeWork( | 1136 | 1164 |
| 1137 | 1165 | if (renderState.tail !== null) { | 1138 | 1166 | // We still have tail rows to render. | 1139 | | - if (renderState.tailExpiration === 0) { | 1140 | | - // Heuristic for how long we're willing to spend rendering rows | 1141 | | - // until we just give up and show what we have so far. | 1142 | | - const TAIL_EXPIRATION_TIMEOUT_MS = 500; | 1143 | | - renderState.tailExpiration = now() + TAIL_EXPIRATION_TIMEOUT_MS; | 1144 | | - // TODO: This is meant to mimic the train model or JND but this | 1145 | | - // is a per component value. It should really be since the start | 1146 | | - // of the total render or last commit. Consider using something like | 1147 | | - // globalMostRecentFallbackTime. That doesn't account for being | 1148 | | - // suspended for part of the time or when it's a new render. | 1149 | | - // It should probably use a global start time value instead. | 1150 | | - } | 1151 | 1167 | // Pop a row. | 1152 | 1168 | const next = renderState.tail; | 1153 | 1169 | renderState.rendering = next; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,8 @@ const TransitionLongLanes: Lanes = /* */ 0b0000000001111000000 | 97 | 97 |
| 98 | 98 | const RetryLanes: Lanes = /* */ 0b0000011110000000000000000000000; | 99 | 99 |
| | 100 | +export const SomeRetryLane: Lanes = /* */ 0b0000010000000000000000000000000; | | 101 | + | 100 | 102 | export const SelectiveHydrationLane: Lane = /* */ 0b0000100000000000000000000000000; | 101 | 103 |
| 102 | 104 | const NonIdleLanes = /* */ 0b0000111111111111111111111111111; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberSuspenseComponent.new.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,14 +41,12 @@ export type SuspenseListRenderState = {| | 41 | 41 | isBackwards: boolean, | 42 | 42 | // The currently rendering tail row. | 43 | 43 | rendering: null | Fiber, | 44 | | - // The absolute time when we started rendering the tail row. | | 44 | + // The absolute time when we started rendering the most recent tail row. | 45 | 45 | renderingStartTime: number, | 46 | 46 | // The last of the already rendered children. | 47 | 47 | last: null | Fiber, | 48 | 48 | // Remaining rows on the tail of the list. | 49 | 49 | tail: null | Fiber, | 50 | | - // The absolute time in ms that we'll expire the tail rendering. | 51 | | - tailExpiration: number, | 52 | 50 | // Tail insertions setting. | 53 | 51 | tailMode: SuspenseListTailMode, | 54 | 52 | // Last Effect before we rendered the "rendering" item. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberSuspenseComponent.old.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,14 +41,12 @@ export type SuspenseListRenderState = {| | 41 | 41 | isBackwards: boolean, | 42 | 42 | // The currently rendering tail row. | 43 | 43 | rendering: null | Fiber, | 44 | | - // The absolute time when we started rendering the tail row. | | 44 | + // The absolute time when we started rendering the most recent tail row. | 45 | 45 | renderingStartTime: number, | 46 | 46 | // The last of the already rendered children. | 47 | 47 | last: null | Fiber, | 48 | 48 | // Remaining rows on the tail of the list. | 49 | 49 | tail: null | Fiber, | 50 | | - // The absolute time in ms that we'll expire the tail rendering. | 51 | | - tailExpiration: number, | 52 | 50 | // Tail insertions setting. | 53 | 51 | tailMode: SuspenseListTailMode, | 54 | 52 | // Last Effect before we rendered the "rendering" item. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -328,6 +328,21 @@ let globalMostRecentFallbackTime: number = 0; | 328 | 328 | const FALLBACK_THROTTLE_MS: number = 500; | 329 | 329 | const DEFAULT_TIMEOUT_MS: number = 5000; | 330 | 330 |
| | 331 | +// The absolute time for when we should start giving up on rendering | | 332 | +// more and prefer CPU suspense heuristics instead. | | 333 | +let workInProgressRootRenderTargetTime: number = Infinity; | | 334 | +// How long a render is supposed to take before we start following CPU | | 335 | +// suspense heuristics and opt out of rendering more content. | | 336 | +const RENDER_TIMEOUT_MS = 500; | | 337 | + | | 338 | +function resetRenderTimer() { | | 339 | + workInProgressRootRenderTargetTime = now() + RENDER_TIMEOUT_MS; | | 340 | +} | | 341 | + | | 342 | +export function getRenderTargetTime(): number { | | 343 | + return workInProgressRootRenderTargetTime; | | 344 | +} | | 345 | + | 331 | 346 | let hasUncaughtError = false; | 332 | 347 | let firstUncaughtError = null; | 333 | 348 | let legacyErrorBoundariesThatAlreadyFailed: Set<mixed> | null = null; | @@ -603,6 +618,7 @@ export function scheduleUpdateOnFiber( | 603 | 618 | // scheduleCallbackForFiber to preserve the ability to schedule a callback | 604 | 619 | // without immediately flushing it. We only do this for user-initiated | 605 | 620 | // updates, to preserve historical behavior of legacy mode. | | 621 | + resetRenderTimer(); | 606 | 622 | flushSyncCallbackQueue(); | 607 | 623 | } | 608 | 624 | } | @@ -1111,6 +1127,7 @@ export function flushRoot(root: FiberRoot, lanes: Lanes) { | 1111 | 1127 | markRootExpired(root, lanes); | 1112 | 1128 | ensureRootIsScheduled(root, now()); | 1113 | 1129 | if ((executionContext & (RenderContext | CommitContext)) === NoContext) { | | 1130 | + resetRenderTimer(); | 1114 | 1131 | flushSyncCallbackQueue(); | 1115 | 1132 | } | 1116 | 1133 | } | @@ -1185,6 +1202,7 @@ export function batchedUpdates<A, R>(fn: A => R, a: A): R { | 1185 | 1202 | executionContext = prevExecutionContext; | 1186 | 1203 | if (executionContext === NoContext) { | 1187 | 1204 | // Flush the immediate callbacks that were scheduled during this batch | | 1205 | + resetRenderTimer(); | 1188 | 1206 | flushSyncCallbackQueue(); | 1189 | 1207 | } | 1190 | 1208 | } | @@ -1199,6 +1217,7 @@ export function batchedEventUpdates<A, R>(fn: A => R, a: A): R { | 1199 | 1217 | executionContext = prevExecutionContext; | 1200 | 1218 | if (executionContext === NoContext) { | 1201 | 1219 | // Flush the immediate callbacks that were scheduled during this batch | | 1220 | + resetRenderTimer(); | 1202 | 1221 | flushSyncCallbackQueue(); | 1203 | 1222 | } | 1204 | 1223 | } | @@ -1227,6 +1246,7 @@ export function discreteUpdates<A, B, C, D, R>( | 1227 | 1246 | executionContext = prevExecutionContext; | 1228 | 1247 | if (executionContext === NoContext) { | 1229 | 1248 | // Flush the immediate callbacks that were scheduled during this batch | | 1249 | + resetRenderTimer(); | 1230 | 1250 | flushSyncCallbackQueue(); | 1231 | 1251 | } | 1232 | 1252 | } | @@ -1240,6 +1260,7 @@ export function discreteUpdates<A, B, C, D, R>( | 1240 | 1260 | executionContext = prevExecutionContext; | 1241 | 1261 | if (executionContext === NoContext) { | 1242 | 1262 | // Flush the immediate callbacks that were scheduled during this batch | | 1263 | + resetRenderTimer(); | 1243 | 1264 | flushSyncCallbackQueue(); | 1244 | 1265 | } | 1245 | 1266 | } | @@ -1256,6 +1277,7 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R { | 1256 | 1277 | executionContext = prevExecutionContext; | 1257 | 1278 | if (executionContext === NoContext) { | 1258 | 1279 | // Flush the immediate callbacks that were scheduled during this batch | | 1280 | + resetRenderTimer(); | 1259 | 1281 | flushSyncCallbackQueue(); | 1260 | 1282 | } | 1261 | 1283 | } | @@ -1323,6 +1345,7 @@ export function flushControlled(fn: () => mixed): void { | 1323 | 1345 | executionContext = prevExecutionContext; | 1324 | 1346 | if (executionContext === NoContext) { | 1325 | 1347 | // Flush the immediate callbacks that were scheduled during this batch | | 1348 | + resetRenderTimer(); | 1326 | 1349 | flushSyncCallbackQueue(); | 1327 | 1350 | } | 1328 | 1351 | } | @@ -1333,6 +1356,7 @@ export function flushControlled(fn: () => mixed): void { | 1333 | 1356 | executionContext = prevExecutionContext; | 1334 | 1357 | if (executionContext === NoContext) { | 1335 | 1358 | // Flush the immediate callbacks that were scheduled during this batch | | 1359 | + resetRenderTimer(); | 1336 | 1360 | flushSyncCallbackQueue(); | 1337 | 1361 | } | 1338 | 1362 | } | @@ -1651,6 +1675,7 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) { | 1651 | 1675 | // If the root or lanes have changed, throw out the existing stack | 1652 | 1676 | // and prepare a fresh one. Otherwise we'll continue where we left off. | 1653 | 1677 | if (workInProgressRoot !== root || workInProgressRootRenderLanes !== lanes) { | | 1678 | + resetRenderTimer(); | 1654 | 1679 | prepareFreshStack(root, lanes); | 1655 | 1680 | startWorkOnPendingInteractions(root, lanes); | 1656 | 1681 | } | |
|