Optimize getter performance by caching memoization symbols #118
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.
Optimize Getter Performance by Caching Memoization Symbols
Summary
This PR implements a symbol caching optimization in
FastGetBuilderto improve getter performance on read-only instances by eliminating repeatedSymbol.for()calls identified as a major bottleneck in CPU profiling.Performance Analysis
CPU profiling of getter benchmarks revealed that
Symbol.for()calls were a significant performance bottleneck in the memoization system. The profiles showed:Optimization Details
Before
Symbol.for()After
FastGetBuilderconstructionMap<string, symbol>for O(1) lookupSymbol.for()calls from the hot path entirelyImplementation Changes
Added symbol caching to FastGetBuilder:
memoSymbols: Map<string, symbol>- caches memoization symbolssnapshotSymbols: Map<string, symbol>- caches snapshot symbolsUpdated symbol access methods:
getMemoSymbol(property: string)- returns cached symbolgetSnapshotSymbol(property: string)- returns cached snapshot symbolModified getter generation:
buildViewGetter()now uses cached symbols instead ofSymbol.for()outerClosureStatements()uses cached symbols for prototype initializationTesting
getter-optimization.benchmark.tsfor targeted performance validationPerformance Impact
This optimization targets the most significant bottleneck identified in CPU profiles:
Symbol.for()string-to-symbol conversionsThe optimization specifically benefits read-only instances where getters are accessed frequently, which is the primary use case for mobx-quick-tree's performance advantages over MST.
Files Changed
src/fast-getter.ts- Symbol caching implementationbench/getter-optimization.benchmark.ts- New benchmark for validationLink to Devin run: https://app.devin.ai/sessions/326791da0b5c40d7b542d8781174b7c9
Requested by: Harry Brundage ([email protected])