Skip to content

perf: use deque for BFS queue and voice task ordering#2559

Merged
seratch merged 2 commits intoopenai:mainfrom
giulio-leone:fix/deque-performance
Feb 28, 2026
Merged

perf: use deque for BFS queue and voice task ordering#2559
seratch merged 2 commits intoopenai:mainfrom
giulio-leone:fix/deque-performance

Conversation

@giulio-leone
Copy link
Contributor

Problem

Two hot paths use list.pop(0) which is O(n) per removal:

  1. run_state.py_build_agent_map(): BFS traversal over agent handoff graph. Each .pop(0) shifts all remaining elements.
  2. voice/result.py_ordered_tasks: Voice pipeline drains ordered task queues front-to-back. Each .pop(0) is O(n).

Solution

Switch both to collections.deque with .popleft() for O(1) front removal.

Changes

  • src/agents/run_state.py: Import deque, use deque([initial_agent]) and .popleft()
  • src/agents/voice/result.py: Import deque, type _ordered_tasks as deque, use .popleft()

Testing

  • 153 run_state tests pass (2 consecutive runs)
  • Syntax verified via ast.parse()

- run_state.py: _build_agent_map() BFS traversal uses list.pop(0) which
  is O(n) per removal. Switch to collections.deque with popleft() for
  O(1).
- voice/result.py: _ordered_tasks consumed front-to-back via .pop(0),
  same O(n) issue. Switch to deque with popleft().

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@seratch
Copy link
Member

seratch commented Feb 28, 2026

Can you fix the lint error?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giulio-leone
Copy link
Contributor Author

Fixed — reformatted src/agents/voice/result.py with ruff format. Thanks for flagging!

@seratch seratch added this to the 0.10.x milestone Feb 28, 2026
@seratch seratch merged commit 7e71ba8 into openai:main Feb 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants