Skip to content

⚡️ Speed up function find_last_node by 19,011% #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/dsa/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# derived from https://github.com/langflow-ai/langflow/pull/5261
def find_last_node(nodes, edges):
"""This function receives a flow and returns the last node."""
return next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None)
# Build set of all source node IDs for O(1) lookup
source_ids = {e["source"] for e in edges}
return next((n for n in nodes if n["id"] not in source_ids), None)


# Function to find all leaf nodes (nodes with no outgoing edges)
Expand Down