Skip to content

Commit 86e74c8

Browse files
committed
Check if suspensey instance resolves in immediate task (#26427)
When rendering a suspensey resource that we haven't seen before, it may have loaded in the background while we were rendering. We should yield to the main thread to see if the load event fires in an immediate task. For example, if the resource for a link element has already loaded, its load event will fire in a task right after React yields to the main thread. Because the continuation task is not scheduled until right before React yields, the load event will ping React before it resumes. If this happens, we can resume rendering without showing a fallback. I don't think this matters much for images, because the `completed` property tells us whether the image has loaded, and during a non-urgent render, we never block the main thread for more than 5ms at a time (for now — we might increase this in the future). It matters more for stylesheets because the only way to check if it has loaded is by listening for the load event. This is essentially the same trick that `use` does for userspace promises, but a bit simpler because we don't need to replay the host component's begin phase; the work-in-progress fiber already completed, so we can just continue onto the next sibling without any additional work. As part of this change, I split the `shouldSuspendCommit` host config method into separate `maySuspendCommit` and `preloadInstance` methods. Previously `shouldSuspendCommit` was used for both. This raised a question of whether we should preload resources during a synchronous render. My initial instinct was that we shouldn't, because we're going to synchronously block the main thread until the resource is inserted into the DOM, anyway. But I wonder if the browser is able to initiate the preload even while the main thread is blocked. It's probably a micro-optimization either way because most resources will be loaded during transitions, not urgent renders. DiffTrain build for commit 0131d0c.
1 parent eee3aa6 commit 86e74c8

File tree

12 files changed

+836
-360
lines changed

12 files changed

+836
-360
lines changed

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-dev.js

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,10 @@ function unhideInstance(instance, props) {
20122012
function unhideTextInstance(textInstance, text) {
20132013
textInstance.isHidden = false;
20142014
}
2015+
function preloadInstance(type, props) {
2016+
// Return true to indicate it's already loaded
2017+
return true;
2018+
}
20152019
function waitForCommitToBeReady() {
20162020
return null;
20172021
}
@@ -4175,6 +4179,10 @@ var SuspenseException = new Error(
41754179
"unexpected behavior.\n\n" +
41764180
"To handle async errors, wrap your component in an error boundary, or " +
41774181
"call the promise's `.catch` method and pass the result to `use`"
4182+
);
4183+
var SuspenseyCommitException = new Error(
4184+
"Suspense Exception: This is not a real error, and should not leak into " +
4185+
"userspace. If you're seeing this, it's likely a bug in React."
41784186
); // This is a noop thenable that we use to trigger a fallback in throwException.
41794187
// TODO: It would be better to refactor throwException into multiple functions
41804188
// so we can trigger a fallback directly without having to check the type. But
@@ -14545,7 +14553,6 @@ function updateHostComponent(
1454514553
// we won't touch this node even if children changed.
1454614554
return;
1454714555
} // If we get updated because one of our children updated, we don't
14548-
suspendHostCommitIfNeeded(workInProgress);
1454914556
getHostContext(); // TODO: Experiencing an error where oldProps is null. Suggests a host
1455014557
// component is hitting the resume path. Figure out why. Possibly
1455114558
// related to `hidden`.
@@ -14564,12 +14571,17 @@ function updateHostComponent(
1456414571
// that suspend don't have children, so it doesn't matter. But that might not
1456514572
// always be true in the future.
1456614573

14567-
function suspendHostCommitIfNeeded(workInProgress, type, props, renderLanes) {
14574+
function preloadInstanceAndSuspendIfNeeded(
14575+
workInProgress,
14576+
type,
14577+
props,
14578+
renderLanes
14579+
) {
1456814580
// Ask the renderer if this instance should suspend the commit.
1456914581
{
1457014582
// If this flag was set previously, we can remove it. The flag represents
1457114583
// whether this particular set of props might ever need to suspend. The
14572-
// safest thing to do is for shouldSuspendCommit to always return true, but
14584+
// safest thing to do is for maySuspendCommit to always return true, but
1457314585
// if the renderer is reasonably confident that the underlying resource
1457414586
// won't be evicted, it can return false as a performance optimization.
1457514587
workInProgress.flags &= ~SuspenseyCommit;
@@ -15031,15 +15043,18 @@ function completeWork(current, workInProgress, renderLanes) {
1503115043
workInProgress.stateNode = _instance3; // Certain renderers require commit-time effects for initial mount.
1503215044
}
1503315045

15034-
suspendHostCommitIfNeeded(workInProgress);
15035-
1503615046
if (workInProgress.ref !== null) {
1503715047
// If there is a ref on a host node we need to schedule a callback
1503815048
markRef(workInProgress);
1503915049
}
1504015050
}
1504115051

15042-
bubbleProperties(workInProgress);
15052+
bubbleProperties(workInProgress); // This must come at the very end of the complete phase, because it might
15053+
// throw to suspend, and if the resource immediately loads, the work loop
15054+
// will resume rendering as if the work-in-progress completed. So it must
15055+
// fully complete.
15056+
15057+
preloadInstanceAndSuspendIfNeeded(workInProgress);
1504315058
return null;
1504415059
}
1504515060

@@ -19422,9 +19437,11 @@ var NotSuspended = 0;
1942219437
var SuspendedOnError = 1;
1942319438
var SuspendedOnData = 2;
1942419439
var SuspendedOnImmediate = 3;
19425-
var SuspendedOnDeprecatedThrowPromise = 4;
19426-
var SuspendedAndReadyToContinue = 5;
19427-
var SuspendedOnHydration = 6; // When this is true, the work-in-progress fiber just suspended (or errored) and
19440+
var SuspendedOnInstance = 4;
19441+
var SuspendedOnInstanceAndReadyToContinue = 5;
19442+
var SuspendedOnDeprecatedThrowPromise = 6;
19443+
var SuspendedAndReadyToContinue = 7;
19444+
var SuspendedOnHydration = 8; // When this is true, the work-in-progress fiber just suspended (or errored) and
1942819445
// we've yet to unwind the stack. In some cases, we may yield to the main thread
1942919446
// after this happens. If the fiber is pinged before we resume, we can retry
1943019447
// immediately instead of unwinding the stack.
@@ -20592,6 +20609,9 @@ function handleThrow(root, thrownValue) {
2059220609
: // immediately resolved (i.e. in a microtask). Otherwise, trigger the
2059320610
// nearest Suspense fallback.
2059420611
SuspendedOnImmediate;
20612+
} else if (thrownValue === SuspenseyCommitException) {
20613+
thrownValue = getSuspendedThenable();
20614+
workInProgressSuspendedReason = SuspendedOnInstance;
2059520615
} else if (thrownValue === SelectiveHydrationException) {
2059620616
// An update flowed into a dehydrated boundary. Before we can apply the
2059720617
// update, we need to finish hydrating. Interrupt the work-in-progress
@@ -20872,7 +20892,7 @@ function renderRootConcurrent(root, lanes) {
2087220892
var unitOfWork = workInProgress;
2087320893
var thrownValue = workInProgressThrownValue;
2087420894

20875-
switch (workInProgressSuspendedReason) {
20895+
resumeOrUnwind: switch (workInProgressSuspendedReason) {
2087620896
case SuspendedOnError: {
2087720897
// Unwind then continue with the normal work loop.
2087820898
workInProgressSuspendedReason = NotSuspended;
@@ -20924,6 +20944,12 @@ function renderRootConcurrent(root, lanes) {
2092420944
break outer;
2092520945
}
2092620946

20947+
case SuspendedOnInstance: {
20948+
workInProgressSuspendedReason =
20949+
SuspendedOnInstanceAndReadyToContinue;
20950+
break outer;
20951+
}
20952+
2092720953
case SuspendedAndReadyToContinue: {
2092820954
var _thenable = thrownValue;
2092920955

@@ -20942,6 +20968,69 @@ function renderRootConcurrent(root, lanes) {
2094220968
break;
2094320969
}
2094420970

20971+
case SuspendedOnInstanceAndReadyToContinue: {
20972+
switch (workInProgress.tag) {
20973+
case HostComponent:
20974+
case HostHoistable:
20975+
case HostSingleton: {
20976+
// Before unwinding the stack, check one more time if the
20977+
// instance is ready. It may have loaded when React yielded to
20978+
// the main thread.
20979+
// Assigning this to a constant so Flow knows the binding won't
20980+
// be mutated by `preloadInstance`.
20981+
var hostFiber = workInProgress;
20982+
var type = hostFiber.type;
20983+
var props = hostFiber.pendingProps;
20984+
var isReady = preloadInstance(type, props);
20985+
20986+
if (isReady) {
20987+
// The data resolved. Resume the work loop as if nothing
20988+
// suspended. Unlike when a user component suspends, we don't
20989+
// have to replay anything because the host fiber
20990+
// already completed.
20991+
workInProgressSuspendedReason = NotSuspended;
20992+
workInProgressThrownValue = null;
20993+
var sibling = hostFiber.sibling;
20994+
20995+
if (sibling !== null) {
20996+
workInProgress = sibling;
20997+
} else {
20998+
var returnFiber = hostFiber.return;
20999+
21000+
if (returnFiber !== null) {
21001+
workInProgress = returnFiber;
21002+
completeUnitOfWork(returnFiber);
21003+
} else {
21004+
workInProgress = null;
21005+
}
21006+
}
21007+
21008+
break resumeOrUnwind;
21009+
}
21010+
21011+
break;
21012+
}
21013+
21014+
default: {
21015+
// This will fail gracefully but it's not correct, so log a
21016+
// warning in dev.
21017+
if (true) {
21018+
error(
21019+
"Unexpected type of fiber triggered a suspensey commit. " +
21020+
"This is a bug in React."
21021+
);
21022+
}
21023+
21024+
break;
21025+
}
21026+
} // Otherwise, unwind then continue with the normal work loop.
21027+
21028+
workInProgressSuspendedReason = NotSuspended;
21029+
workInProgressThrownValue = null;
21030+
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
21031+
break;
21032+
}
21033+
2094521034
case SuspendedOnDeprecatedThrowPromise: {
2094621035
// Suspended by an old implementation that uses the `throw promise`
2094721036
// pattern. The newer replaying behavior can cause subtle issues
@@ -23499,7 +23588,7 @@ function createFiberRoot(
2349923588
return root;
2350023589
}
2350123590

23502-
var ReactVersion = "18.3.0-next-3554c8852-20230320";
23591+
var ReactVersion = "18.3.0-next-0131d0cff-20230320";
2350323592

2350423593
// Might add PROFILE later.
2350523594

0 commit comments

Comments
 (0)