Skip to content

fix: pair unkeyed children front-to-front during reconciliation#4099

Open
Madoshakalaka wants to merge 7 commits intomasterfrom
fix/unkeyed-reconciliation-front-pairing
Open

fix: pair unkeyed children front-to-front during reconciliation#4099
Madoshakalaka wants to merge 7 commits intomasterfrom
fix/unkeyed-reconciliation-front-pairing

Conversation

@Madoshakalaka
Copy link
Copy Markdown
Member

@Madoshakalaka Madoshakalaka commented Apr 2, 2026

Description

BList::apply_unkeyed stored children in reverse render order (rev_children) and paired old/new nodes from the back. When the child count changed between renders the pairing was wrong: growing a list by one at the tail caused the first child to be destroyed and recreated, while shrinking removed the first child instead of the last.

This destroyed stateful DOM elements (<video>, <input>) even though they never changed, and is the root cause behind the {for expr}, {vec}, and {option} expression flattening regressions reported in #3262.

The fix flips rev_children to render order, drains/adds excess from the tail, patches paired nodes front-to-front, then flips back. The only added cost is three in-memory Vec::reverse calls (pointer swaps, sub-microsecond for typical child counts) per reconciliation; DOM operation count is unchanged.

What this fixes

  • Static siblings before a dynamic-length list are no longer recreated when the list grows or shrinks.
  • {for iter}, {vec}, and {option} expressions that flatten into a parent VList no longer corrupt leading siblings.
  • Matches the front-to-front positional reconciliation behavior of React.

What this does not fix

  • {for expr} / {vec} / {option} still flatten into the parent (macro-level issue, separate fix).
  • Dynamic-length lists in the middle of siblings can still mismatch trailing nodes (same limitation as React; use keys or the for x in iter { } syntax for full correctness).

Fixes #3262

Checklist

  • I have reviewed my own code
  • I have added tests
    • Five new wasm_bindgen_test node-identity tests (node_identity_tests in blist.rs) that assert DOM node reuse via Node::is_same_node. Each test fails before the fix and passes after:
      • for_iterable_preserves_sibling_identity
      • vec_expression_preserves_sibling_identity
      • option_expression_preserves_sibling_identity
      • unkeyed_grow_preserves_leading_nodes
      • unkeyed_shrink_preserves_leading_nodes

