Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor layout phase to use recursion #24906

Merged
merged 4 commits into from Jul 12, 2022

Commits on Jul 12, 2022

  1. Move ref commit effects inside switch statement

    Only certain fiber types can have refs attached to them, so this moves the
    Ref effect logic out of the common path and into the corresponding branch
    of the layout phase's switch statement.
    
    The types of fibers this affects are host components and class components.
    Function components are not affected because they can only have a ref via
    useImperativeHandle, which has a different implementation. The experimental
    Scope type attaches its refs in the mutation phase, not the layout phase.
    acdlite committed Jul 12, 2022
    Copy the full SHA
    b8c96b1 View commit details
    Browse the repository at this point in the history
  2. Move flag check into each switch case

    The fiber tag is more specific than the effect flag, so we should always
    refine the type of work first, to minimize redundant checks.
    acdlite committed Jul 12, 2022
    Copy the full SHA
    3df7e8f View commit details
    Browse the repository at this point in the history
  3. Wrap try-catch directly around each user function

    (This is the same as f9e6aef, but for the layout phase rather than the
    mutation phase.)
    
    This moves the try-catch from around each fiber's layout phase to
    direclty around each user function (effect function, callback, etc).
    
    We already do this when unmounting because if one unmount function
    errors, we still need to call all the others so they can clean up
    their resources.
    
    Previously we didn't bother to do this for anything but unmount,
    because if a mount effect throws, we're going to delete that whole
    tree anyway.
    
    But now that we're switching from an iterative loop to a recursive one,
    we don't want every call frame on the stack to have a try-catch, since
    the error handling requires additional memory.
    
    Wrapping every user function is a bit tedious, but it's better
    for performance. Many of them already had try blocks around
    them already.
    acdlite committed Jul 12, 2022
    Copy the full SHA
    a1b1e39 View commit details
    Browse the repository at this point in the history
  4. Use recursion to traverse during layout phase

    This converts the layout phase to iterate over its effects
    recursively instead of iteratively. This makes it easier to track 
    contextual information, like whether a fiber is inside a hidden tree.
    
    We already made this change for the mutation phase. See 481dece for
    more context.
    acdlite committed Jul 12, 2022
    Copy the full SHA
    b641d02 View commit details
    Browse the repository at this point in the history