hushicai / hushicai.github.io

Blog
https://hushicai.github.io
27 stars 1 forks source link

React Fiber #44

Open hushicai opened 5 years ago

hushicai commented 5 years ago

React实现调度的方式是给每一个fiber设置一个expirationTime,不同时间即代表不同优先级,expirationTime越短,则代表优先级越高。

所谓的到期时间(expirationTime),是相对于调度器初始调用的起始时间而言的一个时间段;调度器初始调用后的某一段时间内,需要调度完成这项更新,这个时间段长度值就是到期时间值。

随着时间的流逝,一个更新的优先级会越来越高,这样就可以避免 starvation 问题(即低优先级的工作一直被高优先级的工作打断,而无法完成)。

React在ReactFiberExpirationTime中定义了ExpirationTime.

import MAX_SIGNED_31_BIT_INT from './maxSigned31BitInt';

export type ExpirationTime = number;

export const NoWork = 0; // 没有工作
export const Never = 1; // offscreen或者hidden
export const Sync = MAX_SIGNED_31_BIT_INT; // 同步(不会被调度也不会被打断)
hushicai commented 5 years ago

facebook/react#13071

hushicai commented 5 years ago

facebook/react#13904

hushicai commented 5 years ago

facebook/react#13912

hushicai commented 5 years ago

React为什么使用MAX_SIGNED_31_BIT_INT来表示Sync优先级?

sebmarkbage在facebook/react#10426上做了一些讨论。

sebmarkbage在其中说了一个de-opt概念,facebook/react#14365 也有相关讨论,而这篇文章也针对这个issue讲了一些v8的底层实现。

我尝试搜了一下1073741823,在stackoverflow上也有一些有用的信息。

hushicai commented 5 years ago

Twitter上有一个比较有意思的话题。 有人在评论了一条MDN上关于32位数溢出的相关说明: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value

hushicai commented 5 years ago

32位整数范围:#47

YardWill commented 5 years ago

对于React把10ms作为 1 unit,有什么见解吗?

hushicai commented 5 years ago

React会为每个更新计算一个Expiration Time,以10ms作为unit size,说明在10ms内触发的多个更新,计算出来的优先级是一样的,也就是会被一起commit。

React可能认为这么做足以保持UI的响应性,而不用频繁commit。

YardWill notifications@github.com于2019年6月17日 周一下午5:32写道:

对于React把10ms作为 1 unit,有什么见解吗?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hushicai/hushicai.github.io/issues/44?email_source=notifications&email_token=AAJRBD447GTIB472MZUM6DLP25K3JA5CNFSM4HGA2H3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX2TIPY#issuecomment-502608959, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJRBDZ7ST5OQZF6TH5KCBTP25K3JANCNFSM4HGA2H3A .

hushicai commented 5 years ago

V8 不仅可以优化代码,还可以去优化(deopt),详见v8 Deoptimization

hushicai commented 4 years ago

https://v8.dev/blog/react-cliff

hushicai commented 4 years ago

深入 V8 引擎:“小整数”到底有多小?