Skip to content

Commit 4f93170

Browse files
authored
[Flight] Cache the value if we visit the same I/O or Promise multiple times along different paths (facebook#35005)
We avoid visiting the same async node twice but if we see it again we returned "null" indicating that there's no I/O there. This means that if you have two different Promises both resolving from the same I/O node then we only show one of them. However, in general we treat that as two different I/O entries to allow for things like batching to still show up separately. This fixes that by caching the return value for multiple visits. So if we found I/O (but no user space await) in one path and then we visit that path through a different Promise chain, then we'll still emit it twice. However, if we visit the same exact Promise that we emitted an await on then we skip it. Because there's no need to emit two awaits on the same thing. It only matters when the path ends up informing whether it has I/O or not.
1 parent 0fa3250 commit 4f93170

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

packages/react-server/src/ReactFlightServer.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,15 +2316,37 @@ function visitAsyncNode(
23162316
request: Request,
23172317
task: Task,
23182318
node: AsyncSequence,
2319-
visited: Set<AsyncSequence | ReactDebugInfo>,
2319+
visited: Map<
2320+
AsyncSequence | ReactDebugInfo,
2321+
void | null | PromiseNode | IONode,
2322+
>,
23202323
cutOff: number,
23212324
): void | null | PromiseNode | IONode {
23222325
if (visited.has(node)) {
23232326
// It's possible to visit them same node twice when it's part of both an "awaited" path
23242327
// and a "previous" path. This also gracefully handles cycles which would be a bug.
2325-
return null;
2328+
return visited.get(node);
2329+
}
2330+
// Set it as visited early in case we see ourselves before returning.
2331+
visited.set(node, null);
2332+
const result = visitAsyncNodeImpl(request, task, node, visited, cutOff);
2333+
if (result !== null) {
2334+
// If we ended up with a value, let's use that value for future visits.
2335+
visited.set(node, result);
23262336
}
2327-
visited.add(node);
2337+
return result;
2338+
}
2339+
2340+
function visitAsyncNodeImpl(
2341+
request: Request,
2342+
task: Task,
2343+
node: AsyncSequence,
2344+
visited: Map<
2345+
AsyncSequence | ReactDebugInfo,
2346+
void | null | PromiseNode | IONode,
2347+
>,
2348+
cutOff: number,
2349+
): void | null | PromiseNode | IONode {
23282350
if (node.end >= 0 && node.end <= request.timeOrigin) {
23292351
// This was already resolved when we started this render. It must have been either something
23302352
// that's part of a start up sequence or externally cached data. We exclude that information.
@@ -2416,7 +2438,7 @@ function visitAsyncNode(
24162438
if (promise !== undefined) {
24172439
const debugInfo = promise._debugInfo;
24182440
if (debugInfo != null && !visited.has(debugInfo)) {
2419-
visited.add(debugInfo);
2441+
visited.set(debugInfo, null);
24202442
forwardDebugInfo(request, task, debugInfo);
24212443
}
24222444
}
@@ -2483,6 +2505,10 @@ function visitAsyncNode(
24832505
// Promise that was ultimately awaited by the user space await.
24842506
serializeIONode(request, ioNode, awaited.promise);
24852507

2508+
// If we ever visit this I/O node again, skip it because we already emitted this
2509+
// exact entry and we don't need two awaits on the same thing.
2510+
visited.set(ioNode, null);
2511+
24862512
// Ensure the owner is already outlined.
24872513
if (node.owner != null) {
24882514
outlineComponentInfo(request, node.owner);
@@ -2521,7 +2547,7 @@ function visitAsyncNode(
25212547
if (promise !== undefined) {
25222548
const debugInfo = promise._debugInfo;
25232549
if (debugInfo != null && !visited.has(debugInfo)) {
2524-
visited.add(debugInfo);
2550+
visited.set(debugInfo, null);
25252551
forwardDebugInfo(request, task, debugInfo);
25262552
}
25272553
}
@@ -2542,9 +2568,12 @@ function emitAsyncSequence(
25422568
owner: null | ReactComponentInfo,
25432569
stack: null | Error,
25442570
): void {
2545-
const visited: Set<AsyncSequence | ReactDebugInfo> = new Set();
2571+
const visited: Map<
2572+
AsyncSequence | ReactDebugInfo,
2573+
void | null | PromiseNode | IONode,
2574+
> = new Map();
25462575
if (__DEV__ && alreadyForwardedDebugInfo) {
2547-
visited.add(alreadyForwardedDebugInfo);
2576+
visited.set(alreadyForwardedDebugInfo, null);
25482577
}
25492578
const awaitedNode = visitAsyncNode(request, task, node, visited, task.time);
25502579
if (awaitedNode === undefined) {

0 commit comments

Comments
 (0)