Skip to content

fix: collect the hooks and imports of the string form Bare renders for objects - #7944

Open
benedikt-bartscher wants to merge 7 commits into
reflex-dev:mainfrom
benedikt-bartscher:bare-rendered-var-deps
Open

benedikt-bartscher wants to merge 7 commits into
reflex-dev:mainfrom
benedikt-bartscher:bare-rendered-var-deps

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Bare renders an object or boolean var through to_string(), but _get_vars yields only the original var. Hooks and imports are collected from _get_vars, so when a var subclass overrides to_string() with an expression that needs a hook or an import the original var does not have, the page calls it without declaring it and fails in the browser:

class PickedVar(ObjectVar[Texts], python_types=Texts):
    def to_string(self, use_json: bool = True) -> StringVar:
        return pick(self)  # needs `const [pick_] = usePick()`


rx.text(State.texts)  # renders `pick_(...)`, but usePick is never imported or called

This PR renders through one _rendered_contents() helper and also yields the rendered var from _get_vars when it differs from the contents, so its hooks and imports reach the page. It drops Bare._get_all_imports: Component._get_imports already collects the imports of every var _get_vars yields, and the override replaced each library's list with the original var's, which lost a name the string form imports from the same library. The default to_string() of objects and booleans needs neither, so output for existing apps is unchanged.

The use case is translated data in the i18n PR (#6796): a mapping of locale to text whose string form picks the text for the active locale, so rx.text(product.name) shows one language and follows a locale switch.

@greptile-apps

greptile-apps Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous missing-import issue is fixed.

Summary

Bare now collects the hooks, imports, and components needed by an object or boolean Var’s string form.

  • Bare includes custom string-form dependencies when it renders object and boolean values.

The previous missing-import finding is fixed. No new actionable issues were found.

Reviews (8) · Last reviewed commit: "Reach the components of a Bare's rendere..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.42%

3 warnings

⚠️ 17 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

❌ 1 regressed benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_state_access[write_own] 122.2 ms 127.8 ms -4.42%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:bare-rendered-var-deps (e9ef4d7) with main (df7f157)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 20, 2026 07:43
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 20, 2026 07:43

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

View guided diff | Re-trigger cubic

Bare._get_vars, _render and render each rebuilt the rendered var, so a
boolean or object Bare made a new to_string() var on every call (about four
_get_vars calls per Bare in a page compile), and each new var missed the
_get_all_var_data cache. The contents and rendered var now live in the base
class's _vars_cache, which _clear_compile_caches and deepcopy already drop,
keyed on the contents so a reassigned contents is rendered afresh.

Per call, bool/object: _get_vars 2.9 us -> 0.16 us, render 4.6 us -> 0.77 us;
string/literal _get_vars 246 ns -> 155 ns. A full page compile with 400 Bares
(200 bool/object) goes from ~48 ms to ~45 ms (~42 ms before this PR, the
rest being the rendered var's hooks and imports this PR now collects).

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/reflex-components-core/src/reflex_components_core/base/bare.py">

<violation number="1" location="packages/reflex-components-core/src/reflex_components_core/base/bare.py:128">
P2: This cached property becomes stale when `Bare.contents` is reassigned. `_contents_vars()` rebuilds the rendered var for a new contents identity, but imports, refs, custom code, dynamic imports, and compiler traversal still use the old component list; avoid caching this derived property or invalidate it whenever `contents` changes.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic


def _get_all_refs(self) -> dict[str, None]:
"""Get the refs for the children of the component.
@functools.cached_property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This cached property becomes stale when Bare.contents is reassigned. _contents_vars() rebuilds the rendered var for a new contents identity, but imports, refs, custom code, dynamic imports, and compiler traversal still use the old component list; avoid caching this derived property or invalidate it whenever contents changes.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/reflex-components-core/src/reflex_components_core/base/bare.py, line 128:

<comment>This cached property becomes stale when `Bare.contents` is reassigned. `_contents_vars()` rebuilds the rendered var for a new contents identity, but imports, refs, custom code, dynamic imports, and compiler traversal still use the old component list; avoid caching this derived property or invalidate it whenever `contents` changes.</comment>

<file context>
@@ -146,16 +125,22 @@ def _get_all_app_wrap_components(
 
-    def _get_all_refs(self) -> dict[str, None]:
-        """Get the refs for the children of the component.
+    @functools.cached_property
+    def _get_component_prop_property(self) -> Sequence[BaseComponent]:
+        """The components of the props and of the var rendered for the contents.
</file context>

This branch has not been deployed

No deployments
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.

1 participant