Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Breaking React apart Fiber by Fiber

r31gN_
October 14, 2017

Breaking React apart Fiber by Fiber

React still continues to dominate a decent chunk of the frontend frameworks space. The team behind it is relentless in moving React forward, thus a new (currently in beta) implementation (complete, fully backwards compatible re-write) of the core algorithm was the next logical step. Fiber is what makes React faster and more powerful. In this talk we are going to take a dive into how Fiber works, how is it different and what can you do to use it and take advantage of the improvements it brings.

r31gN_

October 14, 2017
Tweet

More Decks by r31gN_

Other Decks in Technology

Transcript

  1. What’s new in React Fiber? • Perceived performance improvements •

    Error boundaries • Support for custom DOM attributes • Render can return multiple children • Streaming support for SSR • Others?
  2. Work Queue UI Thread 1 1 (takes more than one

    frame) Old Reconcilier (each frame ~ 16ms)
  3. Work Queue UI Thread 1 (junky UI) 2 2 3

    3 Old Reconcilier (each frame ~ 16ms)
  4. In its current implementation React walks the tree recursively and

    calls render functions of the whole updated tree during a single tick. However in the future it might start delaying some updates to avoid dropping frames. [...] If something is offscreen, we can delay any logic related to it. If data is arriving faster than the frame rate, we can coalesce and batch updates. We can prioritize work coming from user interactions (such as an animation caused by a button click) over less important background work (such as rendering new content just loaded from the network) to avoid dropping frames. React’s Design Principles
  5. React Fiber • Completely backwards* compatible re-write of the reconciliation

    algorithm • Promotes incremental rendering (splitting work) • Has the ability to pause, resume, abort, reuse work and assign priorities to updates • Achieves all that by declaring and using a new entity - fiber
  6. Defining a fiber • A unit of work • React

    Fiber is a React specialized reimplementation of the stack, so a fiber is a virtual stack frame • A JS object containing information about a component, its input and output
  7. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } The type of a fiber describes the component that it corresponds to (e.g.: MyComponent, “div”) The key is used during reconciliation to determine whether the fiber can be reused (same as in the old algorithm).
  8. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } These fields point to other fibers, describing the recursive tree structure of a fiber.
  9. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } The return fiber is the fiber to which the program should return after processing the current one. It is conceptually the same as the return address of a stack frame.
  10. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } Conceptually, props are the arguments of a function. A fiber's pendingProps are set at the beginning of its execution, and memoizedProps are set at the end. When the incoming pendingProps are equal to memoizedProps, it signals that the fiber's previous output can be reused, preventing unnecessary work.
  11. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } A number indicating the priority of the work represented by the fiber.
  12. { type, key, child, sibling, return, pendingProps, memoizedProps, pendingWorkPriority, output,

    ... } Output is created only at the leaf nodes by host components (e.g.: div, span, etc.). The output is then transferred up the tree.
  13. Work Queue UI Thread 1 (work gets split over multiple

    frames) Fiber (each frame ~ 16ms)
  14. Work Queue UI Thread 2 (no more janky UI, <

    16ms) Fiber 3 (each frame ~ 16ms)
  15. Resources • Andrew Clark’s Fiber architecture guide • Lin Clark’s

    presentation (A cartoon intro to Fiber) • Deep dive in Fiber • Error handling in React 16 • DOM attributes in React 16