fiber Eng

Before react16 introduced the Fiber framework, React uses a recursive comparison of the virtual DOM tree to identify the nodes that need to be changed, and then synchronously update them (Reconcilation). React will hog browser resources during this time, causing user-triggered events to go unanswered.

React hope to be able to thoroughly solve the problem of the main thread take up for a long time, and Fiber was introduced to change this uncontrollable situation, Its rendering/update process is broken up into small pieces of tasks, through reasonable scheduling mechanism to regulate time, specify the task execution time, and reduces the probability of the page stalling, improve page interaction experience. Through Fiber, the Reconcilation process can be interrupted. Giving away CPU execution at the right time which allows the browser to respond to user interactions in a timely manner.

Summary: Synchronous update => occupy resources => browser cannot correspond => Introduce Fiber for time slice => make update process interruptible => improve interactive experience

Let's take a look at what the browser does in a full frame:

  1. Input events need to be processed first so that users can get the earliest feedback
  2. The next step is to process the timer. Check whether the timer runs out and execute the corresponding callback
  3. Process the Begin Frame, that is, the events of each Frame, including window.resize, Scroll, media Query change, and so on
  4. The requestAnimationFrame (rAF) is then executed, the rAF callback is executed before each drawing
  5. Then proceed to Layout, including calculating the Layout and updating the Layout. What is the style of this element and how should it be displayed on the page
  6. Then proceed to Paint, to get the size and position of each node, and the browser fills in the content for each element
  7. At this point, all six of the above phases have been completed. next comes the idle phase. Tasks registered in requestIdleCallback can be executed. (requestIdleCallback is the basis for the React Fiber implementation.)

How React was implemented before V16? React works by recursively comparing virtual DOM trees, finding nodes that need to be changed, and updating them synchronously. React always occupies browser resources, causing events triggered by users not to be responded to.

Using depth-first traversal to traverse the node, the execution stack will get deeper and deeper, and cannot be interrupted, after interruption can not be restored. If the recursion is very deep, it will be very slow.

In order to solve the above problem of long time occupation, Fiber is introduced to divide large tasks into multiple small tasks, which can be interrupted and resumed without blocking the main process to execute high-priority tasks.

What is the fiber?

Fiber can be understood as an execution unit or as a data structure.

An execution unit: Fiber can be understood as the division of smaller execution units. It divides a large task into many small tasks. The execution of a small task must be completed at one time without pause, but after a small task is executed, control can be handed over to the browser to respond to the user. So brower doesn't have to wait for the big task to finish before responding to the user.

React first requests a scheduler from the browser. If the browser has free time in a frame, The browser determines if there are any pending tasks, If no, it wil hand control to the browser, If yes, the corresponding task will be executed. When the execution is complete, it determines whether there is time left, If there is time and a task to execute, the next task will proceed, otherwise control will be given to the browser .

A data structure:

Fiber is also known as a data structure. React Fiber is implemented using linked lists. Each Virtual DOM can be represented as a fiber, as shown in the figure below, where each node is a fiber. A Fiber contains attributes like child, Sibling, return and so on. The React Fiber mechanism relies on the following data structures.

requestAnimationFrame

It is a browser-provided API for drawing animations. It requires the browser to call the specified callback function to update the animation before the next redraw.

requestIdleCallback(callback)

RequestIdleCallback enables developers to perform background and low-priority work on the main event loop without delaying critical events, such as animations and input responses. If the normal frame task does not exceed 16ms, it indicates that there is extra free time. In this case, the task registered in requestIdleCallback will be executed.

RequestIdleCallback allocates time slices, and when time runs out, control is returned to the browser. If render is interrupted and the task is not complete, it will continue to apply for the next time slice through requestIdleCallback, and continue to execute when there is time.

The concept of Lane priority

React assigns a priority to each update via lane. Lane can be simply understood as some numbers. The smaller the number, the higher the priority. The update of a higher priority interrupts the update of a lower priority. After the update of a higher priority is complete, the update of a lower priority starts.

Fiber implementation principle

The process of rendering and scheduling from the root node can be divided into two phases: the Render phase and the COMMIT phase.

Render phase

Find the changes on all nodes (These changes are called Effects)=> Create a Fiber tree, one virtual DOM node corresponds to one task => and then produce the effect list, From this we can know which nodes are updated, which nodes are added, and which nodes are removed.

The commit phase

Perform the side effects calculated in the previous phase at one time. Do not pause this phase, otherwise UI updates will be discontinuous.

Summary: React introduces Fiber and uses requestIdleCallback for time slicing to solve the problem of page stalling. Fiber specifically divides a large task into execution units to achieve it. Execute if you have free time, and hand control to the browser if you don't. The remaining fibers are then scheduled to execute according to lane priority.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,884评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,212评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,351评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,412评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,438评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,127评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,714评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,636评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,173评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,264评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,402评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,073评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,763评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,253评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,382评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,749评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,403评论 2 358

推荐阅读更多精彩内容