trackDerivedFunction
pre-allocates memory for new deps array too aggressively
#3834
Labels
trackDerivedFunction
pre-allocates memory for new deps array too aggressively
#3834
trackDerivedFunction
frompackages/mobx/src/core/derivation.ts
gets called every time a derivation is read (i.e., very often) and we currently preallocate 400 bytes (vianew 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
The text was updated successfully, but these errors were encountered: