xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
546 stars 23 forks source link

Slow applySnapshot/fromSnapshot #342

Open andriimuzh opened 2 years ago

andriimuzh commented 2 years ago

Hi, I am using mobx-keystone for a while on mobile (ReactNative) project and faced that applySnapshot/fromSnapshot takes too much time. On some not very powerful devices, it may take up to 10 sec what is very bad. My snapshot may contain 1500 - 2500 models and I realize that is a big amount of data but still, 10 sec is too much. Currently, I use the 0.60.2 version I tried updating to 0.64 but in spite of less snapshot-size applying takes up to 20% more time compared with the 0.60.2 version. I hope you'll help me find some solution. Thank you. "mobx": "^6.3.2" "mobx-keystone": "^0.60.2" "mobx-react": "^7.2.0"

xaviergonz commented 2 years ago

Could you post your models (just the props should do) + a snapshot to do performance tests?

andriimuzh commented 2 years ago

Sorry for the long reply. I have to change the data because of NDA, I'll provide the data shortly.

andriimuzh commented 2 years ago

Hi @xaviergonz, I made an example with models and snapshot. I hope you'll find it helpful. Thank you.
https://codesandbox.io/s/winter-river-h923p?file=/src/index.tsx

xaviergonz commented 2 years ago

thanks, I'll take a look as soon as my laptop gets fixed 👍

xaviergonz commented 2 years ago

@andriimuzh

https://codesandbox.io/s/infallible-hermann-in6ug?file=/src/stores/index.ts

It says it takes around 1 ms to do the applySnapshot, am I missing something?

terrysahaidak commented 2 years ago

Hey @xaviergonz not sure why but seems like console.time is broken.

This code shows you the problem:

  console.time("apply snapshot");
  const now = Date.now();
  console.log('apply snapshot', now);
  applySnapshot(rootStore, { ...root, ...data, $modelId: rootStore.$modelId });
  console.timeEnd("apply snapshot");
  console.log('apply snapshot', Date.now() - now);

Also, when I used CPU throttle in developer settings console.time returned me the same result as Date.now thing

xaviergonz commented 2 years ago

Getting around 300ms for applySnapshot, slightly less for fromSnapshot. Is that what you get too? How big is your original data that take 10 secs?

https://codesandbox.io/s/infallible-hermann-in6ug?file=/src/stores/index.ts

terrysahaidak commented 2 years ago

Getting around 300ms for applySnapshot, slightly less for fromSnapshot. Is that what you get too? How big is your original data that take 10 secs?

https://codesandbox.io/s/infallible-hermann-in6ug?file=/src/stores/index.ts

The problem is that you are probably testing it on your laptop. We are facing problems on mid- and low-end android devices running Hermes.

Settings CPU throttling to 4x ony MBP 15 2018 (i7 6 cores) makes it 4s for this same data.

Running it with my pixel 3 on codesandbox makes it 1.5s.

Probably the problem is that it initialize all the models at once but MST is doing it lazily.