Skip to content
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

trackDerivedFunction pre-allocates memory for new deps array too aggressively #3834

Open
realyze opened this issue Feb 20, 2024 · 1 comment
Labels

Comments

@realyze
Copy link
Contributor

realyze commented Feb 20, 2024

trackDerivedFunction from packages/mobx/src/core/derivation.ts gets called every time a derivation is read (i.e., very often) and we currently preallocate 400 bytes (via new Array(100)) for the new dependencies array. This seems overly generous and can lead to more frequent garbage collection.

It depends on the app but arguably in the common case dependencies wouldn't change too wildly between derivation recomputations. So changing this to preallocating a constant memory space for initial deps array and then relative to the last run (e.g. 20% legroom for when dependencies change) seems reasonable.

As an example, I instrumented my local mobx build and in Canva when panning on a whiteboard (where we intersect elements with viewport), we overallocate space for the deps array by roughly 98% (i.e., we only use 2% of the memory we allocate, rest gets CG'd).

I took a stab at improving this in #3833 .

Intended outcome:

Actual outcome:

How to reproduce the issue:

Versions

@realyze
Copy link
Contributor Author

realyze commented Feb 20, 2024

Not really a "bug" but I wasn't sure where else to file this - please feel free to change the label!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant