ENG-10101: perf(memo): evaluate passthrough bodies once and reuse their analysis

Merged
FA
2mo ago
+428-52

Summary

Reduce repeated compile work by evaluating passthrough memo bodies once and reusing their render and collected artifacts during module emission. The prerequisite optimizations in #7121 and #7122 are already merged.

Benchmark coverage is in #7354. Merge that PR first and wait for its main CodSpeed run to finish, then update this branch from main to measure the optimization against the same workload.

  • create_passthrough_component_memo builds the passthrough definition directly from the fixed children signature (_PASSTHROUGH_PARAMS) and evaluates the body once. Previously it evaluated once to derive the tag and again inside _create_component_definition.
  • component_hash retains the rendered body and the artifacts it already collected (imports, internal and added hooks, custom code, dynamic import, app wraps) in a _MemoBodyAnalysis on the registration context, keyed by content hash. The component records its key in _memo_analysis_key (cleared with the other compile caches).
  • compile_experimental_component_memo reuses that analysis when the styled root still matches (_MemoBodyAnalysis.can_reuse: same class, default __copy__, equal style and style metadata) instead of re-rendering and re-collecting. Root prop/ref forwarding preserves the cached render across repeated emission.

Original measurements

Docs site dry compile (511 routes), warm runs:

statecompile
main46.5 to 47.1 s
after #7121 + #712239.3 to 39.6 s
this PR37.2 to 37.4 s

cProfile: create_passthrough_component_memo -7.5 s, compile_experimental_component_memo -2.0 s, _evaluate_memo_function calls 21.5k to 10.8k (profiled time; the full stack goes 112.5 s to 78.9 s profiled).

Test plan

  • New tests in tests/units/reflex_base/components/test_memo.py: analysis recorded and reused, invalidated on style change, isolated per registration context fork.
  • tests/units/reflex_base, tests/units/components, tests/units/compiler, benchmark compile tests green apart from failures that reproduce on clean main here.
  • pyi_hashes.json regenerated; ruff, pyright, pre-commit clean.
  • After resolving conflicts with main: 492 targeted memo, compiler, and registry tests pass, including a regression test for prop forwarding mutating cached analysis.
  • After extracting benchmark coverage to #7354: all 10 remaining page evaluation and compilation benchmark cases pass with --benchmark-disable.

https://claude.ai/code/session_01PmizE1eQhtYZyVs1RK2ke3