-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Performance issue with contexts using $derived in pair with array operations (dev mode only) #14491
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
Comments
There appear to be a few issues/potential solutions for this:
|
So I had a lengthy discussion with OP in the discord thread that lead to this issue
|
This is a dev mode only problem because We can look into why exactly it's slow in this case. |
Definitely a dev thing, but it took us several hours to realize that it occurs only in development mode. I really appreciate HenryKrinkle's assistance in identifying this untrivial situation. In our real-world application, we invoke the contexts thousands of times, which causes the app to freeze for many seconds whenever any of the context's reactive properties is triggered. This makes the app unusable during development, hindering our ability to progress and be confident with the prod output too. Moreover, although this is a development-only issue, it can be a significant deterrent for new Svelte developers trying to learn and understand the framework. I believe the development mode should not exhibit such significant performance degradation compared to production mode, as it can be misleading and counterproductive. |
The reason why this is so slow is:
The task is to find a way to do less work here. |
`$state.raw` and `$derived(.by)` will not have a state symbol on them, potentially causing a disastrous amount of traversal to potentially not find any state symbol. So it's better to not traverse them. Potentially someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`, but that feels so much of an edge case that it doesn't warrant a perf hit for the common case. Fixes #14491
* fix: don't try to add owners to non-`$state` class fields `$state.raw` and `$derived(.by)` will not have a state symbol on them, potentially causing a disastrous amount of traversal to potentially not find any state symbol. So it's better to not traverse them. Potentially someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`, but that feels so much of an edge case that it doesn't warrant a perf hit for the common case. Fixes #14491 * for bind:, too
Describe the bug
In development mode only, Svelte contexts can experience significant performance slowdowns when used in pair with any array operations (map, filter, etc.) in a $derived, no matter if the derived dependencies have changed or not.
Steps to reproduce:
1- Create a "runes class" with 3 runes:
2- Instantiate the class and pass the instance to a context using setContext.
3- Have multiple child components call getContext to retrieve the context.
Observed behavior:
If $state (C) changes, despite it has no impact on $state (A) neither its $derived (B), each additional child component calling getContext introduces incremental delays, which accumulate and result in a noticeable slowdown.
Reproduction
https://svelte.dev/playground/25a02d8edb9f4a4b9e5de54188beb94e?version=5.2.9
Simply hover any row. It will change the hovered $state (C). You'll notice it is laggy. But if you remove the array map in the $derived, it is fast again.
In my real-world codebase, running a devtool perf graph exposes hundreds of add_owner_to_object and add_owner instances. These seem to be only existing in dev mode.
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: