-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
⚡️ Speed up function calculate_node_levels
by 48x
#2136
base: main
Are you sure you want to change the base?
⚡️ Speed up function calculate_node_levels
by 48x
#2136
Conversation
To optimize the given `calculate_node_levels` function, we can make several changes focusing on reducing the number of nested loops and leveraging data structures more efficiently. Here is the revised function. 1. Replace list `queue` (with `pop(0)`) with `deque` which provides O(1) time complexity for append and pop operations. 2. Precompute method dependencies instead of repeatedly checking conditions inside loops. 3. Organize the steps for better readability and separate route processing into a helper function. Here is the rewritten function. Key optimizations. 1. Using `deque` instead of list `queue` to optimize appending and popping elements. 2. Precomputing listener dependencies reduces the number of checks and allows faster access. 3. Factoring out the router processing logic into the `process_router_paths` function improves readability and maintainability.
Co-authored-by: Aseem Saxena <[email protected]>
Disclaimer: This review was made by a crew of AI Agents. Code Review Comment for PR #2136OverviewThis pull request implements significant optimizations to the Key Changes Analysis1. Data Structure Optimization
2. Performance Optimizations
3. Code Organization
Detailed Review FindingsDocumentation ImprovementsThe docstring for def calculate_node_levels(flow: Any) -> Dict[str, int]:
"""
Calculate the hierarchical level of each node in the flow.
Performs a breadth-first traversal to assign levels starting from level 0.
- Parameters:
flow : Any
Flow object with _listeners, _routers, and _router_paths.
- Returns:
Dictionary mapping method names to hierarchical levels.
- Complexity:
Time: O(V + E), Space: O(V)
""" Type Hints EnhancementThe type hints can be improved for better type safety and clarity: from typing import Any, Deque, Dict, List, Optional, Set, Union, TypeVar
Flow = TypeVar('Flow')
def calculate_node_levels(flow: Flow) -> Dict[str, int]: Error HandlingAdding error handling in def process_router_paths(flow: Any, current: str, current_level: int, levels: Dict[str, int], queue: Deque[str]) -> None:
if not hasattr(flow, '_routers') or not hasattr(flow, '_router_paths'):
raise AttributeError("Flow object missing required attributes") Performance Optimization Suggestions
from functools import lru_cache
@lru_cache(maxsize=128)
def get_listener_dependencies(flow: Any) -> Tuple[Dict, Dict]:
import concurrent.futures
def process_level_parallel(nodes: List[str], flow: Any) -> Dict[str, int]: Potential Issues
Recommendations for Future Improvements
ConclusionThe improvements in this PR significantly enhance both performance and code organization. With the suggested enhancements—especially concerning type safety and error handling—the robustness and maintainability of the code will be notably strengthened. I recommend approval of this PR, contingent upon addressing minor documentation and type hint suggestions. |
📄 4,848% (48.48x) speedup for
calculate_node_levels
insrc/crewai/flow/utils.py
⏱️ Runtime :
58.0 milliseconds
→1.17 millisecond
(best of318
runs)📝 Explanation and details
To optimize the given
calculate_node_levels
function, we can make several changes focusing on reducing the number of nested loops and leveraging data structures more efficiently. Here is the revised function.queue
(withpop(0)
) withdeque
which provides O(1) time complexity for append and pop operations.Key optimizations.
deque
instead of listqueue
to optimize appending and popping elements.process_router_paths
function improves readability and maintainability.✅ Correctness verification report:
🌀 Generated Regression Tests Details