1. Summary
Enhance the Neo.core.Effect system (or provide a utility around it) to support memoization for VDOM-generating methods. This will significantly improve rendering performance by caching the VDOM output and preventing unnecessary re-executions when component configs (inputs) have not changed.
2. Rationale
Functional components, driven by Neo.core.Effect, re-generate their VDOM whenever a tracked config changes. While efficient, re-generating complex VDOM trees can still be computationally intensive. By memoizing the output of pure VDOM-generating methods, we can avoid redundant work. If the inputs to createVdom() are the same as the last execution, the cached VDOM can be returned directly, bypassing the VDOM generation and worker communication steps.
3. Scope & Implementation Plan
- Memoization Mechanism: Design and implement a caching layer for
Neo.core.Effect instances (or a new MemoizedEffect class). This mechanism will:
- Store the last computed VDOM output.
- Efficiently compare current inputs (tracked configs) with previous inputs to determine if re-execution is necessary.
- Invalidate the cache when inputs change.
- Integration: Determine how developers will opt-in to memoization (e.g., a config on
FunctionalBase, a decorator, or a utility function).
- Performance Testing: Create benchmarks to measure the performance gains achieved through memoization, especially for components with complex VDOM structures or frequently updated but unchanged inputs.
4. Definition of Done
- A memoization mechanism for
Neo.core.Effect is implemented.
- Functional components can leverage memoization to improve rendering performance.
- Performance benchmarks demonstrate measurable gains.
5. Contract Ledger (Intake-Derived)
| Target Surface |
Source of Authority |
Proposed Behavior |
Fallback / Edge Case |
Docs |
Evidence |
Neo.functional.useMemo(fn, dependencies) (src/functional/useMemo.mjs, re-exported via src/functional/_export.mjs) |
Issue #6997 · Epic #6992 · src/functional/useConfig.mjs (Symbol.for('hooks') / Symbol.for('hookIndex') slot convention) |
Memoizes the return value of fn() in component[Symbol.for('hooks')][hookIndex++]. • Explicit mode (Array.isArray(dependencies)): inputs are dependencies ∪ trackedConfigs; reuses cached value when dependencies.length matches, every element passes Object.is(next, prev) || Neo.isEqual(next, prev), and every Neo.core.Config read inside fn passes Object.is(cfg.get(), prevVal) || cfg.isEqual(cfg.get(), prevVal) (re-registering each config on the active Effect). • Automatic mode (dependencies === undefined): tracks Neo.core.Config reads inside fn via EffectManager collector (trackedConfigs.size > 0) and reuses cached value when all tracked configs still pass Object.is(cfg.get(), prevVal) || cfg.isEqual(cfg.get(), prevVal). |
• Throws Error("useMemo must be called from within a functional component's createVdom method.") when no active Effect or owning component exists. • Calling other hooks (useConfig, useEvent, useMemo) inside fn is forbidden (cache hits skip fn, shifting subsequent hook slot indices). • In automatic mode, plain outer-scope variables captured from createVdom are not tracked unless passed in dependencies. |
Yes (src/functional/useMemo.mjs JSDoc, learn/comparisons/NeoVsReact.md) |
test/playwright/unit/functional/UseMemo.spec.mjs (6 Playwright unit tests including child-component VDOM reconciliation, explicit+Config invalidation, and 8,000-item sort/checksum 30-update interleaved benchmark) |
Neo.functional.component.Base#processVdomForComponents & #generateIds (src/functional/component/Base.mjs) |
Issue #6997 · src/functional/component/Base.mjs:403-547 |
Preserves existing child component instances in #nextChildComponents when encountering {componentId} references inside cached/memoized VDOM subtrees, and tracks auto-generated VDOM IDs via Symbol.for('vdomAutoId') so generateIds never overwrites {componentId} placeholders on cached renders. |
Unreferenced child components absent from #nextChildComponents continue to be destroyed in cleanup step 6. |
Yes (src/functional/component/Base.mjs JSDoc) |
test/playwright/unit/functional/UseMemo.spec.mjs (preserves child component instances inside a memoized VDOM subtree across unrelated parent re-renders) |
1. Summary
Enhance the
Neo.core.Effectsystem (or provide a utility around it) to support memoization for VDOM-generating methods. This will significantly improve rendering performance by caching the VDOM output and preventing unnecessary re-executions when component configs (inputs) have not changed.2. Rationale
Functional components, driven by
Neo.core.Effect, re-generate their VDOM whenever a tracked config changes. While efficient, re-generating complex VDOM trees can still be computationally intensive. By memoizing the output of pure VDOM-generating methods, we can avoid redundant work. If the inputs tocreateVdom()are the same as the last execution, the cached VDOM can be returned directly, bypassing the VDOM generation and worker communication steps.3. Scope & Implementation Plan
Neo.core.Effectinstances (or a newMemoizedEffectclass). This mechanism will:FunctionalBase, a decorator, or a utility function).4. Definition of Done
Neo.core.Effectis implemented.5. Contract Ledger (Intake-Derived)
Neo.functional.useMemo(fn, dependencies)(src/functional/useMemo.mjs, re-exported viasrc/functional/_export.mjs)#6997· Epic#6992·src/functional/useConfig.mjs(Symbol.for('hooks')/Symbol.for('hookIndex')slot convention)fn()incomponent[Symbol.for('hooks')][hookIndex++].• Explicit mode (
Array.isArray(dependencies)): inputs aredependencies ∪ trackedConfigs; reuses cached value whendependencies.lengthmatches, every element passesObject.is(next, prev) || Neo.isEqual(next, prev), and everyNeo.core.Configread insidefnpassesObject.is(cfg.get(), prevVal) || cfg.isEqual(cfg.get(), prevVal)(re-registering each config on the activeEffect).• Automatic mode (
dependencies === undefined): tracksNeo.core.Configreads insidefnviaEffectManagercollector (trackedConfigs.size > 0) and reuses cached value when all tracked configs still passObject.is(cfg.get(), prevVal) || cfg.isEqual(cfg.get(), prevVal).Error("useMemo must be called from within a functional component's createVdom method.")when no activeEffector owning component exists.• Calling other hooks (
useConfig,useEvent,useMemo) insidefnis forbidden (cache hits skipfn, shifting subsequent hook slot indices).• In automatic mode, plain outer-scope variables captured from
createVdomare not tracked unless passed independencies.src/functional/useMemo.mjsJSDoc,learn/comparisons/NeoVsReact.md)test/playwright/unit/functional/UseMemo.spec.mjs(6 Playwright unit tests including child-component VDOM reconciliation, explicit+Config invalidation, and 8,000-item sort/checksum 30-update interleaved benchmark)Neo.functional.component.Base#processVdomForComponents&#generateIds(src/functional/component/Base.mjs)#6997·src/functional/component/Base.mjs:403-547#nextChildComponentswhen encountering{componentId}references inside cached/memoized VDOM subtrees, and tracks auto-generated VDOM IDs viaSymbol.for('vdomAutoId')sogenerateIdsnever overwrites{componentId}placeholders on cached renders.#nextChildComponentscontinue to be destroyed in cleanup step 6.src/functional/component/Base.mjsJSDoc)test/playwright/unit/functional/UseMemo.spec.mjs(preserves child component instances inside a memoized VDOM subtree across unrelated parent re-renders)