-
Notifications
You must be signed in to change notification settings - Fork 392
Refactor reactivity system with doubly-linked list and push-pull model #2025
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
Draft
wangshunnn
wants to merge
48
commits into
master
Choose a base branch
from
refactor-reactivity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,930
−556
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…update ignore files
…gitignore, and adjust build configuration
|
内部讨论后续 Mpx3 倾向于接入 @vue/reactivity 核心包,减少未来维护成本,也能提高跨端输出一致性。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor
Deps和Subs由之前的 Array 和 Set 数据结构改为双向链表结构(Doubly-linked list),受 Preact Signals 启发。packages/utils/@types),包含之前 @mpxjs/webpack-plugin 内部单独声明的declare module '@mpxjs/utils'部分,方便 @mpxjs/reactivity 等 TS 包引入使用。Performance
和 v2.10.6 版本的性能对比 benchmark 测试如下图所示,性能普遍全面提升,平均为之前的 2x~4x,最为明显的是 create computed(~9x),和
computed/effect依赖大量ref的场景(~4x),以及大量computed/effect场景(~3x)。前者明显提升的另一个原因是之前
computed创建需要同时实例化一个RefImpl和一个ReactiveEffect,现在仅需实例化一个ComputedRefImpl。More benchmarks for this PR vs. v2.10.6
Memory Usage
内存测试 benchmark 中创建了 1 ref + 10000 computeds (100 chained pairs) + 10000 effects (1 computed with an effect),按树形拓扑结构连接(详见代码或下面折叠内容)。
Memory benchmark 拓扑结构
Analyse
空间复杂度方面,一个明显优化在依赖收集阶段:
Dep需要一个Sub[]数组,而一个Sub则需要记录新旧依赖的两个Dep[]数组,以及两个记录新旧依赖id编号的Set(),用于去重和后续查询清理。Dep和 一个Sub仅靠一个中间节点link通过双向链表关联,也无需额外 id 编号,各自仅需头尾节点即可实现链式查询和遍历。时间复杂度方面,一个明显的优化在于
computed/effect每次运行后的依赖清理阶段:deps[]数组然后对比旧数组来筛选移除,并且 remove 涉及数组index.of和splice操作,所以当不需要移除任何依赖时时间复杂度最优为Sub的deps[]数组长度,Dep的subs[]数组长度。Sub尾节点depsTail之后的节点便是需要移除的,而且双向链表 remove 单个节点仅需Sub的deps数量。相比之前平均降低了一个量级。Fix and others
修复一些小程序和 Web (Vue2) 表现不一致等历史遗留问题:
triggerRef):triggerRef之前仅对shallowRef生效,现在对普通Ref也生效,与 Web(Vue) 端对齐。toRef):修复toRef缺少的第三个可选默认值参数,与当前类型声明以及 Web 端对齐。toRefs):增加对数组类型的支持,与 Web(Vue) 端对齐。effectScope): 修复嵌套的scope.off()后getCurrentScope失效问题 - refer Vue fix。