Skip to content

[Fiber] Should Fiber have a root field? #8222

Closed

Description

@acdlite

Problem

When a component calls setState, we must update the pendingWorkPriority on its fiber and on all its ancestor fibers up to the root. When we get to the root, we ensure that the root is scheduled:

// Pseudo-code
while (fiber) {
  // Step 1
  if (comparePriority(priority, fiber.pendingWorkPriority)) {
    updatePriority(fiber)
  }
  // Step 2
  if (isRoot(fiber)) {
    ensureRootIsScheduled(fiber);
  }
  fiber = fiber.return;
}

If setState is called multiple times consecutively, we walk the path each time. And if a callback is passed to setState, that requires an additional walk.

Because the priority of a child is always greater than or equal to the priority of its parent, it'd be nice to optimize this by stopping whenever the condition in step 1 fails.

The problem is step 2. We can't assume that the root of a tree is scheduled just because it has pending work: roots are unscheduled if an uncaught error is thrown in that tree. So we have to ensure the root is scheduled for every update.

One idea would be to use a field on FiberRoot (e.g. hasUncaughtError) rather than unschedule it. But that field would still need to be cleared on every update, which requires access to the root. So that doesn't solve the problem.

Possible solution

We could solve it by adding a root field to Fiber, so that we can always update it quickly. The question is whether the extra memory usage is worth the performance gain.

Activity

  1. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    Another option: since it's only needed for class components, we could add a root field to the class instead.

  2. sebmarkbage commented on Nov 7, 2016

    @sebmarkbage
    Contributor

    Intuitively it feels to me that the problem is with the scheduling of the root and not the fiber.

  3. sebmarkbage commented on Nov 7, 2016

    @sebmarkbage
    Contributor

    If we need to do extra work for the purpose of satisfying the scheduling problems during an error scenario, we've done the wrong tradeoff IMO. Error handling is a bonus.

  4. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    Do you mean this part?

    We can't assume that the root of a tree is scheduled just because it has pending work

  5. sebmarkbage commented on Nov 7, 2016

    @sebmarkbage
    Contributor

    yea

  6. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    I agree with your trade-off analysis, not sure how to solve it though.

  7. sebmarkbage commented on Nov 7, 2016

    @sebmarkbage
    Contributor

    roots are unscheduled if an uncaught error is thrown in that tree

    Can you help me understand what happens here conceptually? If all work has been invalidated, should we clear the priority flag in the entire tree to get it to a consistent state?

  8. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    @gaearon can probably explain it better. If there's an uncaught error, we need to avoid doing any pending work on that tree until there's another update.

    If all work has been invalidated, should we clear the priority flag in the entire tree to get it to a consistent state?

    That seems like a viable option

  9. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    What if there are pending updates on the queue and we clear the priority flag? Should they be dropped completely?

  10. sebmarkbage commented on Nov 7, 2016

    @sebmarkbage
    Contributor

    I don't know. I don't understand the case quite. However, currently we seem to be breaking one invariant of the model: If there are priority levels pending, then the root is scheduled.

    I need to understand why they should be allowed to diverge.

  11. acdlite commented on Nov 7, 2016

    @acdlite
    CollaboratorAuthor

    They diverge in master for now mostly as a hack. I think you're totally right, though. That's an important invariant.

    I think clearing the entire tree would also fix the issue Dan found in #8212. Never mind, unrelated.

    Re: my previous question, dropping updates because of an uncaught error makes sense to me conceptually, I think.

  12. acdlite commented on Nov 8, 2016

    @acdlite
    CollaboratorAuthor

    Closing in favor of #8227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions