Skip to content

Improve memoizing of computed values - #452

Closed
asterius1 wants to merge 17 commits into
mobxjs:masterfrom
asterius1:always_track_computed
Closed

asterius1 wants to merge 17 commits into
mobxjs:masterfrom
asterius1:always_track_computed

Conversation

@asterius1

Copy link
Copy Markdown
Member

This pull requests rewrites bunch of mobx core to 'change direction' of optimally recalculating changes.

Previously it was propagating from changed observables to observing derivations.

In proposed approach propagated up is only fact that some deep dependency might have changed (and reactions scheduled from it). Derivations before computing itself can check if any dependencies indeed changed and recompute when appropriate.
It introduces few big wins:

  • When modifications and computations are mixed mobx can still rerun only needed stuff
  • When in autorun(() => a ? b : c) a becomes true c is not being recomputed
  • Batching of memoization can be introduced. For example batch(() => { ... }) would make computed memoized for it's duration, giving (as far as I can tell) minimal possible number of recomputations regardless of modifications or reactions.

One issue I had is autoruns rerunning itself. Even though all tests for it are ok, I could point out some corner cases in which behaviour slightly change. In general self-rerunning autoruns, even outside this pull request, have many undefined/unexpected behaviours, although those cases can be quite artificial. I was thinking if we could add a rule so changes in autorun can't schedule it for rerun.

computed temporary memoization perf test previews the case, that made me do this in the first place.
Currently it takes 7 seconds. With changes it's down to ~1ms.
Most perf tests are very similar. Few perform worse.

The nicest thing about it is being able to not compromise clear code with performance even while implementing complex dependencies with possibly expensive actions.

Adjusted few tests, mainly lowering computation count.

Right now pull request doesn't pass the test for whyRun. Need to adjust language to new changes.

@mweststrate

Copy link
Copy Markdown
Member

So far this looks pretty amazing 😊. I will give detailed feedback and discuss stuff later. this might take a while but I'll come back on this ☺

Comment thread src/core/computedvalue.ts
newObserving = null; // during tracking it's array with new observed observers

isPendingUnobservation: boolean; // for effective unobserving
isObserved = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the following invariant if I'm correct? isObserved === dependenciesState !== -1? That might indicate that isObserved might not be explicitly needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the case only for ComputedValue.
Atom doesn't implement derivations, so they don't have dependenciesState
Reaction doesn't implement observable so they don't have isObserved.

isObserved is there to make sure that clearing up batch doesn't flip out.
It's kinda safer with.
I am not sure if it's 100% necessary.

@mweststrate

Copy link
Copy Markdown
Member

Left a bunch of comments and notes to self for now, will look later a bit more in depth to a some individual tests and memory and cpu performance in general

@asterius1

Copy link
Copy Markdown
Member Author

Thanks.
I am now rewriting DerivationsSets for better performance, because it's indeed a performance bottleneck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants