facebook / react

The library for web and native user interfaces.
https://react.dev
MIT License
228.58k stars 46.79k forks source link

Why not use an array to save fibers with side effects, but use a linked list on the finishedWork tree #24770

Closed lizuncong closed 6 months ago

lizuncong commented 2 years ago

Hello,i'm curious why React17 uses a linked list on finishedWork tree instead of using an array to hold fiber nodes with side effects. It seems that it is easier to understand with arrays. Thank you for your answer

let effectFiberList = []; // an array to hold fiber nodes with side effects

// in the deleteChild function, we can add the childToDelete directly to the effectFiberList
function ChildReconciler(shouldTrackSideEffects) {
  function deleteChild(returnFiber, childToDelete) {
    if (!shouldTrackSideEffects) {
      // Noop.
      return;
    }
    //  var last = returnFiber.lastEffect;
    //  if (last !== null) {
    //    last.nextEffect = childToDelete;
    //    returnFiber.lastEffect = childToDelete;
    //  } else {
    //    returnFiber.firstEffect = returnFiber.lastEffect = childToDelete;
    //  }
    effectFiberList.push(childToDelete);
    // childToDelete.nextEffect = null;
    childToDelete.flags = Deletion;
  }
}

// and in the completeUnitOfWork
function completeUnitOfWork(unitOfWork) {
  var completedWork = unitOfWork;
  do {
    // ....
    var flags = completedWork.flags;
    if (flags > PerformedWork) {
      //   if (returnFiber.lastEffect !== null) {
      //     returnFiber.lastEffect.nextEffect = completedWork;
      //   } else {
      //     returnFiber.firstEffect = completedWork;
      //   }
      //   returnFiber.lastEffect = completedWork;
      effectFiberList.push(completedWork);
    }
    //....
    completedWork = returnFiber; // Update the next thing we're working on in case something throws.
    workInProgress = completedWork;
  } while (completedWork !== null); // We've reached the root.
}
F3n67u commented 2 years ago

FWIW, This code is removed during the refactor of the effect list implement, see this pr: https://github.com/facebook/react/pull/20644

github-actions[bot] commented 6 months ago

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

github-actions[bot] commented 6 months ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!