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

Reduce memory overhead of tracking #3833

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/mobx/src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,13 @@ export function checkIfStateReadsAreAllowed(observable: IObservable) {
*/
export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context: any) {
const prevAllowStateReads = allowStateReadsStart(true)
// pre allocate array allocation + room for variation in deps
// array will be trimmed by bindDependencies
changeDependenciesStateTo0(derivation)
derivation.newObserving_ = new Array(derivation.observing_.length + 100)
// Preallocate array; will be trimmed by bindDependencies.
derivation.newObserving_ = new Array(
derivation.observing_.length
? Math.ceil(derivation.observing_.length * 1.2) // 20% extra room for variation in deps
: 100 // Establishing initial dependencies, preallocate constant space.
Copy link
Collaborator

@urugator urugator Feb 23, 2024

Choose a reason for hiding this comment

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

Is derivation.observing_.length a good indicator of "initial"? Ideally we would like to avoid prealocating anything for non-initial runs if there are no observable deps, eg:

const Cmp = observer(() => <div>no-observable-deps</div>)

Maybe there is an opportunity to bail-out even earlier is this case, dunno.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, good question! I think we wouldn't re-run trackDerivedFunction in case there are no dependencies, right? (Because nothing "inside" would trigger a rerun and external calls would get the cached value.)

Copy link
Collaborator

@urugator urugator Mar 4, 2024

Choose a reason for hiding this comment

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

No, deps can still change based on something not observable like prop/state.
The main point is that zero deps isn't special case - we assume the same thing - the number of deps don't change significantly between runs. Therefore we preallocate array of the same size - in this case an array of zero length. Probably not much else we can do. But we need to somehow differentiate between initial run (preallocate constant space) and non-initial run (preallocate zero). Probably we can just change the condition to: derivation.runId_ !== 0

)
derivation.unboundDepsCount_ = 0
derivation.runId_ = ++globalState.runId
const prevTracking = globalState.trackingDerivation
Expand Down