Skip to content

Commit 2aa25df

Browse files
authored
alter skew based diffing for better heuristics (#4483)
1 parent 8e8dd92 commit 2aa25df

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/diff/children.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,30 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
298298
childVNode._flags |= INSERT_VNODE;
299299
}
300300
} else if (matchingIndex !== skewedIndex) {
301+
// When we move elements around i.e. [0, 1, 2] --> [1, 0, 2]
302+
// --> we diff 1, we find it at position 1 while our skewed index is 0 and our skew is 0
303+
// we set the skew to 1 as we found an offset.
304+
// --> we diff 0, we find it at position 0 while our skewed index is at 2 and our skew is 1
305+
// this makes us increase the skew again.
306+
// --> we diff 2, we find it at position 2 while our skewed index is at 4 and our skew is 2
307+
//
308+
// this becomes an optimization question where currently we see a 1 element offset as an insertion
309+
// or deletion i.e. we optimize for [0, 1, 2] --> [9, 0, 1, 2]
310+
// while a more than 1 offset we see as a swap.
311+
// We could probably build heuristics for having an optimized course of action here as well, but
312+
// might go at the cost of some bytes.
313+
//
314+
// If we wanted to optimize for i.e. only swaps we'd just do the last two code-branches and have
315+
// only the first item be a re-scouting and all the others fall in their skewed counter-part.
316+
// We could also further optimize for swaps
301317
if (matchingIndex == skewedIndex - 1) {
302318
skew--;
303319
} else if (matchingIndex == skewedIndex + 1) {
304320
skew++;
305321
} else if (matchingIndex > skewedIndex) {
306-
// Our matched DOM-node is further in the list of children than
307-
// where it's at now.
308-
309-
// When the remaining old children is bigger than the new-children
310-
// minus our skewed index we know we are dealing with a shrinking list
311-
// we have to increase our skew with the matchedIndex - the skewed index
312-
if (remainingOldChildren > newChildrenLength - skewedIndex) {
313-
skew += matchingIndex - skewedIndex;
314-
} else {
315-
// If we have matched all the children just decrease the skew
316-
skew--;
317-
}
318-
} else if (matchingIndex < skewedIndex) {
319-
if (matchingIndex == skewedIndex - skew) {
320-
skew -= matchingIndex - skewedIndex;
321-
} else {
322-
// When our new position is in front of our old position than we increase the skew
323-
skew++;
324-
}
322+
skew--;
323+
} else {
324+
skew++;
325325
}
326326

327327
// Move this VNode's DOM if the original index (matchingIndex) doesn't

test/browser/render.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,12 +1680,10 @@ describe('render()', () => {
16801680
'<div>11.remove()',
16811681
'<div>9.remove()',
16821682
'<div>10.remove()',
1683-
'<div>3146250.appendChild(<div>1)',
1684-
'<div>3462501.appendChild(<div>2)',
1685-
'<div>3465012.appendChild(<div>3)',
1686-
'<div>4650123.appendChild(<div>4)',
1687-
'<div>6501234.appendChild(<div>5)',
1688-
'<div>6012345.appendChild(<div>6)'
1683+
'<div>3146250.insertBefore(<div>0, <div>3)',
1684+
'<div>0314625.insertBefore(<div>1, <div>3)',
1685+
'<div>0134625.insertBefore(<div>2, <div>3)',
1686+
'<div>0123465.insertBefore(<div>5, <div>6)'
16891687
]);
16901688
clearLog();
16911689
});

0 commit comments

Comments
 (0)