sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
77.95k stars 4.07k forks source link

`$$restProps` is updated even when normal prop values are changed #7004

Closed aradalvand closed 2 years ago

aradalvand commented 2 years ago

Describe the bug

This is a small thing but I wanted to know whether it could be fixed or it was done intentionally. Here's the REPL.

What's happening here in this example is that whenever the value of the prop rating changes, it's as if $$restProps is also updated, and as a result, every part of the component that's referencing $$restProps would be notified and re-rendered, even though the contents of $$restProps haven't changed at all.

For $$props this would of course make sense, but not for $$restProps since the rating prop is a normal component prop and would never be included in $$restProps. So, any changes to it shouldn't really concern $$restProps at all.

This is a small inefficiency and I was curious as to why it happens. Thanks.

Severity

annoyance

baseballyama commented 2 years ago

I think the root cause is the same as https://github.com/sveltejs/svelte/issues/6921

In order to decide whether to recalculate a $ part, the runtime determines if a dependent variable(s) has changed. However, as shown in the following runtime code, object is always considered to have changed. Therefore a $ part is to be recalculated more than necessary.

https://github.com/sveltejs/svelte/blob/683c39adb7dd5acff9c6354b3d92ab0f8f430ad1/src/runtime/internal/utils.ts#L39-L41

This seems to be the intended behavior, at least from the code.

aradalvand commented 2 years ago

Okay but still nothing in the $$restProps has actually changed, I think Svelte should take a smarter approach in this regard. Don't you agree?

baseballyama commented 2 years ago

Can you explain the benefit of eliminating this small inefficiency? I think it's possible to eliminate this inefficiency, but there is a trade-off with performance. So I think there needs to be a clear reason to implement it.

baseballyama commented 2 years ago

Close this because there was no clear explanation here. Nevertheless, the conservative reactivity could be improved in Svelte4.