Skip to content

Implement deferring state updates #4760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v11-2
Choose a base branch
from

Conversation

JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented May 8, 2025

Copy of #4580

Note

I realized that we can make signals work by having a generic sCU in there, this enables us to say that we need to run through the update because of the state hooks. One concern that I still have is where hooks and state are mixed in the same component, if the state hooks end up settling but the signal changed we risk skipping an update we need. We could fix that by doing an options._afterRender check in signals as well though.

This puts our implementation more in line with React which could resolve a few issues as well as reduce the byte size impact of our code. Another benefit this brings would be that we stop mutating the Component instance and hence can't mess with other implementations like signals.

What React does is when you call reduce is that it enqueues an update but does not execute the action yet. It first renders the component so the outer scope to the state setter can be recaptured. This way it ensures that it deals with up-to-date props/...

Essentially what this comes down to is that we store every reducer invocation and only execute it as the component is rendered. This way it has access to the outer component scope, .... This needs us to add a new SKIP_CHILDREN bit-flag on the VNode so we can bail out when the state settles to equal.

For this to properly work we'd need to introduce options._afterRender which would check all of the hooks and set SKIP_CHILDREN when all hooks are equal.

To get the last test to pass we'd need to find a way to deterministically see whether this component started the diff as we should not bail if the parent instructed the change.

I think this should generally be a performance improvement as well as we aren't touching the Component instance anymore on every pass.

There was a failing test for the NaN case, this is a top-down update which is considered a bail so it will render 25 times and bail afterwards.
For clarity, this loop is also present in React https://stackblitz.com/edit/vitejs-vite-mqaupwag?file=src%2Fmain.tsx,src%2FApp.tsx&terminal=dev

Copy link

