Repository: facebook/react
File: packages/react-devtools-shared/src/backend/fiber/renderer.js (JavaScript)

Code before:
        if (
          nextSuspendedBy === null ||
          (nextSuspendedBy.indexOf(asyncInfo) === -1 &&
            getAwaitInSuspendedByFromIO(nextSuspendedBy, asyncInfo.awaited) ===
              null)
        ) {
          // This IO entry is no longer blocking the current tree.
          // Let's remove it from the parent SuspenseNode.
          const ioInfo = asyncInfo.awaited;
          const suspendedBySet = parentSuspenseNode.suspendedBy.get(ioInfo);
          if (
            suspendedBySet === undefined ||
            !suspendedBySet.delete(instance)
          ) {
            throw new Error(
              'We are cleaning up async info that was not on the parent Suspense boundary. ' +
                'This is a bug in React.',
            );
          }
          if (suspendedBySet.size === 0) {
            parentSuspenseNode.suspendedBy.delete(asyncInfo.awaited);
          }
          if (
            parentSuspenseNode.hasUniqueSuspenders &&
            !ioExistsInSuspenseAncestor(parentSuspenseNode, ioInfo)
          ) {
            // This entry wasn't in any ancestor and is no longer in this suspense boundary.
            // This means that a child might now be the unique suspender for this IO.
            // Search the child boundaries to see if we can reveal any of them.
            unblockSuspendedBy(parentSuspenseNode, ioInfo);
          }
        }
      }
    }
  }

  function insertChild(instance: DevToolsInstance): void {
    const parentInstance = reconcilingParent;
    if (parentInstance === null) {
      // This instance is at the root.
      return;
    }
    // Place it in the parent.
    instance.parent = parentInstance;
    if (previouslyReconciledSibling === null) {
      previouslyReconciledSibling = instance;
      parentInstance.firstChild = instance;
    } else {
      previouslyReconciledSibling.nextSibling = instance;
      previouslyReconciledSibling = instance;
    }

Review comment (suggestion):
Ideally we'd check if `!suspendedBySet.delete(instance)` first before doing the loop so that in the common case when we do succeed in deleting it, there's no reason to check.

Code after:
        if (
          nextSuspendedBy === null ||
          (nextSuspendedBy.indexOf(asyncInfo) === -1 &&
            getAwaitInSuspendedByFromIO(nextSuspendedBy, asyncInfo.awaited) ===
              null)
        ) {
          // This IO entry is no longer blocking the current tree.
          // Let's remove it from the parent SuspenseNode.
          const ioInfo = asyncInfo.awaited;
          const suspendedBySet = parentSuspenseNode.suspendedBy.get(ioInfo);
          // A boundary can await the same IO multiple times.
          // We still want to error if we're trying to remove IO that isn't present on
          // this boundary so we need to check if we've already removed it.
          // We're assuming previousSuspendedBy is a small array so this should be faster
          // than allocating and maintaining a Set.
          let alreadyRemovedIO = false;
          for (let j = 0; j < i; j++) {
            const removedIOInfo = previousSuspendedBy[j].awaited;
            if (removedIOInfo === ioInfo) {
              alreadyRemovedIO = true;
              break;
            }
          }
          if (
            suspendedBySet === undefined ||
            (!alreadyRemovedIO && !suspendedBySet.delete(instance))
          ) {
            throw new Error(
              'We are cleaning up async info that was not on the parent Suspense boundary. ' +
                'This is a bug in React.',
            );
          }
          if (suspendedBySet.size === 0) {
            parentSuspenseNode.suspendedBy.delete(asyncInfo.awaited);
          }
          if (
            parentSuspenseNode.hasUniqueSuspenders &&
            !ioExistsInSuspenseAncestor(parentSuspenseNode, ioInfo)
          ) {
            // This entry wasn't in any ancestor and is no longer in this suspense boundary.
            // This means that a child might now be the unique suspender for this IO.
            // Search the child boundaries to see if we can reveal any of them.
            unblockSuspendedBy(parentSuspenseNode, ioInfo);
          }
        }
      }
    }
  }

  function insertChild(instance: DevToolsInstance): void {
    const parentInstance = reconcilingParent;