Skip to content

Commit

Permalink
Reduce memory overhead of tracking (#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
realyze committed Mar 7, 2024
1 parent 1b8ab19 commit 6111b09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-numbers-prove.md
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Reduce memory overhead of tracking dependencies
9 changes: 6 additions & 3 deletions packages/mobx/src/core/derivation.ts
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(
// Reserve constant space for initial dependencies, dynamic space otherwise.
// See https://github.com/mobxjs/mobx/pull/3833
derivation.runId_ === 0 ? 100 : derivation.observing_.length
)
derivation.unboundDepsCount_ = 0
derivation.runId_ = ++globalState.runId
const prevTracking = globalState.trackingDerivation
Expand Down

0 comments on commit 6111b09

Please sign in to comment.