Skip to content

perf: use deque for BFS queues and FIFO buffers across core modules#61394

Open
giulio-leone wants to merge 2 commits intoray-project:masterfrom
giulio-leone:fix/deque-fifo-queues
Open

perf: use deque for BFS queues and FIFO buffers across core modules#61394
giulio-leone wants to merge 2 commits intoray-project:masterfrom
giulio-leone:fix/deque-fifo-queues

Conversation

@giulio-leone
Copy link

Problem

Four files use .pop(0) for FIFO queue processing — BFS traversals, actor pool scheduling, and log file monitoring. Each .pop(0) on a Python list is O(n) because it shifts all remaining elements.

Solution

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

Changes

File Pattern
python/ray/dag/dag_node.py BFS graph traversal in traverse_and_apply()
python/ray/dag/compiled_dag_node.py BFS frontier traversal
python/ray/util/actor_pool.py Pending submits FIFO queue in ActorPool
python/ray/_private/log_monitor.py Open/closed file info FIFO queues in LogMonitor

All existing .append() and len() calls work identically on deque.

@giulio-leone giulio-leone requested a review from a team as a code owner February 28, 2026 05:22
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a performance optimization by replacing list-based FIFO queues with collections.deque across four core modules. The changes correctly use popleft() for O(1) dequeuing operations, which should improve performance for BFS traversals and other FIFO buffer processing as intended. The implementation is clean and consistent across all modified files. I've added one suggestion to use typing.Deque for type hints to ensure compatibility with Python versions older than 3.9. Otherwise, the changes look great.

@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Feb 28, 2026
@giulio-leone giulio-leone force-pushed the fix/deque-fifo-queues branch 2 times, most recently from f7628ba to 2ce77de Compare February 28, 2026 15:57
giulio-leone added a commit to giulio-leone/ray that referenced this pull request Mar 1, 2026
Copilot AI review requested due to automatic review settings March 1, 2026 05:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes multiple O(n) FIFO operations (list.pop(0)) across Ray’s Python core modules by switching those queues to collections.deque and using popleft() for O(1) front removal, improving performance in BFS traversals and log/actor scheduling loops.

Changes:

  • Replace list-based BFS queues with deque in DAG traversal/compilation code paths.
  • Replace ActorPool’s pending-submission FIFO list with deque.
  • Replace LogMonitor’s open/closed file FIFO lists with deque and update pop logic to popleft().

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
python/ray/util/actor_pool.py Use deque for _pending_submits and popleft() when scheduling next work.
python/ray/dag/dag_node.py Use deque for BFS queue in traverse_and_apply().
python/ray/dag/compiled_dag_node.py Use deque for BFS frontier traversal in compilation.
python/ray/_private/log_monitor.py Use deque for file-info FIFOs and switch .pop(0) to .popleft().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1 to 2
from __future__ import annotations

Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from __future__ import annotations is unrelated to the deque perf change and changes module-level annotation semantics. Since this repo targets Python >=3.10 (see python/setup.py), it isn't needed for deque[...] annotations here—consider removing it to keep behavior/surface area unchanged.

Suggested change
from __future__ import annotations

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — no from __future__ import annotations was present in this file (confirmed via diff against master), so nothing to remove. The other two comments are addressed in commit 6223fa4.

import time
import traceback
from collections import deque
from typing import Callable, List, Optional, Set
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List is imported from typing but no longer used after switching these buffers to deque. Please drop the unused import to avoid lint/type-check failures.

Suggested change
from typing import Callable, List, Optional, Set
from typing import Callable, Optional, Set

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — removed unused List import from typing. See commit 6223fa4.

giulio-leone added a commit to giulio-leone/ray that referenced this pull request Mar 1, 2026
Replace list-based queues with collections.deque for O(1) popleft()
operations in BFS traversals and FIFO buffers:
- dag_node.py: BFS traversal in _apply_recursive
- compiled_dag_node.py: BFS traversal in _build_compiled_dag
- actor_pool.py: pending submits FIFO buffer
- log_monitor.py: open/closed file info buffers

Refs: ray-project#61394
@giulio-leone giulio-leone force-pushed the fix/deque-fifo-queues branch from d24294c to c83eef9 Compare March 1, 2026 14:54
Four files drain lists front-to-back via .pop(0) in BFS traversals,
actor pool scheduling, and log file monitoring.  Each .pop(0) is O(n);
switching to collections.deque with .popleft() gives O(1) removal.

Files changed:
- python/ray/dag/dag_node.py: BFS graph traversal
- python/ray/dag/compiled_dag_node.py: BFS frontier traversal
- python/ray/util/actor_pool.py: pending submits FIFO queue
- python/ray/_private/log_monitor.py: open/closed file info queues

Signed-off-by: g97iulio1609 <giulio97.leone@gmail.com>
@giulio-leone giulio-leone force-pushed the fix/deque-fifo-queues branch from c83eef9 to 26aff5e Compare March 1, 2026 20:50
- Remove unused `List` import from typing after switching to deque
- Update LogMonitor docstrings from list[LogFileInfo] to deque[LogFileInfo]

Refs: ray-project#61394
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Mar 2, 2026
@edoakes
Copy link
Collaborator

edoakes commented Mar 2, 2026

@giulio-leone please sign off your commits to fix the DCO build. Will need to amend existing ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants