Skip to content

define _compile_stateful_components as an external#6198

Merged
adhami3310 merged 2 commits intomainfrom
define-_compile_stateful_components-as-an-external
Mar 19, 2026
Merged

define _compile_stateful_components as an external#6198
adhami3310 merged 2 commits intomainfrom
define-_compile_stateful_components-as-an-external

Conversation

@adhami3310
Copy link
Copy Markdown
Member

No description provided.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 19, 2026

Greptile Summary

This PR refactors _compile_stateful_components in reflex/compiler/compiler.py by extracting its inner nested closure get_shared_components_recursive into a standalone module-level function with the accumulated state (rendered_components, all_import_dicts) passed as explicit parameters instead of being captured via closure. The logic is functionally identical — this is purely a structural/readability improvement that also makes the helper independently testable.

Key observations:

  • The extracted function get_shared_components_recursive is missing the underscore prefix (_) that all other private helper functions in this module use (e.g. _compile_document_root, _compile_memo_components), making it appear public when it is only used internally.
  • The function signature lacks a -> None return type annotation, which is generally expected for module-level functions that operate via side-effects on their arguments.
  • ParsedImportDict is correctly imported alongside the existing ImportVar import to support the new type annotation.
  • No behavioral changes are introduced; the benchmark test in tests/benchmarks/test_compilation.py continues to exercise _compile_stateful_components without modification.

Confidence Score: 4/5

  • Safe to merge — purely structural refactor with no logic changes; minor naming and annotation issues only.
  • The change is a straightforward extraction of a nested closure to module level with no semantic differences. The only concerns are a missing underscore prefix on the new function (inconsistent with module conventions) and a missing -> None return annotation — neither affects correctness or runtime behavior.
  • reflex/compiler/compiler.py — review the naming convention for the new module-level helper function.

Important Files Changed

Filename Overview
reflex/compiler/compiler.py Extracts the nested get_shared_components_recursive closure into a module-level function with explicit parameters; logic is functionally identical but the new function lacks an underscore prefix (inconsistent with module convention) and a -> None return type annotation.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["_compile_stateful_components(page_components)"] --> B["Initialize rendered_components: dict[str, None]\nInitialize all_import_dicts: list[ParsedImportDict]"]
    B --> C["For each page_component"]
    C --> D["get_shared_components_recursive(component, rendered_components, all_import_dicts)"]
    D --> E{"For each child\n(depth-first)"}
    E --> D
    E --> F{"isinstance StatefulComponent\n& references > 1\n& is_prod_mode()"}
    F -- No --> G["Skip"]
    F -- Yes --> H["Reset rendered_as_shared = False"]
    H --> I["Collect dynamic_imports → rendered_components"]
    I --> J["Collect custom_code → rendered_components"]
    J --> K["Append _get_all_imports → all_import_dicts"]
    K --> L["Set rendered_as_shared = True"]
    L --> C
    C --> M["merge_imports(*all_import_dicts)"]
    M --> N["Remove self-referential import"]
    N --> O["stateful_components_template(imports, memoized_code)"]
Loading

Last reviewed commit: "define _compile_stat..."

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 19, 2026

Merging this PR will improve performance by 3.14%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 7 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_compile_stateful[_stateful_page] 155.5 µs 150.7 µs +3.14%

Comparing define-_compile_stateful_components-as-an-external (99d59f4) with main (cb91b90)

Open in CodSpeed

@adhami3310 adhami3310 merged commit 6767870 into main Mar 19, 2026
47 checks passed
@adhami3310 adhami3310 deleted the define-_compile_stateful_components-as-an-external branch March 19, 2026 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants