From 63636c10cd1bea48635dfb45846099b301d0c113 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 12 Feb 2025 16:12:28 -0800 Subject: [PATCH] import var perf improvements --- reflex/components/component.py | 49 ++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/reflex/components/component.py b/reflex/components/component.py index d27bddf782..4d215e3b9e 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -1208,7 +1208,7 @@ def _should_transpile(self, dep: str | None) -> bool: Returns: True if the dependency should be transpiled. """ - return ( + return bool(self.transpile_packages) and ( dep in self.transpile_packages or format.format_library_name(dep or "") in self.transpile_packages ) @@ -1291,10 +1291,10 @@ def _get_imports(self) -> ParsedImportDict: event_imports = Imports.EVENTS if self.event_triggers else {} # Collect imports from Vars used directly by this component. - var_datas = [var._get_all_var_data() for var in self._get_vars()] - var_imports: List[ImmutableParsedImportDict] = [ + var_datas = (var._get_all_var_data() for var in self._get_vars()) + var_imports: Iterator[ImmutableParsedImportDict] = ( var_data.imports for var_data in var_datas if var_data is not None - ] + ) added_import_dicts: list[ParsedImportDict] = [] for clz in self._iter_parent_classes_with_method("add_imports"): @@ -1566,8 +1566,24 @@ def import_var(self): An import var. """ # If the tag is dot-qualified, only import the left-most name. - tag = self.tag.partition(".")[0] if self.tag else None - alias = self.alias.partition(".")[0] if self.alias else None + tag = ( + ( + self.tag[:tag_index] + if (tag_index := self.tag.find(".")) != -1 + else self.tag + ) + if self.tag + else None + ) + alias = ( + ( + self.alias[:alias_index] + if (alias_index := self.alias.find(".")) != -1 + else self.alias + ) + if self.alias + else None + ) return ImportVar( tag=tag, is_default=self.is_default, @@ -2090,13 +2106,19 @@ def _get_hook_deps(hook: str) -> list[str]: A list of var names created by the hook declaration. """ # Ensure that the hook is a var declaration. - var_decl = hook.partition("=")[0].strip() + var_decl = ( + hook[:hook_index] if (hook_index := hook.find("=")) != -1 else hook + ).strip() + if not any(var_decl.startswith(kw) for kw in ["const ", "let ", "var "]): return [] # Extract the var name from the declaration. - _, _, var_name = var_decl.partition(" ") - var_name = var_name.strip() + var_name = ( + var_decl[var_decl_index + 1 :] + if (var_decl_index := var_decl.find(" ")) != -1 + else "" + ).strip() # Break up array and object destructuring if used. if var_name.startswith("[") or var_name.startswith("{"): @@ -2426,13 +2448,18 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) -> contents = tag["contents"][1:-1] if tag["contents"] else None - raw_tag_name = tag.get("name") + raw_tag_name: str = tag["name"] tag_name = Var(raw_tag_name or "Fragment") tag_name = ( Var.create(raw_tag_name) if raw_tag_name - and raw_tag_name.split(".")[0] not in imported_names + and ( + raw_tag_name[:raw_tag_name_index] + if (raw_tag_name_index := raw_tag_name.find(".")) != -1 + else raw_tag_name + ) + not in imported_names and raw_tag_name.lower() == raw_tag_name else tag_name )