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

Optimize dependency value change checks by allowing earlier exists from the loop #209

Merged
merged 2 commits into from
Sep 24, 2022

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Sep 24, 2022

This pull request modifies the dependency version change check used by computed & effect. Before the change each dependency's ._refresh() was called before checking the dependency's version number. After this change the version number is checked first, then ._refresh() is called, and then the version number is checked again. In practice this allows the dependency checks break the loop earlier, avoiding calls to _refresh() for a) plain signals that have changed and b) computed values that get repeated reads.

The following benchmark demonstrates the impact in a scenario that contains both a) and b):

import * as core from "@preact/signals-core";

const count = core.signal(0);
const double = core.computed(() => count.value * 2);
core.effect(() => double.value + count.value);
core.effect(() => double.value + count.value);
core.effect(() => double.value + count.value);

console.time("core");

for (let i = 0; i < 20000000; i++) {
count.value++;
double.value;
}

console.timeEnd("core");

The results before the change:

core: 2.616s

And after the change:

core: 2.196s

@changeset-bot
Copy link

changeset-bot bot commented Sep 24, 2022

🦋 Changeset detected

Latest commit: 49756ae

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-core 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 24, 2022

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit 49756ae
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/632e9b037fd10200081224ba
😎 Deploy Preview https://deploy-preview-209--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 24, 2022

Size Change: +284 B (0%)

Total Size: 67.5 kB

Filename Size Change
docs/dist/assets/index.********.js 1.08 kB +245 B (+29%) 🚨
docs/dist/assets/signals-core.module.********.js 1.36 kB +11 B (+1%)
docs/dist/assets/signals.module.********.js 1.97 kB +2 B (0%)
docs/dist/demos-********.js 3.35 kB +3 B (0%)
docs/dist/react-********.js 240 B +1 B (0%)
packages/core/dist/signals-core.js 1.42 kB +11 B (+1%)
packages/core/dist/signals-core.mjs 1.44 kB +11 B (+1%)
ℹ️ View Unchanged
Filename Size
docs/dist/assets/client.********.js 45.8 kB
docs/dist/assets/jsxRuntime.module.********.js 282 B
docs/dist/assets/preact.module.********.js 4 kB
docs/dist/assets/style.********.js 21 B
docs/dist/assets/style.********.css 1.21 kB
docs/dist/basic-********.js 243 B
docs/dist/nesting-********.js 1.13 kB
packages/preact/dist/signals.js 1.19 kB
packages/preact/dist/signals.mjs 1.15 kB
packages/react/dist/signals.js 833 B
packages/react/dist/signals.mjs 810 B

compressed-size-action

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.

🎉

@jviide jviide merged commit 7610bbc into main Sep 24, 2022
@jviide jviide deleted the optimize-deps-check branch September 24, 2022 10:30
@github-actions github-actions bot mentioned this pull request Sep 24, 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.

3 participants