ENG-10101: perf(memo): evaluate passthrough bodies once and reuse their analysis
Merged
FA
2mo ago+428+428+012345678901234567890123456789-52-52-01234567890123456789
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_memobuilds the passthrough definition directly from the fixedchildrensignature (_PASSTHROUGH_PARAMS) and evaluates the body once. Previously it evaluated once to derive the tag and again inside_create_component_definition.component_hashretains the rendered body and the artifacts it already collected (imports, internal and added hooks, custom code, dynamic import, app wraps) in a_MemoBodyAnalysison 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_memoreuses 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:
| state | compile |
|---|---|
| main | 46.5 to 47.1 s |
| after #7121 + #7122 | 39.3 to 39.6 s |
| this PR | 37.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 cleanmainhere.pyi_hashes.jsonregenerated; 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.