Repository navigation
fix: collect the hooks and imports of the string form Bare renders for objects - #7944
benedikt-bartscher wants to merge 7 commits into
Conversation
[Medium impact] The PR appears safe to merge; the previous missing-import issue is fixed. Summary
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 |
Merging this PR will degrade performance by 4.42%3 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
…those of the original var
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).
There was a problem hiding this comment.
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
…sal, so their imports count too
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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>
Barerenders an object or boolean var throughto_string(), but_get_varsyields only the original var. Hooks and imports are collected from_get_vars, so when a var subclass overridesto_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:This PR renders through one
_rendered_contents()helper and also yields the rendered var from_get_varswhen it differs from the contents, so its hooks and imports reach the page. It dropsBare._get_all_imports:Component._get_importsalready collects the imports of every var_get_varsyields, 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 defaultto_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.