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

[preact] Optimize performance of prop bindings #153

Merged
merged 6 commits into from
Sep 15, 2022

Conversation

developit
Copy link
Member

In Preact 10, new VNode instances are passed to the DIFF hook on every render. In the previous prop bindings implementation, this was causing signal prop values to be bound on every VDOM render, leaking memory and doing unnecessary work.

This new approach replaces Signal prop values with their peek()'d current values in order to allow Preact to render those props using its standard setProperty mechanics, and the Signal instances are stored in a new property attached to the element VNode. After a VNode is diffed (options.diffed), if it has Signals stored in this new property, their DOM bindings are set up using an Updater that is stored on the DOM element itself - this works because DOM elements are stable references in Preact 10, whereas VNodes are not.

The implementation in this PR uses a single Updater instance for each Element VNode with one or more Signal prop values. Any time one of the Signals is changed, the Updater loops over the current set of Signals bound to its associated element and compares their new value to a cached copy - if the value is different, a DOM mutation is performed. In the future when #136 has landed, it may be more efficient (and smaller) to instead do per-signal-prop bindings using Effect. I have a version locally that does this.

@changeset-bot
Copy link

changeset-bot bot commented Sep 15, 2022

🦋 Changeset detected

Latest commit: 0da9ce3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@preact/signals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Sep 15, 2022

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit 0da9ce3
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/632340a4474bf8000c6c92bc
😎 Deploy Preview https://deploy-preview-153--preact-signals-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 15, 2022

Size Change: +460 B (+1%)

Total Size: 65.6 kB

Filename Size Change
docs/dist/assets/index.********.js 840 B -220 B (-21%) 🎉
docs/dist/assets/signals.module.********.js 1.95 kB -3.73 kB (-66%) 🏆
docs/dist/assets/style.*********.js 0 B -21 B (removed) 🏆
docs/dist/assets/style.********.css 1.21 kB +217 B (+22%) 🚨
docs/dist/basic-********.js 244 B +18 B (+8%) 🔍
docs/dist/demos-********.js 3.35 kB -91 B (-3%)
docs/dist/nesting-********.js 1.13 kB +15 B (+1%)
packages/preact/dist/signals.js 1.12 kB -11 B (-1%)
packages/preact/dist/signals.mjs 1.11 kB +8 B (+1%)
docs/dist/assets/jsxRuntime.module.********.js 282 B +282 B (new file) 🆕
docs/dist/assets/preact.module.********.js 4 kB +4 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
docs/dist/assets/client.********.js 45.8 kB
docs/dist/assets/signals-core.module.********.js 932 B
docs/dist/assets/style.********.js 21 B
docs/dist/react-********.js 238 B
packages/core/dist/signals-core.js 984 B
packages/core/dist/signals-core.mjs 788 B
packages/react/dist/signals.js 831 B
packages/react/dist/signals.mjs 791 B

compressed-size-action

@marvinhagemeister marvinhagemeister merged commit b70bc48 into main Sep 15, 2022
@marvinhagemeister marvinhagemeister deleted the preact-dom-bindings branch September 15, 2022 15:27
@github-actions github-actions bot mentioned this pull request Sep 15, 2022
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.

2 participants