名称:快速 diff 算法
借鉴:ivi 和 inferno + 纯文本 diff 算法
第一步:预处理前置节点(从前到后比对新旧节点)
第二步:预处理后置节点(从后到前比对新旧节点)
第三步:处理仅有新增节点情况
第四步:处理仅有卸载情况
第五步:处理其他情况(新增 / 卸载 / 移动)寻找最长递增子序列
源码:
// can be all-keyed or mixed
const patchKeyedChildren = (
c1: VNode[],
c2: VNodeArrayChildren,
container: RendererElement,
parentAnchor: RendererNode | null,
parentComponent: ComponentInternalInstance | null,
parentSuspense: SuspenseBoundary | null,
namespace: ElementNamespace,
slotScopeIds: string[] | null,
optimized: boolean,
) => {
let i = 0
const l2 = c2.length
let e1 = c1.length - 1 // prev ending index
let e2 = l2 - 1 // next ending index
// 1. sync from start
// (a b) c
// (a b) d e
while (i <= e1 && i <= e2) {
const n1 = c1[i]
const n2 = (c2[i] = optimized
? cloneIfMounted(c2[i] as VNode)
: normalizeVNode(c2[i]))
if (isSameVNodeType(n1, n2)) {
patch(
n1,
n2,
container,
null,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
)
} else {
break
}
i++
}
// 2. sync from end
// a (b c)
// d e (b c)
while (i <= e1 && i <= e2) {
const n1 = c1[e1]
const n2 = (c2[e2] = optimized
? cloneIfMounted(c2[e2] as VNode)
: normalizeVNode(c2[e2]))
if (isSameVNodeType(n1, n2)) {
patch(
n1,
n2,
container,
null,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
)
} else {
break
}
e1--
e2--
}
// 3. common sequence + mount
// (a b)
// (a b) c
// i = 2, e1 = 1, e2 = 2
// (a b)
// c (a b)
// i = 0, e1 = -1, e2 = 0
if (i > e1) {
if (i <= e2) {
const nextPos = e2 + 1
const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
while (i <= e2) {
patch(
null,
(c2[i] = optimized
? cloneIfMounted(c2[i] as VNode)
: normalizeVNode(c2[i])),
container,
anchor,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
)
i++
}
}
}
// 4. common sequence + unmount
// (a b) c
// (a b)
// i = 2, e1 = 2, e2 = 1
// a (b c)
// (b c)
// i = 0, e1 = 0, e2 = -1
else if (i > e2) {
while (i <= e1) {
unmount(c1[i], parentComponent, parentSuspense, true)
i++
}
}
// 5. unknown sequence
// [i ... e1 + 1]: a b [c d e] f g
// [i ... e2 + 1]: a b [e d c h] f g
// i = 2, e1 = 4, e2 = 5
else {
const s1 = i // prev starting index
const s2 = i // next starting index
// 5.1 build key:index map for newChildren
const keyToNewIndexMap: Map<PropertyKey, number> = new Map()
for (i = s2; i <= e2; i++) {
const nextChild = (c2[i] = optimized
? cloneIfMounted(c2[i] as VNode)
: normalizeVNode(c2[i]))
if (nextChild.key != null) {
if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
warn(
`Duplicate keys found during update:`,
JSON.stringify(nextChild.key),
`Make sure keys are unique.`,
)
}
keyToNewIndexMap.set(nextChild.key, i)
}
}
// 5.2 loop through old children left to be patched and try to patch
// matching nodes & remove nodes that are no longer present
let j
let patched = 0
const toBePatched = e2 - s2 + 1
let moved = false
// used to track whether any node has moved
let maxNewIndexSoFar = 0
// works as Map<newIndex, oldIndex>
// Note that oldIndex is offset by +1
// and oldIndex = 0 is a special value indicating the new node has
// no corresponding old node.
// used for determining longest stable subsequence
const newIndexToOldIndexMap = new Array(toBePatched)
for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
for (i = s1; i <= e1; i++) {
const prevChild = c1[i]
if (patched >= toBePatched) {
// all new children have been patched so this can only be a removal
unmount(prevChild, parentComponent, parentSuspense, true)
continue
}
let newIndex
if (prevChild.key != null) {
newIndex = keyToNewIndexMap.get(prevChild.key)
} else {
// key-less node, try to locate a key-less node of the same type
for (j = s2; j <= e2; j++) {
if (
newIndexToOldIndexMap[j - s2] === 0 &&
isSameVNodeType(prevChild, c2[j] as VNode)
) {
newIndex = j
break
}
}
}
if (newIndex === undefined) {
unmount(prevChild, parentComponent, parentSuspense, true)
} else {
newIndexToOldIndexMap[newIndex - s2] = i + 1
if (newIndex >= maxNewIndexSoFar) {
maxNewIndexSoFar = newIndex
} else {
moved = true
}
patch(
prevChild,
c2[newIndex] as VNode,
container,
null,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
)
patched++
}
}
// 5.3 move and mount
// generate longest stable subsequence only when nodes have moved
const increasingNewIndexSequence = moved
? getSequence(newIndexToOldIndexMap)
: EMPTY_ARR
j = increasingNewIndexSequence.length - 1
// looping backwards so that we can use last patched node as anchor
for (i = toBePatched - 1; i >= 0; i--) {
const nextIndex = s2 + i
const nextChild = c2[nextIndex] as VNode
const anchor =
nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
if (newIndexToOldIndexMap[i] === 0) {
// mount new
patch(
null,
nextChild,
container,
anchor,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
)
} else if (moved) {
// move if:
// There is no stable subsequence (e.g. a reverse)
// OR current node is not among the stable sequence
if (j < 0 || i !== increasingNewIndexSequence[j]) {
move(nextChild, container, anchor, MoveType.REORDER)
} else {
j--
}
}
}
}
}
参数
c1
: 旧的子节点列表(VNode 数组)c2
: 新的子节点列表(VNode 数组)container
: 渲染容器,即挂载点parentAnchor
: 父锚点节点,用于指定插入位置parentComponent
: 父组件实例parentSuspense
: 父 suspense 实例namespace
: 命名空间,用于处理 SVG 等特殊元素slotScopeIds
: 插槽作用域 IDoptimized
: 是否优化,基于n2.dynamicChildren
决定
主要逻辑
-
从头同步(Sync from start)
- 从头开始比较两个子节点列表,直到遇到不同的节点或到达任一列表的末尾。
- 如果节点类型相同,则调用
patch
函数更新节点。
-
从尾同步(Sync from end)
- 从尾开始比较两个子节点列表,直到遇到不同的节点或到达任一列表的开头。
- 如果节点类型相同,则调用
patch
函数更新节点。
-
处理公共序列和新增节点(Common sequence + mount)
- 如果
i
超过了e1
,说明旧列表已经遍历完,但新列表还有剩余节点。 - 从
i
到e2
的节点是新增的,调用patch
函数插入这些节点。
- 如果
-
处理公共序列和移除节点(Common sequence + unmount)
- 如果
i
超过了e2
,说明新列表已经遍历完,但旧列表还有剩余节点。 - 从
i
到e1
的节点是需要移除的,调用unmount
函数卸载这些节点。
- 如果
-
处理未知序列(Unknown sequence)
- 如果
i
既没有超过e1
也没有超过e2
,说明两个列表中间有一段未知的序列需要处理。 - 构建新节点的键索引映射:
- 创建一个
keyToNewIndexMap
,将新节点的键映射到其索引。
- 创建一个
- 遍历旧节点列表:
- 对于每个旧节点,尝试在新节点列表中找到匹配的节点。
- 如果找到匹配的节点,则调用
patch
函数更新节点,并记录其在新节点列表中的位置。 - 如果没有找到匹配的节点,则卸载该旧节点。
- 生成最长稳定子序列:
- 如果节点有移动,则计算最长稳定子序列。
- 移动和插入新节点:
- 从后向前遍历新节点列表,根据最长稳定子序列决定是否移动节点。
- 如果节点不在稳定子序列中,则移动或插入该节点。
- 如果