Skip to content

perf: lazily allocate observers_ Set to reduce memory for unobserved atoms#4682

Open
gesposito wants to merge 1 commit into
mobxjs:mainfrom
gesposito:perf/lazy-observers-allocation
Open

perf: lazily allocate observers_ Set to reduce memory for unobserved atoms#4682
gesposito wants to merge 1 commit into
mobxjs:mainfrom
gesposito:perf/lazy-observers-allocation

Conversation

@gesposito

@gesposito gesposito commented Jul 3, 2026

Copy link
Copy Markdown

What / why

Every Atom and ComputedValue eagerly allocates an empty Set for observers_, costing ~160 bytes each. In large stores most atoms are never observed (e.g. hydrated data that no reaction reads), so this memory is wasted. This PR allocates the Set lazily on first observer instead (observers_: Set<IDerivation> | null = null, allocated via ??= in addObserver). The field initializer is kept so the slot stays in-object and the hidden class remains stable.

getObservers returns a shared frozen-by-convention empty Set when unallocated; its only caller (getObserverTree) reads it without mutating, so there is no behavioral change.

Benchmarks

From npm -w mobx run test:performance (Node 26, macOS, both sides rebuilt before measuring). Decorator benchmarks, 50k instances with 10 observable fields:

Scenario main heap this PR change
10 @computed getters 129.3 MB 122.0 MB −6%
Hydrated partial (3 of 10 fields assigned) 152.6 MB 123.6 MB −19%
Hydrated full (all 10 assigned) 223.9 MB 144.2 MB −36%

The hydrated-full scenario also improves in time: construct 155→132 ms, re-read 60→28 ms. The proxy and legacy perf suites (37 tests each) pass with timings within run-to-run noise of main, including the "observable with many observers + dispose" case that exercises the lazy allocation path hardest.

Notes for review

  • Three existing tests asserted on the internal observers_.size for never-observed atoms; they were updated to observers_?.size ?? 0 (same intent, tolerates lazy allocation).
  • A changeset (patch bump for mobx) is included.

Code change checklist

  • Added/updated unit tests
  • Updated /docs. For new functionality, at least API.md should be updated — N/A, internal change only, no public API or documented behavior affected
  • Verified that there is no significant performance drop (npm -w mobx run test:performance)

@changeset-bot

changeset-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 923121d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mobx Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gesposito
gesposito force-pushed the perf/lazy-observers-allocation branch from 016e96b to 923121d Compare July 3, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant