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

Code before:
        continue;
      }
      while (node.nextSibling === null) {
        if (node.parent === null || node.parent === parentSuspenseNode) {
          return;
        }
        node = node.parent;
      }
      node = node.nextSibling;
    }
  }

  function removePreviousSuspendedBy(
    instance: DevToolsInstance,
    previousSuspendedBy: null | Array<ReactAsyncInfo>,
    parentSuspenseNode: null | SuspenseNode,
  ): void {
    // Remove any async info from the parent, if they were in the previous set but
    // is no longer in the new set.
    if (previousSuspendedBy !== null && parentSuspenseNode !== null) {
      const nextSuspendedBy = instance.suspendedBy;
      // 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. We're tracking the IO we already removed so that we can error
      // every time a delete fails.
      const removedIOInfos = new Set<ReactIOInfo>();
      for (let i = 0; i < previousSuspendedBy.length; i++) {
        const asyncInfo = previousSuspendedBy[i];
        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 ||
            (!removedIOInfos.has(ioInfo) && !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.',
            );
          }
          removedIOInfos.add(ioInfo);
          if (suspendedBySet.size === 0) {
            parentSuspenseNode.suspendedBy.delete(asyncInfo.awaited);
          }

Review comment (suggestion):
I don't think we want to allocate memory in this pass since it can happen at every level when there's no change.

Code after:
        continue;
      }
      while (node.nextSibling === null) {
        if (node.parent === null || node.parent === parentSuspenseNode) {
          return;
        }
        node = node.parent;
      }
      node = node.nextSibling;
    }
  }

  function removePreviousSuspendedBy(
    instance: DevToolsInstance,
    previousSuspendedBy: null | Array<ReactAsyncInfo>,
    parentSuspenseNode: null | SuspenseNode,
  ): void {
    // Remove any async info from the parent, if they were in the previous set but
    // is no longer in the new set.
    if (previousSuspendedBy !== null && parentSuspenseNode !== null) {
      const nextSuspendedBy = instance.suspendedBy;
      for (let i = 0; i < previousSuspendedBy.length; i++) {
        const asyncInfo = previousSuspendedBy[i];
        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(