perf: lazily allocate observers_ Set to reduce memory for unobserved atoms#4682
Open
gesposito wants to merge 1 commit into
Open
perf: lazily allocate observers_ Set to reduce memory for unobserved atoms#4682gesposito wants to merge 1 commit into
gesposito wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 923121d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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
force-pushed
the
perf/lazy-observers-allocation
branch
from
July 3, 2026 08:51
016e96b to
923121d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What / why
Every
AtomandComputedValueeagerly allocates an emptySetforobservers_, 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??=inaddObserver). The field initializer is kept so the slot stays in-object and the hidden class remains stable.getObserversreturns 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:@computedgettersThe 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
observers_.sizefor never-observed atoms; they were updated toobservers_?.size ?? 0(same intent, tolerates lazy allocation).mobx) is included.Code change checklist
/docs. For new functionality, at leastAPI.mdshould be updated — N/A, internal change only, no public API or documented behavior affectednpm -w mobx run test:performance)