Skip to content

Commit 7e71ba8

Browse files
authored
perf: use deque for BFS queue and voice task ordering (#2559)
1 parent fc284d4 commit 7e71ba8

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/agents/run_state.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import copy
66
import dataclasses
77
import json
8+
from collections import deque
89
from collections.abc import Callable, Mapping, Sequence
910
from dataclasses import dataclass, field
1011
from typing import TYPE_CHECKING, Any, Generic, Literal, Optional, Union, cast
@@ -2089,10 +2090,10 @@ def _build_agent_map(initial_agent: Agent[Any]) -> dict[str, Agent[Any]]:
20892090
Dictionary mapping agent names to agent instances.
20902091
"""
20912092
agent_map: dict[str, Agent[Any]] = {}
2092-
queue = [initial_agent]
2093+
queue: deque[Agent[Any]] = deque([initial_agent])
20932094

20942095
while queue:
2095-
current = queue.pop(0)
2096+
current = queue.popleft()
20962097
if current.name in agent_map:
20972098
continue
20982099
agent_map[current.name] = current

src/agents/voice/result.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import base64
5+
from collections import deque
56
from collections.abc import AsyncIterator
67
from typing import Any
78

@@ -52,9 +53,9 @@ def __init__(
5253
self._turn_text_buffer = ""
5354
self._queue: asyncio.Queue[VoiceStreamEvent] = asyncio.Queue()
5455
self._tasks: list[asyncio.Task[Any]] = []
55-
self._ordered_tasks: list[
56-
asyncio.Queue[VoiceStreamEvent | None]
57-
] = [] # New: list to hold local queues for each text segment
56+
self._ordered_tasks: deque[asyncio.Queue[VoiceStreamEvent | None]] = (
57+
deque()
58+
) # New: deque to hold local queues for each text segment
5859
self._dispatcher_task: asyncio.Task[Any] | None = (
5960
None # Task to dispatch audio chunks in order
6061
)
@@ -248,7 +249,7 @@ async def _dispatch_audio(self):
248249
break
249250
await asyncio.sleep(0)
250251
continue
251-
local_queue = self._ordered_tasks.pop(0)
252+
local_queue = self._ordered_tasks.popleft()
252253
while True:
253254
chunk = await local_queue.get()
254255
if chunk is None:

0 commit comments

Comments
 (0)