-
Notifications
You must be signed in to change notification settings - Fork 59
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
Suggestion: don't recompute when dependent signals come back to previous values used for last computation #197
base: main
Are you sure you want to change the base?
Conversation
… for last computation
This is an interesting comparison case. Currently, comparison is done "on the way out" rather than "on the way in", so there's no way to catch such a non-change. I don't really know how to evaluate the pros and cons of these approaches. Can you tell me more about where such a case has come up for you in practice, or what motivated you to implement tansu this way? |
I think the "use an extra computed signal" trick exposes basically exactly the performance tradeoff here: if you're willing to hold onto previous values values (at a memory cost) and run equality comparisons more often (at an execution time cost) you can potentially rerun computed bodies less often. It might not be pretty/performant but I think you can actually build this behavior into a subclass of State/Computeds by attaching a (Though this is one thing I think is cleaner with an "intercept tracked reads" primitive. You'd then be able to make a |
@littledan @shaylew Thank you for your answers.
I have noticed that. I think it is good (of course!) to do the comparison "on the way out", as you say, because if the value did not change, there is no reason to do anything at all. Of course this has a memory cost, as commented by @shaylew:
That's what we decided for tansu.
I agree this is not pretty/performant.
Having a way to intercept tracked reads is an interesting primitive to add. I don't think it is available in the current spec, is it?
We included the feature of storing the value received by each listener as part of a big refactoring that fixed a number of bugs. It was included especially because tansu implements the svelte store contract, which means a I think people would have the same expectation with native signals: signals should not be recomputed if the value of all dependent signals is the same as the last computation. I think it should work out of the box without having to add an extra layer of signals. Having a different behavior when adding or not adding a layer of computed signals feels buggy in my opinion. |
Hello,
I am a maintainer of the tansu signal library.
As I was exploring how to re-implement
tansu
with thesignal-polyfill
package (as I am hoping it will bring better interoperability with other signal libraries), I came across the following simple use case which does not match my expectations (and does not match either the behavior we implemented in tansu).I am opening this PR to discuss it. It does not (yet) contain any fix, just the test cases. One fails and the other one succeeds:
Here is the failing use case:
Here is a small variation of the previous case, with an extra intermediate computed signal, which prevents the re-computation of c:
I believe we should not have to add intermediate computed signals to prevent unneeded re-computations.
In addition to checking the version number of dependent signals, I was expecting the algorithm to also compare the values with the previous values (using the provided equal function) before calling the re-computation function.
What do you think?