From 1bf6c847b78e2024676899e7f896af67d4c20bfa Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Tue, 11 Feb 2025 03:02:48 -0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=20(codeflash)=20=E2=9A=A1=EF=B8=8F?= =?UTF-8?q?=20Speed=20up=20function=20`update=5Ftarget=5Fhandle`=20by=2027?= =?UTF-8?q?%=20in=20`src/backend/base/langflow/graph/graph/utils.py`=20(#5?= =?UTF-8?q?388)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ⚡️ Speed up function `update_target_handle` by 27% Sure, here are the optimized versions of the given functions. The key optimizations include avoiding redundant dictionary lookups, removing unnecessary condition checks, and simplifying the logic where possible. * lint fix * Apply suggestions from code review Co-authored-by: Christophe Bornet --------- Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com> Co-authored-by: Christophe Bornet --- .../base/langflow/graph/graph/utils.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/base/langflow/graph/graph/utils.py b/src/backend/base/langflow/graph/graph/utils.py index c3c28151b..2135cda9d 100644 --- a/src/backend/base/langflow/graph/graph/utils.py +++ b/src/backend/base/langflow/graph/graph/utils.py @@ -151,10 +151,13 @@ def update_target_handle(new_edge, g_nodes): dict: The updated edge. """ target_handle = new_edge["data"]["targetHandle"] - if target_handle.get("proxy"): - proxy_id = target_handle["proxy"]["id"] - if node := next((n for n in g_nodes if n["id"] == proxy_id), None): - set_new_target_handle(proxy_id, new_edge, target_handle, node) + if proxy := target_handle.get("proxy"): + proxy_id = proxy["id"] + for node in g_nodes: + if node["id"] == proxy_id: + set_new_target_handle(proxy_id, new_edge, target_handle, node) + break + return new_edge @@ -179,13 +182,18 @@ def set_new_target_handle(proxy_id, new_edge, target_handle, node) -> None: "type": type_, "id": proxy_id, } - if node["data"]["node"].get("flow"): + + node_data = node["data"]["node"] + if node_data.get("flow"): + field_template_proxy = node_data["template"][field]["proxy"] new_target_handle["proxy"] = { - "field": node["data"]["node"]["template"][field]["proxy"]["field"], - "id": node["data"]["node"]["template"][field]["proxy"]["id"], + "field": field_template_proxy["field"], + "id": field_template_proxy["id"], } + if input_types := target_handle.get("inputTypes"): new_target_handle["inputTypes"] = input_types + new_edge["data"]["targetHandle"] = new_target_handle