Skip to content
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

Delay executing state updates #4580

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented Nov 29, 2024

Resolves #2913

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.

Copy link

github-actions bot commented Nov 29, 2024

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -0% - +0% (-3.94ms - +3.95ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -1% - +0% (-0.17ms - +0.06ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -0% - +4% (-0.25ms - +3.14ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -3% - +4% (-0.41ms - +0.71ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +0% (-0.98ms - +0.23ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -4% - +4% (-0.09ms - +0.10ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -1% - +1% (-0.18ms - +0.28ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -2% - +2% (-0.78ms - +0.86ms)
    preact-local vs preact-main

usedJSHeapSize

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

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local943.52ms - 948.75ms-unsure 🔍
-0% - +0%
-3.94ms - +3.95ms
preact-main943.18ms - 949.08msunsure 🔍
-0% - +0%
-3.95ms - +3.94ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local24.85ms - 24.85ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main24.85ms - 24.85msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.55ms - 16.63ms-unsure 🔍
-1% - +0%
-0.17ms - +0.06ms
preact-main16.53ms - 16.75msunsure 🔍
-0% - +1%
-0.06ms - +0.17ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.66ms - 1.69ms-unsure 🔍
-1% - +1%
-0.02ms - +0.02ms
preact-main1.65ms - 1.68msunsure 🔍
-1% - +1%
-0.02ms - +0.02ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local75.82ms - 78.27ms-unsure 🔍
-0% - +4%
-0.25ms - +3.14ms
preact-main74.43ms - 76.78msunsure 🔍
-4% - +0%
-3.14ms - +0.25ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local11.04ms - 11.58ms-unsure 🔍
-5% - +1%
-0.62ms - +0.14ms
preact-main11.28ms - 11.81msunsure 🔍
-1% - +5%
-0.14ms - +0.62ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.01ms - 16.90ms-unsure 🔍
-3% - +4%
-0.41ms - +0.71ms
preact-main15.97ms - 16.65msunsure 🔍
-4% - +3%
-0.71ms - +0.41ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local4.50ms - 4.52ms-unsure 🔍
-0% - +0%
-0.00ms - +0.02ms
preact-main4.49ms - 4.51msunsure 🔍
-0% - +0%
-0.02ms - +0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #4160
  • Commit: 17960ec

duration

VersionAvg timevs preact-localvs preact-main
preact-local65.68ms - 66.43ms-unsure 🔍
-1% - +0%
-0.98ms - +0.23ms
preact-main65.96ms - 66.91msunsure 🔍
-0% - +1%
-0.23ms - +0.98ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.59ms - 3.63ms-unsure 🔍
-0% - +1%
-0.00ms - +0.04ms
preact-main3.58ms - 3.61msunsure 🔍
-1% - +0%
-0.04ms - +0.00ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local30.51ms - 31.16ms-unsure 🔍
-1% - +2%
-0.25ms - +0.73ms
preact-main30.24ms - 30.96msunsure 🔍
-2% - +1%
-0.73ms - +0.25ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local34.88ms - 36.07ms-faster ✔
0% - 6%
0.15ms - 2.12ms
preact-main35.83ms - 37.40msslower ❌
0% - 6%
0.15ms - 2.12ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local29.35ms - 30.25ms-unsure 🔍
-3% - +2%
-0.83ms - +0.69ms
preact-main29.26ms - 30.48msunsure 🔍
-2% - +3%
-0.69ms - +0.83ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local22.45ms - 23.08ms-unsure 🔍
-2% - +1%
-0.43ms - +0.32ms
preact-main22.62ms - 23.02msunsure 🔍
-1% - +2%
-0.32ms - +0.43ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local20.99ms - 21.59ms-unsure 🔍
-1% - +2%
-0.22ms - +0.40ms
preact-main21.10ms - 21.30msunsure 🔍
-2% - +1%
-0.40ms - +0.22ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local20.03ms - 20.62ms-unsure 🔍
-3% - +1%
-0.65ms - +0.17ms
preact-main20.28ms - 20.85msunsure 🔍
-1% - +3%
-0.17ms - +0.65ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 200
  • Built by: CI #4160
  • Commit: 17960ec

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.09ms - 2.21ms-unsure 🔍
-4% - +4%
-0.09ms - +0.10ms
preact-main2.08ms - 2.22msunsure 🔍
-4% - +4%
-0.10ms - +0.09ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.12ms - 1.12ms-slower ❌
0% - 1%
0.00ms - 0.01ms
preact-main1.11ms - 1.11msfaster ✔
0% - 1%
0.00ms - 0.01ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local33.65ms - 34.03ms-unsure 🔍
-1% - +1%
-0.18ms - +0.28ms
preact-main33.66ms - 33.92msunsure 🔍
-1% - +1%
-0.28ms - +0.18ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.23ms - 1.24ms-slower ❌
0% - 1%
0.00ms - 0.01ms
preact-main1.22ms - 1.22msfaster ✔
0% - 1%
0.00ms - 0.01ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local36.86ms - 37.98ms-unsure 🔍
-2% - +2%
-0.78ms - +0.86ms
preact-main36.78ms - 37.98msunsure 🔍
-2% - +2%
-0.86ms - +0.78ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.48ms - 3.51ms-unsure 🔍
-1% - +1%
-0.02ms - +0.02ms
preact-main3.48ms - 3.51msunsure 🔍
-1% - +1%
-0.02ms - +0.02ms
-

tachometer-reporter-action v2 for CI

Copy link

github-actions bot commented Nov 29, 2024

Size Change: -76 B (-0.12%)

Total Size: 62.3 kB

Filename Size Change
dist/preact.js 4.74 kB +31 B (+0.66%)
dist/preact.min.js 4.76 kB +35 B (+0.74%)
dist/preact.min.module.js 4.76 kB +33 B (+0.7%)
dist/preact.min.umd.js 4.79 kB +34 B (+0.72%)
dist/preact.module.js 4.76 kB +33 B (+0.7%)
dist/preact.umd.js 4.81 kB +35 B (+0.73%)
hooks/dist/hooks.js 1.44 kB -86 B (-5.64%)
hooks/dist/hooks.module.js 1.46 kB -97 B (-6.22%)
hooks/dist/hooks.umd.js 1.51 kB -94 B (-5.87%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.js 4.13 kB
compat/dist/compat.module.js 4.06 kB
compat/dist/compat.umd.js 4.2 kB
debug/dist/debug.js 3.82 kB
debug/dist/debug.module.js 3.83 kB
debug/dist/debug.umd.js 3.9 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.module.js 274 B
devtools/dist/devtools.umd.js 346 B
jsx-runtime/dist/jsxRuntime.js 973 B
jsx-runtime/dist/jsxRuntime.module.js 947 B
jsx-runtime/dist/jsxRuntime.umd.js 1.05 kB
test-utils/dist/testUtils.js 451 B
test-utils/dist/testUtils.module.js 456 B
test-utils/dist/testUtils.umd.js 536 B

compressed-size-action

@JoviDeCroock JoviDeCroock marked this pull request as draft November 29, 2024 07:30
@JoviDeCroock JoviDeCroock force-pushed the delay-executing-state branch 3 times, most recently from 374f151 to 340bd1a Compare November 29, 2024 15:13
@JoviDeCroock JoviDeCroock force-pushed the delay-executing-state branch 4 times, most recently from 1b6b6e2 to a8832fe Compare November 30, 2024 05:32
@JoviDeCroock JoviDeCroock marked this pull request as ready for review November 30, 2024 05:32
@coveralls
Copy link

coveralls commented Nov 30, 2024

Coverage Status

coverage: 99.613% (-0.004%) from 99.617%
when pulling 17960ec on delay-executing-state
into 80d9226 on main.

src/constants.js Outdated Show resolved Hide resolved
@JoviDeCroock JoviDeCroock force-pushed the delay-executing-state branch from a4f6d02 to 7a72b6a Compare December 2, 2024 09:24
@JoviDeCroock JoviDeCroock force-pushed the delay-executing-state branch 3 times, most recently from 3525847 to a2d6574 Compare December 2, 2024 09:33
@JoviDeCroock JoviDeCroock force-pushed the delay-executing-state branch from 5597c1e to 17960ec Compare December 2, 2024 09:39
@JoviDeCroock
Copy link
Member Author

I think this could cause issues for signals, when we combine signals with hooks we won't be able to rerender if the hooks have executed but ended up at the same value.

@JoviDeCroock JoviDeCroock marked this pull request as draft December 11, 2024 14:07
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.

useReducer: stale value in closure or lost update?
2 participants