Skip to content
Myr

perf(jsx/dom): optimize matching-head child lookup during reconciliation (#5329)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #5306

authored by Liang Xu · committed by GitHub · parent 8755b17 · Browse files · 2 files+82 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
  • Co-authored-by: Liang Xu <lx3133584@users.noreply.github.com>

Diff stats

2 files+82 added−16 removed

  • tests:+53 added−0 removed
  • other:+29 added−16 removed

2 files+82 added−16 removed

modified:src/jsx/dom/index.test.tsx+53 added
2016 unmodified lines
2017 '<div><div>a</div><div>c</div><div>b</div><button>add</button><button>remove</button><button>swap</button></div>'2018 )2019 })20202021 it('reconciles large keyed list updates and reordering efficiently', async () => {2022 let findIndexCalls = 02023 const originalFindIndex = Array.prototype.findIndex2024 // eslint-disable-next-line @typescript-eslint/no-explicit-any2025 Array.prototype.findIndex = function (this: unknown[], ...args: [any]) {2026 findIndexCalls++2027 return originalFindIndex.apply(this, args)2028 }20292030 try {2031 const ListApp = () => {2032 const [items, setItems] = useState(() => Array.from({ length: 100 }, (_, i) => `item-${i}`))2033 return (2034 <div>2035 <ul>2036 {items.map((item) => (2037 <li key={item}>{item}</li>2038 ))}2039 </ul>2040 <button id='reverse' onClick={() => setItems((prev) => [...prev].reverse())}>2041 reverse2042 </button>2043 <button id='update' onClick={() => setItems((prev) => [...prev])}>2044 update2045 </button>2046 </div>2047 )2048 }20492050 render(<ListApp />, root)2051 expect(root.querySelectorAll('li')).toHaveLength(100)2052 expect(root.querySelector('li')?.textContent).toBe('item-0')20532054 findIndexCalls = 02055 const updateBtn = root.querySelector('#update') as HTMLButtonElement2056 updateBtn.click()2057 await Promise.resolve()20582059 expect(findIndexCalls).toBe(0)2060 expect(root.querySelectorAll('li')).toHaveLength(100)2061 expect(root.querySelector('li')?.textContent).toBe('item-0')20622063 const reverseBtn = root.querySelector('#reverse') as HTMLButtonElement2064 reverseBtn.click()2065 await Promise.resolve()20662067 expect(root.querySelectorAll('li')).toHaveLength(100)2068 expect(root.querySelector('li')?.textContent).toBe('item-99')2069 } finally {2070 Array.prototype.findIndex = originalFindIndex2071 }2072 })20732074 it('preserves keyed child identity and state when reordering', async () => {2075 let reverse: () => void = () => {}2076 const Row = ({ id }: { id: string }) => {2077 const [count, setCount] = useState(0)
1604 unmodified lines
modified:src/jsx/dom/render.ts+29 added-16 removed
572 unmodified lines
573 oldChild = candidate as NodeObject574 oldChildrenByKey.delete(key)575 }576 } else if (oldVChildren && oldVChildren.length) {577 const i = oldVChildren.findIndex(578 isNodeString(child)579 ? (c) => isNodeString(c)580 : child.key !== undefined581 ? (c) => c.key === (child as Node).key && c.tag === (child as Node).tag582 : (c) => c.tag === (child as Node).tag583 )577 const first = oldVChildren[0]578 const isMatchFirst = isNodeString(child)579 ? isNodeString(first)580 : !isNodeString(first) &&581 (child.key !== undefined582 ? (first as Node).key === (child as Node).key &&583 (first as Node).tag === (child as Node).tag584 : (first as Node).tag === (child as Node).tag)585586 if (isMatchFirst) {587 oldChild = oldVChildren.shift() as NodeObject588 } else {589 const i = oldVChildren.findIndex(590 isNodeString(child)591 ? (c) => isNodeString(c)592 : child.key !== undefined593 ? (c) => c.key === (child as Node).key && c.tag === (child as Node).tag594 : (c) => c.tag === (child as Node).tag595 )596585 scanBudget -= i === -1 ? oldVChildren.length : i586 if (i !== -1) {587 oldChild = oldVChildren[i] as NodeObject588 oldVChildren.splice(i, 1)597 scanBudget -= i === -1 ? oldVChildren.length : i598 if (i !== -1) {599 oldChild = oldVChildren[i] as NodeObject600 oldVChildren.splice(i, 1)601 }602 // Try indexing once, after searches exceed a linear budget.603 if (scanBudget < 0) {604 scanBudget = Infinity605 oldChildrenByKey = indexChildrenByKey(oldVChildren)606 }607 }590 // Try indexing once, after searches exceed a linear budget.591 if (scanBudget < 0) {592 scanBudget = Infinity593 oldChildrenByKey = indexChildrenByKey(oldVChildren)594 }608 }609610 if (oldChild) {611 if (isNodeString(child)) {
268 unmodified lines