`apply_unkeyed` paired nodes from the back of render order, so growing
or shrinking a list destroyed and recreated leading siblings instead of
reusing them.  Flip `rev_children` to render order before diffing,
drain/add excess from the tail, and flip back.  This makes positional
reconciliation match the intuitive front-to-front expectation and fixes
stateful elements (`<video>`, `<input>`) being reset when a sibling
list changes length.
@Madoshakalaka Madoshakalaka added performance A-yew Area: The main yew crate labels Apr 2, 2026
@Madoshakalaka Madoshakalaka changed the title fix: pair unkeyed children front-to-front during reconciliation (#3262) fix: pair unkeyed children front-to-front during reconciliation Apr 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Visit the preview URL for this PR (updated for commit b77923d):

https://yew-rs-api--pr4099-fix-unkeyed-reconcil-8duxatdr.web.app

(expires Sun, 12 Apr 2026 20:35:07 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.776 ns      │ 4.022 ns      │ 2.781 ns      │ 2.839 ns      │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.748 ns      │ 3.173 ns      │ 2.752 ns      │ 2.763 ns      │ 100     │ 1000000000

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Size Comparison

Details
examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 99.980 100.237 +0.257 +0.257%
boids 163.821 164.078 +0.257 +0.157%
communication_child_to_parent 93.236 93.493 +0.257 +0.275%
communication_grandchild_with_grandparent 105.054 105.311 +0.257 +0.244%
communication_grandparent_to_grandchild 101.412 101.669 +0.257 +0.253%
communication_parent_to_child 90.648 90.905 +0.257 +0.283%
contexts 105.115 105.372 +0.257 +0.244%
counter 85.940 86.197 +0.257 +0.299%
counter_functional 87.976 88.232 +0.257 +0.292%
dyn_create_destroy_apps 89.862 90.119 +0.257 +0.286%
file_upload 98.950 99.207 +0.257 +0.260%
function_delayed_input 93.931 94.188 +0.257 +0.273%
function_memory_game 169.043 169.299 +0.256 +0.151%
function_router 397.922 398.178 +0.256 +0.064%
function_todomvc 164.065 164.321 +0.256 +0.156%
futures 234.660 234.975 +0.314 +0.134%
game_of_life 100.273 100.529 +0.256 +0.255%
immutable 257.880 258.261 +0.381 +0.148%
inner_html 80.463 80.720 +0.257 +0.319%
js_callback 109.084 109.341 +0.257 +0.235%
keyed_list 175.777 176.034 +0.257 +0.146%
mount_point 83.832 84.089 +0.257 +0.306%
nested_list 112.743 112.999 +0.256 +0.227%
node_refs 91.224 91.480 +0.257 +0.282%
password_strength 1718.438 1718.694 +0.257 +0.015%
portals 92.713 92.970 +0.257 +0.277%
router 364.637 364.893 +0.256 +0.070%
suspense 113.062 113.318 +0.257 +0.227%
timer 88.078 88.335 +0.257 +0.292%
timer_functional 98.567 98.824 +0.257 +0.261%
todomvc 141.759 142.015 +0.256 +0.180%
two_apps 85.804 86.061 +0.257 +0.299%
web_worker_fib 135.562 135.818 +0.257 +0.189%
web_worker_prime 183.963 184.220 +0.257 +0.140%
webgl 82.606 82.863 +0.257 +0.311%

✅ None of the examples has changed their size significantly.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Benchmark - SSR

Yew Master

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.914 311.451 311.152 0.169
Hello World 10 509.264 515.416 511.291 2.057
Function Router 10 32813.028 33625.083 33253.429 318.588
Concurrent Task 10 1006.007 1007.623 1006.997 0.600
Many Providers 10 1087.216 1109.432 1094.671 7.907

Pull Request

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.928 311.503 311.109 0.164
Hello World 10 476.501 482.934 480.496 2.291
Function Router 10 32447.473 33249.384 32844.365 285.697
Concurrent Task 10 1006.600 1007.905 1007.107 0.461
Many Providers 10 1099.726 1170.723 1125.274 20.161

@Madoshakalaka
Copy link
Copy Markdown
Member Author

The benchmark deltas are CI environmental noise, not caused by this change.

05_swap1k and 06_remove-one-1k show 12-18% and 11-14% improvement respectively, but neither ever calls apply_unkeyed. Both go through the full apply_keyed path (key mismatch from the back is detected at the swap/removal point), which this PR does not touch.

For benchmarks that do hit apply_unkeyed via the keyed fast-path fallback (03_update10th1k_x16, 04_select1k), old and new lists have identical length (1000 vs 1000), so both old and new code produce identical DOM operations with the same node pairing. The only added work is three Vec::reverse calls on the 1000-element rev_children, totaling μs levels of pointer swaps against ms levels of DOM patches.

The real performance cost of this fix is those three in-memory reverses per apply_unkeyed call. For the typical case (same-length lists), this is maaybe 0.03% of reconciliation time. For the corrected case (different-length lists), the fix avoids destroying and recreating DOM elements that were previously mis-paired, which is a net performance win since creating a new element is far more expensive than reusing one.

@Madoshakalaka
Copy link
Copy Markdown
Member Author

@WorldSEnder you might be interested

@Madoshakalaka Madoshakalaka marked this pull request as ready for review April 2, 2026 07:01
github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
Comment on lines +166 to +168
// `rights` is stored in reverse render order. Flip to render order so
// that simple index-based pairing matches children front-to-front.
rights.reverse();
Copy link
Copy Markdown
Member

@WorldSEnder WorldSEnder Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it even make sense to keep rights in reverse order if we do this?

The only reason I can recall why this is done so that no reordering or swapping of the elements in the vec needs to happen here. You need to check for yourself that the existing implementation is the only possible way to do this with Vec<_> and the constraint that the writer calls need to happen back-to-front.

I remember trying out to use a VecDeque so that the drain above could happen at the back of the DOM order, which seemed not worth it for code size (VecDeque and Vec seem to not codeshare as much as I thought). I fully agree with your reasoning though: If the common failure case (mismatching lengths) leads to overhead downstream, then paying for two list reversals instead should be worth it. I just think that in the case of matching list lengths, no such reversing should need to take place.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying something in 8f3873d

Couldn't get the apply_keyed algorithm to work with render-order in a pinch.

Will experiment more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait. I see a small but constanst ~0.173 kB size increase across the example crates. will fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's acceptible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to see if I can find out what causes it, too, but it's very hard to inspect the resulting binaries in such detail 😓 could be a new panic path and error message, ~170 bytes is not a lot.

github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
Replace rev_children (reverse render order) with children (render order).

apply_keyed assigns reverse indices to the spliced middle so the existing
run algorithm works unchanged. shift iterates in reverse. Hydration drops
a now-unnecessary reverse.
github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
@Madoshakalaka Madoshakalaka marked this pull request as draft April 2, 2026 16:47
Replace the reversed-index hack (middle_count - 1 - fwd_idx) with
native render-order indices in apply_keyed's run algorithm. The barrier
tracking uses Option<usize> so checked_sub handles the "everything
committed" edge case that previously required reverse indexing.
@Madoshakalaka Madoshakalaka marked this pull request as ready for review April 2, 2026 17:19
github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
Use into_iter().rev() (pointer arithmetic on DoubleEndedIterator)
instead of in-place slice reverse (element-sized swap loops).
github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
github-actions[bot]
github-actions bot previously approved these changes Apr 2, 2026
github-actions[bot]
github-actions bot previously approved these changes Apr 3, 2026
Resolve conflict in blist.rs where Edition 2024 migration (#4114)
changed `if let` to `match` while our branch restructured the inner
keyed-reconciliation logic for render-order indices. Take master's
`match ancestor` syntax with our Option<usize> barrier_idx and
flipped comparison directions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: elements are re-rendered even if they didn't change, when there's a list below them (worked in v0.19.3)

2 participants