Skip to content

chore: perf tweaks for actions/styles/classes #12654

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

Merged
merged 2 commits into from
Jul 31, 2024
Merged

chore: perf tweaks for actions/styles/classes #12654

merged 2 commits into from
Jul 31, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jul 29, 2024

  • check if we really need to add/remove the class (calling includes first is cheaper than always setting/removing it)
  • check if we really need to update a style (calling getPropertyValue/setProperty is expensive)
  • check if we should call the action's update function (this is not only a perf tweak but also a correctness fix)

closes #12652

@peterkogo could you check if these tweaks increase perf for you? From a quick local test it did for me

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jul 29, 2024

🦋 Changeset detected

Latest commit: 0348817

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

This PR includes changesets to release 1 package
Name Type
svelte 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

- check if we really need to add/remove the class (calling `includes` first is cheaper than always setting/removing it)
- check if we really need to update a style (calling `getPropertyValue/setProperty` is expensive)
- check if we should call the action's update function (this is not only a perf tweak but also a correctness fix)

closes #12652
@peterkogo
Copy link

Wow, that was quick! I'll check first thing tomorrow morning!

@peterkogo
Copy link

@dummdidumm okay I couldn't wait.
It already looks much better on my end!
However, my action update functions are still re-running. Double-checked to see if I was tripping up on something, and it appears that all of my derived, effects etc. are rerunning on an update — which is the reason all of my actions get updated constantly.

Am I mistaken in the assumption that this should not happen? REPL with $inspect(id) rerunning on every update.

@dummdidumm
Copy link
Member Author

How does your action update function look like? Does it take an object or a primitive? Can you give an example REPL using an action where things update which you don't expect to / which don't update in Svelte 4?

In general, the update behavior is a bit different in Svelte 5. Since you're using $state.frozen, you're updating the object as a whole through one signal, and so everything depending on that signal updates, even if the value within the signal it listens to hasn't changed. To avoid that, you can add $derived in the right places.

@peterkogo
Copy link

peterkogo commented Jul 29, 2024

I am calling the action with an object containing a mixture of props and some other stuff.
I was not aware that immutable state behaves like that in Svelte 5 and thought that the reason <svelte:options immutable /> was kicked because this is of no concern anymore.
You think it makes sense for me to open a separate issue on this topic? I'm not too keen on having to memoize every prop by hand 🥲

@Rich-Harris Rich-Harris merged commit 5a05f63 into main Jul 31, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the perf-tweaks branch July 31, 2024 03:02
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.

Svelte 5 performance of style and class directives
3 participants