Skip to content

Commit 04d2475

Browse files
fix: improve agent lookup from O(n) to O(1) using dict
1 parent fb5bd0d commit 04d2475

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

swarms/structs/hiearchical_swarm.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ class HierarchicalSwarm:
673673
director_model_name (str): Model name for the main director agent.
674674
add_collaboration_prompt (bool): Whether to add collaboration prompts to agents.
675675
director_feedback_on (bool): Whether director feedback is enabled.
676+
parallel_execution (bool): Whether to execute agent tasks in parallel (default: True).
676677
"""
677678

678679
def __init__(
@@ -696,7 +697,7 @@ def __init__(
696697
planning_enabled: bool = True,
697698
autosave: bool = True,
698699
verbose: bool = False,
699-
parallel_execution: bool = False,
700+
parallel_execution: bool = True,
700701
agent_as_judge: bool = False,
701702
judge_agent_model_name: str = "gpt-4o-mini",
702703
*args,
@@ -721,6 +722,7 @@ def __init__(
721722
director_feedback_on (bool): Whether director feedback is enabled.
722723
autosave (bool): Whether to enable autosaving of conversation history.
723724
verbose (bool): Whether to enable verbose logging.
725+
parallel_execution (bool): Whether to execute agent tasks in parallel (default: True).
724726
*args: Additional positional arguments.
725727
**kwargs: Additional keyword arguments.
726728
@@ -731,6 +733,12 @@ def __init__(
731733
self.description = description
732734
self.director = director
733735
self.agents = agents
736+
# O(1) agent lookup via dict - keyed by agent_name
737+
self.agent_map = {
738+
agent.agent_name: agent
739+
for agent in (agents or [])
740+
if hasattr(agent, "agent_name")
741+
}
734742
self.max_loops = max_loops
735743
self.output_type = output_type
736744
self.feedback_director_model_name = (
@@ -1487,15 +1495,8 @@ def call_single_agent(
14871495
Exception: If agent execution fails.
14881496
"""
14891497
try:
1490-
# Find agent by name
1491-
agent = None
1492-
for a in self.agents:
1493-
if (
1494-
hasattr(a, "agent_name")
1495-
and a.agent_name == agent_name
1496-
):
1497-
agent = a
1498-
break
1498+
# Find agent by name - O(1) lookup via dict
1499+
agent = self.agent_map.get(agent_name)
14991500

15001501
if agent is None:
15011502
available_agents = [

0 commit comments

Comments
 (0)