mobx icon indicating copy to clipboard operation
mobx copied to clipboard

findCycle performance

Open asterius1 opened this issue 10 years ago • 4 comments

I found myself with my relatively large app, which suddenly became slow on mounting a complex component.

Profiling shown that findCycle function was running for ~3 seconds, while the rest was about 100ms. Apparently it's complexity is exponential in full DAG for example. Wrote a quick comparision of current version of this function and quick alternative with memoizing result: https://jsfiddle.net/s5hsp8ju/

It probably still can be fine tuned, but already runs 500 000 times faster for 30 nodes :)

asterius1 avatar Jun 23 '16 21:06 asterius1

Quick update of test case: https://jsfiddle.net/s5hsp8ju/1/ Just switched _findCycle(nodes[MAX - 2], nodes[MAX - 1]) to _findCycle(nodes[MAX - 1], nodes[MAX - 2]). first one could work with first approach if we itarated list in reversed order, second one cannot be solved significantly faster with the first approach.

asterius1 avatar Jun 23 '16 22:06 asterius1

Thanks for the PoC! I will play with it and either use the Map setup (with some fallback for other browsers) or see whether it is possible to make the findCycle an after-the-fact (e.g. stack overflow) analysis or a debug only thing so that it doesn't incur any runtime overhead at all

mweststrate avatar Jun 25 '16 08:06 mweststrate

Now, that I run mobx with my quickfix, findCycle is unnoticeable in timelime, so no pressure to get rid of it in production. It's also nice to get an elegant error, instead of overflowing stack.

asterius1 avatar Jun 25 '16 12:06 asterius1

Turned out findCycle didn't caught any cycles anymore which were not already caught (at a later stage) by the internal invariants. So just removed the entire thing :). Nothing beats that. Thanks for looking into this and suggesting the improvements though! Released as 2.3.4

mweststrate avatar Jul 04 '16 08:07 mweststrate