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
Add caching to sleeping pure computed observables #1543
Conversation
|
…ward supporting caching for sleeping computeds
…on number for each observable that's incremented when the observable's value is changed.
e424f25
to
ac9ce26
Compare
Since this change is all about performance, it's useful to check that there are no negative affects on performance. To check this, I set up a series of benchmark tests using a simple computed observable that just returns the value of a single observable. The following actions showed no significant change in performance:
The following actions showed a significant change in performance:
As the computed observable itself becomes more complex, those two differences will improve as well. @brianmhunt, @rniemeyer, @SteveSanderson, I'd like to get this merged in soon. So please let me know if you have any concerns. |
Cool. This is all a bit beyond me, but I like the idea. Would every |
Yes. Putting it in |
@mbest - I'll start taking a detailed look at this one and merge this week |
Thanks, Ryan. |
observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); } | ||
observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); } | ||
observable.valueHasMutated = function () { | ||
observable.updateVersion(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think the above line should perhaps be moved inside notifySubscribers
? The version number would then mean "notification number" rather than "mutation number".
The reason I suggest this is that I think there's a lot of code out in the wild that does stuff like this:
myObservable().subProperty = "New value";
myObservable.notifySubscribers();
I know that's not the recommended pattern, but people may do it (and in practice, there are sure to be plenty of cases where it is done).
If the version continues to refer to mutations rather than notifications, pure computeds won't update correctly in this case, will they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also thought about using the notification, but if the observable is rate-limited, that would change when a sleeping computed would see the new value. Also how would it know when a sleeping computed has changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we could update the version in both cases (mutation and notification), at least for rate-limited and sleeping observables.
In principle this sounds great. The way you've described the feature totally makes sense, and thanks for quantifying the perf impact. I won't pretend to have made sense of every line in the diff (in fact, it's complex enough that I probably would have to reimplement it from scratch to understand it) but I'm fine with trusting that you know what you are doing with this :) Some line comments added. |
I split the code changes into two commits to help make the actual changes more clear. The first commit mainly re-organizes the code and doesn't change functionality. If you ignore white-space changes, it's a little clearer. |
I've added a test regarding the order of subscriptions. I hope it's clear enough. |
8b6b2b1
to
364f7ee
Compare
…tions when awakening
364f7ee
to
c104d35
Compare
…on, except for a sleeping pure computed to support chaining of pure computeds.
@SteveSanderson, I thought some more about the notification/mutation issue and now agree with you that the version should be changed on change notification. This makes a sleeping computed act more like a normal computed when dependent on a rate-limited observable. I've added some tests to show this. |
I think this looks good to merge. @SteveSanderson - do you feel that your concerns were addressed? |
@mbest's comments are comforting :) I'll check the code and merge it in the morning hopefully! |
Add caching to sleeping pure computed observables
Thanks again @mbest! This looks like it was tricky to get right, so thanks for being so thorough with the testing. It's great that We have so much functionality around computeds by now that the logic in Anyway, great to get this in :) |
This is mainly in response to #1531, although I had the idea to do this while originally working on the pure computed feature.