Skip to content

Commit fbcc61c

Browse files
committed
avoid recursive calls for PASSTHROUGH memo components
1 parent 75b7a25 commit fbcc61c

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

packages/reflex-base/src/reflex_base/components/component.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ def render(self) -> dict:
13811381
self._cached_render_result = rendered_dict
13821382
return rendered_dict
13831383

1384-
def _get_component_hash(self) -> str:
1384+
def _get_component_hash(self, shallow: bool = False) -> str:
13851385
"""Get a stable content hash for this component.
13861386
13871387
The hash incorporates the rendered JSX dict plus the component's
@@ -1392,16 +1392,32 @@ def _get_component_hash(self) -> str:
13921392
components differing only in ``on_mount``, which is excluded
13931393
from ``_render`` props but lives in the lifecycle hook).
13941394
1395+
Args:
1396+
shallow: If True, only hash the component's own render output and
1397+
directly defined hooks, imports, custom code, and app-wrap
1398+
components, excluding any of those from child components.
1399+
13951400
Returns:
13961401
The hex digest content hash.
13971402
"""
13981403
hasher = md5(usedforsecurity=False)
13991404
_update_deterministic_hash(hasher, self.render())
1400-
_update_deterministic_hash(hasher, dict(self._get_all_imports()))
1401-
_update_deterministic_hash(hasher, dict(self._get_all_hooks_internal()))
1402-
_update_deterministic_hash(hasher, dict(self._get_all_hooks()))
1403-
_update_deterministic_hash(hasher, dict(self._get_all_custom_code()))
1404-
_update_deterministic_hash(hasher, dict(self._get_all_app_wrap_components()))
1405+
if shallow:
1406+
# For non-snapshot strategies, we only hash the component's own hooks, imports, custom code, and app-wrap components
1407+
_update_deterministic_hash(hasher, dict(self._get_imports()))
1408+
_update_deterministic_hash(hasher, dict(self._get_hooks_internal()))
1409+
_update_deterministic_hash(hasher, dict(self._get_added_hooks()))
1410+
_update_deterministic_hash(hasher, self._get_hooks())
1411+
_update_deterministic_hash(hasher, self._get_custom_code())
1412+
_update_deterministic_hash(hasher, dict(self._get_app_wrap_components()))
1413+
else:
1414+
_update_deterministic_hash(hasher, dict(self._get_all_imports()))
1415+
_update_deterministic_hash(hasher, dict(self._get_all_hooks_internal()))
1416+
_update_deterministic_hash(hasher, dict(self._get_all_hooks()))
1417+
_update_deterministic_hash(hasher, dict(self._get_all_custom_code()))
1418+
_update_deterministic_hash(
1419+
hasher, dict(self._get_all_app_wrap_components())
1420+
)
14051421
return hasher.hexdigest()
14061422

14071423
def _compute_memo_tag(self) -> str:
@@ -1418,8 +1434,16 @@ def _compute_memo_tag(self) -> str:
14181434
Returns:
14191435
The stable tag name.
14201436
"""
1437+
from reflex_base.components.memoize_helpers import (
1438+
MemoizationStrategy,
1439+
get_memoization_strategy,
1440+
)
1441+
1442+
comp_hash = self._get_component_hash(
1443+
shallow=get_memoization_strategy(self) == MemoizationStrategy.PASSTHROUGH
1444+
)
14211445
return format.format_state_name(
1422-
f"{type(self).__qualname__}_{self.tag or 'Comp'}_{self._get_component_hash()}"
1446+
f"{type(self).__qualname__}_{self.tag or 'Comp'}_{comp_hash}"
14231447
).capitalize()
14241448

14251449
def _replace_prop_names(self, rendered_dict: dict) -> None:

0 commit comments

Comments
 (0)