github-actions bot commented May 8, 2025

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -1% - +1% (-5.62ms - +11.73ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.03ms - +0.03ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +2% (-1.13ms - +1.11ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -1% - +1% (-0.22ms - +0.12ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +1% (-0.71ms - +0.79ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -1% - +6% (-0.03ms - +0.12ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -1% - +1% (-0.29ms - +0.24ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -3% - +3% (-1.03ms - +0.86ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.00ms - +0.01ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -5% - +5% (-0.38ms - +0.38ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -4% - +0% (-0.04ms - +0.00ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -0% - +1% (-0.01ms - +0.01ms)
    preact-local vs preact-main
  • update10th1k: slower ❌ 0% - 1% (0.00ms - 0.02ms)
    preact-local vs preact-main

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local988.07ms - 1001.39ms-unsure 🔍
-1% - +1%
-5.62ms - +11.73ms
preact-main986.11ms - 997.23msunsure 🔍
-1% - +1%
-11.73ms - +5.62ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.18ms - 19.18ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main19.18ms - 19.18msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.54ms - 16.58ms-unsure 🔍
-0% - +0%
-0.03ms - +0.03ms
preact-main16.54ms - 16.58msunsure 🔍
-0% - +0%
-0.03ms - +0.03ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.54ms - 1.54ms-unsure 🔍
-0% - +0%
-0.00ms - +0.01ms
preact-main1.53ms - 1.54msunsure 🔍
-0% - +0%
-0.01ms - +0.00ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local68.35ms - 69.80ms-unsure 🔍
-2% - +2%
-1.13ms - +1.11ms
preact-main68.23ms - 69.94msunsure 🔍
-2% - +2%
-1.11ms - +1.13ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local7.50ms - 8.10ms-unsure 🔍
-5% - +5%
-0.38ms - +0.38ms
preact-main7.57ms - 8.04msunsure 🔍
-5% - +5%
-0.38ms - +0.38ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.29ms - 16.48ms-unsure 🔍
-1% - +1%
-0.22ms - +0.12ms
preact-main16.30ms - 16.58msunsure 🔍
-1% - +1%
-0.12ms - +0.22ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.77ms - 3.77ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main3.77ms - 3.77msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #4706
  • Commit: f820118

duration

VersionAvg timevs preact-localvs preact-main
preact-local61.38ms - 62.51ms-unsure 🔍
-1% - +1%
-0.71ms - +0.79ms
preact-main61.41ms - 62.40msunsure 🔍
-1% - +1%
-0.79ms - +0.71ms
-

usedJSHeapSize

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

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local25.78ms - 26.48ms-unsure 🔍
-2% - +2%
-0.52ms - +0.48ms
preact-main25.79ms - 26.51msunsure 🔍
-2% - +2%
-0.48ms - +0.52ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local32.05ms - 33.50ms-unsure 🔍
-3% - +4%
-0.84ms - +1.14ms
preact-main31.94ms - 33.31msunsure 🔍
-3% - +3%
-1.14ms - +0.84ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local28.53ms - 29.53ms-unsure 🔍
-2% - +3%
-0.52ms - +0.88ms
preact-main28.36ms - 29.34msunsure 🔍
-3% - +2%
-0.88ms - +0.52ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local26.30ms - 27.35ms-unsure 🔍
-3% - +3%
-0.67ms - +0.75ms
preact-main26.31ms - 27.26msunsure 🔍
-3% - +2%
-0.75ms - +0.67ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local22.11ms - 23.20ms-unsure 🔍
-4% - +3%
-0.88ms - +0.67ms
preact-main22.20ms - 23.31msunsure 🔍
-3% - +4%
-0.67ms - +0.88ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local20.23ms - 21.05ms-unsure 🔍
-3% - +2%
-0.61ms - +0.47ms
preact-main20.36ms - 21.06msunsure 🔍
-2% - +3%
-0.47ms - +0.61ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 240
  • Built by: CI #4706
  • Commit: f820118

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.08ms - 2.22ms-unsure 🔍
-1% - +6%
-0.03ms - +0.12ms
preact-main2.07ms - 2.13msunsure 🔍
-6% - +1%
-0.12ms - +0.03ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.15ms - 1.17ms-unsure 🔍
-4% - +0%
-0.04ms - +0.00ms
preact-main1.16ms - 1.20msunsure 🔍
-0% - +4%
-0.00ms - +0.04ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local33.25ms - 33.62ms-unsure 🔍
-1% - +1%
-0.29ms - +0.24ms
preact-main33.27ms - 33.65msunsure 🔍
-1% - +1%
-0.24ms - +0.29ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.24ms - 1.24ms-unsure 🔍
-0% - +1%
-0.01ms - +0.01ms
preact-main1.23ms - 1.25msunsure 🔍
-1% - +0%
-0.01ms - +0.01ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local29.53ms - 30.88ms-unsure 🔍
-3% - +3%
-1.03ms - +0.86ms
preact-main29.63ms - 30.95msunsure 🔍
-3% - +3%
-0.86ms - +1.03ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.93ms - 2.95ms-slower ❌
0% - 1%
0.00ms - 0.02ms
preact-main2.93ms - 2.94msfaster ✔
0% - 1%
0.00ms - 0.02ms
-

tachometer-reporter-action v2 for CI

Copy link

github-actions bot commented May 8, 2025

Size Change: -6 B (-0.01%)

Total Size: 46.5 kB

Filename Size Change
dist/preact.js 4.65 kB +33 B (+0.72%)
dist/preact.mjs 4.66 kB +35 B (+0.76%)
dist/preact.umd.js 4.72 kB +35 B (+0.75%)
hooks/dist/hooks.js 1.52 kB -34 B (-2.19%)
hooks/dist/hooks.mjs 1.54 kB -40 B (-2.53%)
hooks/dist/hooks.umd.js 1.59 kB -35 B (-2.16%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.js 3.73 kB
compat/dist/compat.mjs 3.65 kB
compat/dist/compat.umd.js 3.79 kB
debug/dist/debug.js 3.85 kB
debug/dist/debug.mjs 3.85 kB
debug/dist/debug.umd.js 3.94 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.mjs 271 B
devtools/dist/devtools.umd.js 346 B
jsx-runtime/dist/jsxRuntime.js 861 B
jsx-runtime/dist/jsxRuntime.mjs 830 B
jsx-runtime/dist/jsxRuntime.umd.js 934 B
test-utils/dist/testUtils.js 473 B
test-utils/dist/testUtils.mjs 473 B
test-utils/dist/testUtils.umd.js 555 B

compressed-size-action

@JoviDeCroock JoviDeCroock marked this pull request as ready for review May 14, 2025 07:50
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

@JoviDeCroock
Copy link
Member Author

Thinking about this, a case I should test for having a bug is where it's the initial render and we trigger a render that goes to a new state and then back to the initial state. We should not allow that to bail out of render 😅

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.

3 participants