Skip to content

perf: use deque for BFS queue in get_function_dependency() - #1952

Closed
giulio-leone wants to merge 1 commit into
FoundationAgents:mainfrom
giulio-leone:fix/sanitize-bfs-deque-performance
Closed

perf: use deque for BFS queue in get_function_dependency()#1952
giulio-leone wants to merge 1 commit into
FoundationAgents:mainfrom
giulio-leone:fix/sanitize-bfs-deque-performance

Conversation

@giulio-leone

Copy link
Copy Markdown

Problem

get_function_dependency() traverses the call graph via BFS using list.pop(0), which is O(n) per removal. For large call graphs this makes traversal O(n²).

Solution

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

Changes

  • metagpt/utils/sanitize.py: Import deque, init queue as deque([entrypoint]), use .popleft()

Testing

  • Syntax verified via ast.parse()

get_function_dependency() traverses the call graph via BFS using
list.pop(0), which is O(n) per removal.  Switch to collections.deque
with popleft() for O(1).
@giulio-leone

Copy link
Copy Markdown
Author

Hi! Gentle ping — this PR is rebased, CI passes, and ready for review. Happy to address any feedback. Thanks!

@giulio-leone

Copy link
Copy Markdown
Author

Closing — minor perf optimization, low impact.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant