Skip to content

Commit 9dec311

Browse files
wip - testing compile cache
1 parent 5d1d6db commit 9dec311

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

reflex/components/base/bare.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ def _get_all_hooks(self) -> dict[str, VarData | None]:
9595
Returns:
9696
The hooks for the component.
9797
"""
98+
if (cached := self.__dict__.get("_cached_all_hooks")) is not None:
99+
return cached
98100
hooks = super()._get_all_hooks()
99101
if isinstance(self.contents, Var):
100102
for component in _components_from_var(self.contents):
101103
hooks |= component._get_all_hooks()
104+
self.__dict__["_cached_all_hooks"] = hooks
102105
return hooks
103106

104107
def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
@@ -110,11 +113,18 @@ def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
110113
Returns:
111114
The imports for the component.
112115
"""
116+
if (
117+
not collapse
118+
and (cached := self.__dict__.get("_cached_all_imports")) is not None
119+
):
120+
return cached
113121
imports = super()._get_all_imports(collapse=collapse)
114122
if isinstance(self.contents, Var):
115123
var_data = self.contents._get_all_var_data()
116124
if var_data:
117125
imports |= {k: list(v) for k, v in var_data.imports}
126+
if not collapse:
127+
self.__dict__["_cached_all_imports"] = imports
118128
return imports
119129

120130
def _get_all_dynamic_imports(self) -> set[str]:
@@ -135,10 +145,13 @@ def _get_all_custom_code(self) -> dict[str, None]:
135145
Returns:
136146
The custom code.
137147
"""
148+
if (cached := self.__dict__.get("_cached_all_custom_code")) is not None:
149+
return cached
138150
custom_code = super()._get_all_custom_code()
139151
if isinstance(self.contents, Var):
140152
for component in _components_from_var(self.contents):
141153
custom_code |= component._get_all_custom_code()
154+
self.__dict__["_cached_all_custom_code"] = custom_code
142155
return custom_code
143156

144157
def _get_all_app_wrap_components(
@@ -196,14 +209,19 @@ def render(self) -> dict:
196209
Returns:
197210
The rendered component.
198211
"""
212+
if (cached := self.__dict__.get("_cached_render")) is not None:
213+
return cached
199214
contents = (
200215
Var.create(self.contents)
201216
if not isinstance(self.contents, Var)
202217
else self.contents
203218
)
204219
if isinstance(contents, (BooleanVar, ObjectVar)):
205-
return {"contents": f"{contents.to_string()!s}"}
206-
return {"contents": f"{contents!s}"}
220+
result = {"contents": f"{contents.to_string()!s}"}
221+
else:
222+
result = {"contents": f"{contents!s}"}
223+
self.__dict__["_cached_render"] = result
224+
return result
207225

208226
def _add_style_recursive(
209227
self, style: ComponentStyle, theme: Component | None = None

reflex/components/component.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,13 +1223,16 @@ def render(self) -> dict:
12231223
Returns:
12241224
The dictionary for template of component.
12251225
"""
1226+
if (cached := self.__dict__.get("_cached_render")) is not None:
1227+
return cached
12261228
tag = self._render()
12271229
rendered_dict = dict(
12281230
tag.set(
12291231
children=[child.render() for child in self.children],
12301232
)
12311233
)
12321234
self._replace_prop_names(rendered_dict)
1235+
self.__dict__["_cached_render"] = rendered_dict
12331236
return rendered_dict
12341237

12351238
def _replace_prop_names(self, rendered_dict: dict) -> None:
@@ -1496,6 +1499,8 @@ def _get_all_custom_code(self) -> dict[str, None]:
14961499
Returns:
14971500
The custom code.
14981501
"""
1502+
if (cached := self.__dict__.get("_cached_all_custom_code")) is not None:
1503+
return cached
14991504
# Store the code in a set to avoid duplicates.
15001505
code: dict[str, None] = {}
15011506

@@ -1517,6 +1522,7 @@ def _get_all_custom_code(self) -> dict[str, None]:
15171522
code |= child._get_all_custom_code()
15181523

15191524
# Return the code.
1525+
self.__dict__["_cached_all_custom_code"] = code
15201526
return code
15211527

15221528
def _get_dynamic_imports(self) -> str | None:
@@ -1648,10 +1654,18 @@ def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
16481654
Returns:
16491655
The import dict with the required imports.
16501656
"""
1657+
if (
1658+
not collapse
1659+
and (cached := self.__dict__.get("_cached_all_imports")) is not None
1660+
):
1661+
return cached
16511662
imports_ = imports.merge_parsed_imports(
16521663
self._get_imports(), *[child._get_all_imports() for child in self.children]
16531664
)
1654-
return imports.collapse_imports(imports_) if collapse else imports_
1665+
result = imports.collapse_imports(imports_) if collapse else imports_
1666+
if not collapse:
1667+
self.__dict__["_cached_all_imports"] = result
1668+
return result
16551669

16561670
def _get_mount_lifecycle_hook(self) -> str | None:
16571671
"""Generate the component lifecycle hook.
@@ -1805,6 +1819,8 @@ def _get_all_hooks(self) -> dict[str, VarData | None]:
18051819
Returns:
18061820
The code that should appear just before returning the rendered component.
18071821
"""
1822+
if (cached := self.__dict__.get("_cached_all_hooks")) is not None:
1823+
return cached
18081824
code = {}
18091825

18101826
# Add the internal hooks for this component.
@@ -1821,6 +1837,7 @@ def _get_all_hooks(self) -> dict[str, VarData | None]:
18211837
for child in self.children:
18221838
code.update(child._get_all_hooks())
18231839

1840+
self.__dict__["_cached_all_hooks"] = code
18241841
return code
18251842

18261843
def get_ref(self) -> str | None:
@@ -2439,13 +2456,20 @@ def _render_stateful_code(
24392456
) -> str:
24402457
if not self.tag:
24412458
return ""
2459+
cache_key = (
2460+
"_cached_stateful_code_export" if export else "_cached_stateful_code"
2461+
)
2462+
if (cached := self.__dict__.get(cache_key)) is not None:
2463+
return cached
24422464
# Render the code for this component and hooks.
2443-
return stateful_component_template(
2465+
result = stateful_component_template(
24442466
tag_name=self.tag,
24452467
memo_trigger_hooks=self.memo_trigger_hooks,
24462468
component=self.component,
24472469
export=export,
24482470
)
2471+
self.__dict__[cache_key] = result
2472+
return result
24492473

24502474
@classmethod
24512475
def _fix_event_triggers(

reflex/components/core/cond.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ def render(self) -> dict:
7272
Returns:
7373
The dictionary for template of component.
7474
"""
75-
return {
75+
if (cached := self.__dict__.get("_cached_render")) is not None:
76+
return cached
77+
result = {
7678
"cond_state": str(self.cond),
7779
"true_value": self.children[0].render(),
7880
"false_value": self.children[1].render(),
7981
}
82+
self.__dict__["_cached_render"] = result
83+
return result
8084

8185
def add_imports(self) -> ImportDict:
8286
"""Add imports for the Cond component.

reflex/components/core/foreach.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,18 @@ def render(self):
163163
Returns:
164164
The dictionary for template of component.
165165
"""
166+
if (cached := self.__dict__.get("_cached_render")) is not None:
167+
return cached
166168
tag = self._render()
167169

168-
return dict(
170+
result = dict(
169171
tag,
170172
iterable_state=str(tag.iterable),
171173
arg_name=tag.arg_var_name,
172174
arg_index=tag.index_var_name,
173175
)
176+
self.__dict__["_cached_render"] = result
177+
return result
174178

175179

176180
foreach = Foreach.create

reflex/components/core/match.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ def render(self) -> dict:
279279
Returns:
280280
The dictionary for template of component.
281281
"""
282-
return dict(self._render())
282+
if (cached := self.__dict__.get("_cached_render")) is not None:
283+
return cached
284+
result = dict(self._render())
285+
self.__dict__["_cached_render"] = result
286+
return result
283287

284288
def add_imports(self) -> ImportDict:
285289
"""Add imports for the Match component.

0 commit comments

Comments
 (0)