You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue happens in the development mode only. I believe this bug is somewhat related to #14491, respectively to the fix #14533, which helps with this case for classes, but not functions.
Steps to reproduce:
set a context that is a POJO with a $state.raw field (e.g. by using a factory function)
fill the $state.raw with something sufficiently deep
have multiple child components mount and call getContext to retrieve the context (same symptom as in the referenced PR)
The Svelte REPL apparently cannot build the component in a dev mode, therefore you need to run it in localhost. What I see in the devtool performance monitor is something like this:
Looking at the getContext implementation, it is understandable why this happens. The add_owner_to_object simply recurses into the fields of the state. I hope this could be addressed so that people who don't use classes (for whatever reason) aren't surprised by this.
We've also seen the add_owner_to_object happen when using useThrelte from Threlte 8. I looked up the implementation and it also uses the factory function pattern, where nothing prevents the deep recursion.
The problem with this is that getContext calls add_owner so that mutations coming from stuff passed through getContext are fine. Since it's $state.raw the algorithm doesn't stop traversal at any point, going through the whole object which is costly - and then it does that for each entry.
Possible solutions:
do widening at setContext ("everything is allowed for this thing"). Could mean false negatives when you also pass it as a prop that you don't bind but that's likely rare. Could mean worse perf in other places because it's not lazy (theoretically you can never do getContext, but again that's likely also rare)
do something with WeakMap to save traversed trees that didn't turn up any $state to add ownership to. Results in less runs at the cost of potentialyl higher memory
Describe the bug
This issue happens in the development mode only. I believe this bug is somewhat related to #14491, respectively to the fix #14533, which helps with this case for classes, but not functions.
Steps to reproduce:
$state.raw
field (e.g. by using a factory function)$state.raw
with something sufficiently deepgetContext
to retrieve the context (same symptom as in the referenced PR)Reproduction
https://svelte.dev/playground/ab1f18a235f44df28eae50837df5bc1d?version=5.19.0
The Svelte REPL apparently cannot build the component in a dev mode, therefore you need to run it in localhost. What I see in the devtool performance monitor is something like this:
Looking at the
getContext
implementation, it is understandable why this happens. Theadd_owner_to_object
simply recurses into the fields of the state. I hope this could be addressed so that people who don't use classes (for whatever reason) aren't surprised by this.Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: