Skip to content

Conversation

@JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented Feb 20, 2025

Fixes #4699 and a whole lot more

There's a few cases that get solved in this PR, let's go over them!

First and foremost, the issue points us at an issue where having a null'ish middle node can lead to false positives. In the example we have one keyed node like the following

<CompA key=1 />
null
<CompB />
<CompB />

When we create this initially we'll see 3 creations for A, B and B. This is expected, when we swap out CompA by changing its key to i.e. 2 we expect that the only new creation we'll see is from <CompA />, however this is not the case we'll also remount one of the two <CompB /> nodes and reset their state if they are using state which is unexpected.

This is caused by the following:

  • We can't find a matching vnode because the key changed
  • We decrement the skew to deal with offsets, we assume that the amount of children grew in the new variant hence we increment skew. (when we increment skew we'll look at a smaller index in the oldChildren i.e. [1, 2, 3] becomes [0, 1, 2, 3] --> when we diff 0 that's technically -1 in oldChildren and we want to compare 1 with 1)
  • When we get to the next skew in newChildren we'll compare <CompB /> with null, this means that we'll erroneously assume we swapped out <CompB /> with null which makes us unmount it.

We fix the above by adding a check in the mount case that does the following

  • Decrement the skew if the list of newChildren is larger than the list of oldChildren (we added a node 0 becomes -1)
  • Increment the skew if the list of newChildren is smaller than the list of oldChildren (we removed a node 0 becomes 1)
  • If the list stays the same, like our issue case, we don't do anything!

This made me start looking into non-keyed cases and indeed, this issue existed in the unkeyed case as well which luckily is also fixed with the above measure. I've added tests for all of these cases. When digging even deeper I found some general issues with how we handle placeholders (aka holes in our array). Generally when we replace a keyed node, append or prepend with a placeholder we see re-mounting issues. I've counter-acted that by replacing our null case in the child-matching with a && !key condition, this means that for keyed nodes we'll always search the children as this is kind of the goal of keys, we can move them around without fear.

@github-actions
Copy link

