From 1b6675ef68b9e00d7d8d9b3da65479f61d6037d9 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 13 Feb 2025 12:38:01 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`Gra?= =?UTF-8?q?ph.find=5Frunnable=5Fpredecessors=5Ffor=5Fsuccessor`=20by=20129?= =?UTF-8?q?%=20in=20PR=20#6309=20(`fix-order-loop`)=20(#6310)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add is_loop property to Vertex class for detecting looping outputs * feat: improve vertex runnability logic for graph traversal - Update `is_vertex_runnable` to handle loop vertices more robustly - Modify `are_all_predecessors_fulfilled` to better manage cycle dependencies - Change adjacency maps to use sets for more efficient predecessor/successor tracking * ⚡️ Speed up method `Graph.find_runnable_predecessors_for_successor` by 129% in PR #6309 (`fix-order-loop`) Here's the optimized version of the program. ### Changes and Optimizations. * fix(serialization.py): update isinstance check for list and tuple to use union operator for better type checking --------- Co-authored-by: Gabriel Luiz Freitas Almeida Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com> --- src/backend/base/langflow/graph/graph/base.py | 12 ++++++------ .../base/langflow/serialization/serialization.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/backend/base/langflow/graph/graph/base.py b/src/backend/base/langflow/graph/graph/base.py index 7e57c274c..974a24935 100644 --- a/src/backend/base/langflow/graph/graph/base.py +++ b/src/backend/base/langflow/graph/graph/base.py @@ -1979,21 +1979,21 @@ class Graph: runnable_vertices = [] visited = set() - def find_runnable_predecessors(predecessor: Vertex) -> None: - predecessor_id = predecessor.id + def find_runnable_predecessors(predecessor_id: str) -> None: if predecessor_id in visited: return visited.add(predecessor_id) - is_active = self.get_vertex(predecessor_id).is_active() - is_loop = self.get_vertex(predecessor_id).is_loop + predecessor_vertex = self.get_vertex(predecessor_id) + is_active = predecessor_vertex.is_active() + is_loop = predecessor_vertex.is_loop if self.run_manager.is_vertex_runnable(predecessor_id, is_active=is_active, is_loop=is_loop): runnable_vertices.append(predecessor_id) else: for pred_pred_id in self.run_manager.run_predecessors.get(predecessor_id, []): - find_runnable_predecessors(self.get_vertex(pred_pred_id)) + find_runnable_predecessors(pred_pred_id) for predecessor_id in self.run_manager.run_predecessors.get(vertex_id, []): - find_runnable_predecessors(self.get_vertex(predecessor_id)) + find_runnable_predecessors(predecessor_id) return runnable_vertices def remove_from_predecessors(self, vertex_id: str) -> None: diff --git a/src/backend/base/langflow/serialization/serialization.py b/src/backend/base/langflow/serialization/serialization.py index 3a8c47b57..ed9f03b8a 100644 --- a/src/backend/base/langflow/serialization/serialization.py +++ b/src/backend/base/langflow/serialization/serialization.py @@ -111,7 +111,7 @@ def _truncate_value(value: Any, max_length: int | None, max_items: int | None) - """Truncate value based on its type and provided limits.""" if max_length is not None and isinstance(value, str) and len(value) > max_length: return value[:max_length] - if max_items is not None and isinstance(value, (list, tuple)) and len(value) > max_items: + if max_items is not None and isinstance(value, list | tuple) and len(value) > max_items: return value[:max_items] return value