github-actions bot commented Feb 20, 2025

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -1% - +0% (-6.41ms - +2.76ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.05ms - +0.06ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -1% - +1% (-0.64ms - +0.81ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +0% (-0.03ms - +0.06ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +1% (-0.85ms - +0.35ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -5% - +4% (-0.10ms - +0.08ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -1% - +0% (-0.46ms - +0.13ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -4% - +3% (-1.22ms - +1.12ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 -0% - +0% (-0.02ms - +0.01ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -2% - +2% (-0.03ms - +0.03ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -5% - +3% (-0.52ms - +0.29ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +1% (-0.00ms - +0.04ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - +0% (-0.01ms - +0.00ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -3% - +1% (-0.04ms - +0.01ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -1% - +1% (-0.03ms - +0.03ms)
    preact-local vs preact-main

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local923.38ms - 928.58ms-unsure 🔍
-1% - +0%
-6.41ms - +2.76ms
preact-main924.03ms - 931.59msunsure 🔍
-0% - +1%
-2.76ms - +6.41ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.18ms - 19.19ms-unsure 🔍
-0% - +0%
-0.02ms - +0.01ms
preact-main19.18ms - 19.20msunsure 🔍
-0% - +0%
-0.01ms - +0.02ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.56ms - 16.61ms-unsure 🔍
-0% - +0%
-0.05ms - +0.06ms
preact-main16.54ms - 16.63msunsure 🔍
-0% - +0%
-0.06ms - +0.05ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.55ms - 1.59ms-unsure 🔍
-2% - +2%
-0.03ms - +0.03ms
preact-main1.55ms - 1.59msunsure 🔍
-2% - +2%
-0.03ms - +0.03ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local79.43ms - 80.45ms-unsure 🔍
-1% - +1%
-0.64ms - +0.81ms
preact-main79.34ms - 80.37msunsure 🔍
-1% - +1%
-0.81ms - +0.64ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local9.45ms - 9.97ms-unsure 🔍
-5% - +3%
-0.52ms - +0.29ms
preact-main9.51ms - 10.13msunsure 🔍
-3% - +5%
-0.29ms - +0.52ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.56ms - 16.63ms-unsure 🔍
-0% - +0%
-0.03ms - +0.06ms
preact-main16.55ms - 16.61msunsure 🔍
-0% - +0%
-0.06ms - +0.03ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.77ms - 3.80ms-unsure 🔍
-0% - +1%
-0.00ms - +0.04ms
preact-main3.76ms - 3.78msunsure 🔍
-1% - +0%
-0.04ms - +0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #4478
  • Commit: 725f2d2

duration

VersionAvg timevs preact-localvs preact-main
preact-local64.39ms - 64.94ms-unsure 🔍
-1% - +1%
-0.85ms - +0.35ms
preact-main64.38ms - 65.44msunsure 🔍
-1% - +1%
-0.35ms - +0.85ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.98ms - 2.98ms-unsure 🔍
-0% - +0%
-0.01ms - +0.00ms
preact-main2.98ms - 2.99msunsure 🔍
-0% - +0%
-0.00ms - +0.01ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local29.39ms - 29.92ms-unsure 🔍
-2% - +1%
-0.46ms - +0.36ms
preact-main29.39ms - 30.02msunsure 🔍
-1% - +2%
-0.36ms - +0.46ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local33.61ms - 34.54ms-slower ❌
0% - 4%
0.05ms - 1.29ms
preact-main33.00ms - 33.81msfaster ✔
0% - 4%
0.05ms - 1.29ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local25.15ms - 25.55ms-unsure 🔍
-1% - +2%
-0.25ms - +0.39ms
preact-main25.03ms - 25.52msunsure 🔍
-2% - +1%
-0.39ms - +0.25ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local26.13ms - 26.79ms-slower ❌
0% - 4%
0.06ms - 1.14ms
preact-main25.43ms - 26.29msfaster ✔
0% - 4%
0.06ms - 1.14ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local26.84ms - 27.38ms-slower ❌
0% - 3%
0.02ms - 0.91ms
preact-main26.29ms - 27.00msfaster ✔
0% - 3%
0.02ms - 0.91ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local19.86ms - 20.24ms-unsure 🔍
-3% - +1%
-0.51ms - +0.22ms
preact-main19.88ms - 20.51msunsure 🔍
-1% - +3%
-0.22ms - +0.51ms
-
text-update

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.06ms - 2.20ms-unsure 🔍
-5% - +4%
-0.10ms - +0.08ms
preact-main2.09ms - 2.20msunsure 🔍
-4% - +5%
-0.08ms - +0.10ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.12ms - 1.12ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main1.12ms - 1.12msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local34.52ms - 34.90ms-unsure 🔍
-1% - +0%
-0.46ms - +0.13ms
preact-main34.65ms - 35.10msunsure 🔍
-0% - +1%
-0.13ms - +0.46ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.24ms - 1.27ms-unsure 🔍
-3% - +1%
-0.04ms - +0.01ms
preact-main1.25ms - 1.29msunsure 🔍
-1% - +3%
-0.01ms - +0.04ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local32.40ms - 33.67ms-unsure 🔍
-4% - +3%
-1.22ms - +1.12ms
preact-main32.10ms - 34.07msunsure 🔍
-3% - +4%
-1.12ms - +1.22ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.95ms - 3.00ms-unsure 🔍
-1% - +1%
-0.03ms - +0.03ms
preact-main2.96ms - 3.00msunsure 🔍
-1% - +1%
-0.03ms - +0.03ms
-

tachometer-reporter-action v2 for CI

@github-actions
Copy link

github-actions bot commented Feb 20, 2025

Size Change: +95 B (+0.12%)

Total Size: 78.4 kB

Filename Size Change
dist/preact.js 4.74 kB +12 B (+0.25%)
dist/preact.min.js 4.74 kB +15 B (+0.32%)
dist/preact.min.module.js 4.76 kB +12 B (+0.25%)
dist/preact.min.umd.js 4.77 kB +15 B (+0.32%)
dist/preact.mjs 4.75 kB +13 B (+0.27%)
dist/preact.module.js 4.75 kB +13 B (+0.27%)
dist/preact.umd.js 4.77 kB +15 B (+0.32%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.js 4.12 kB
compat/dist/compat.mjs 4.05 kB
compat/dist/compat.module.js 4.05 kB
compat/dist/compat.umd.js 4.19 kB
debug/dist/debug.js 3.83 kB
debug/dist/debug.mjs 3.83 kB
debug/dist/debug.module.js 3.83 kB
debug/dist/debug.umd.js 3.91 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.mjs 274 B
devtools/dist/devtools.module.js 274 B
devtools/dist/devtools.umd.js 346 B
hooks/dist/hooks.js 1.54 kB
hooks/dist/hooks.mjs 1.57 kB
hooks/dist/hooks.module.js 1.57 kB
hooks/dist/hooks.umd.js 1.61 kB
jsx-runtime/dist/jsxRuntime.js 978 B
jsx-runtime/dist/jsxRuntime.mjs 952 B
jsx-runtime/dist/jsxRuntime.module.js 952 B
jsx-runtime/dist/jsxRuntime.umd.js 1.05 kB
test-utils/dist/testUtils.js 473 B
test-utils/dist/testUtils.mjs 477 B
test-utils/dist/testUtils.module.js 477 B
test-utils/dist/testUtils.umd.js 555 B

compressed-size-action

@coveralls
Copy link

coveralls commented Feb 20, 2025

Coverage Status

coverage: 99.609%. remained the same
when pulling 725f2d2 on falsy-replacement
into 2a788d6 on main.

@JoviDeCroock JoviDeCroock marked this pull request as ready for review February 21, 2025 08:03
@JoviDeCroock JoviDeCroock changed the title Falsy replacement Keyed nullish placeholders cause re-mounts Feb 21, 2025
Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

LGTM, surprised how little changes were needed. Great job!

@JoviDeCroock JoviDeCroock merged commit a54a914 into main Feb 21, 2025
13 checks passed
@JoviDeCroock JoviDeCroock deleted the falsy-replacement branch February 21, 2025 08:20
@JoviDeCroock JoviDeCroock mentioned this pull request Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preact diffing incorrectly when there is a null or false in the rendered VNode's children and one of its sibling is remounted

5 participants