Skip to content

perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165

Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone:fix/message-queue-deque
Open

perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone:fix/message-queue-deque

Conversation

@giulio-leone
Copy link

Problem

InMemoryTaskMessageQueue.dequeue() uses list.pop(0) which is O(n) — every remaining element must be shifted left on each call. For high-throughput task queues this becomes a bottleneck as the queue grows.

Solution

Replace the internal list[QueuedMessage] with collections.deque[QueuedMessage] so that popleft() runs in O(1) amortised time. append(), indexed access (queue[0] for peek), and iteration (list(queue) for clear) remain O(1)/O(n) respectively — no other API changes.

Testing

All 1122 tests pass (98 skipped, 1 xfailed).

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

@Kludex
Copy link
Member

Kludex commented Feb 28, 2026

Please fix the pipeline.

g97iulio1609 added 2 commits February 28, 2026 15:41
Replace list with collections.deque in InMemoryTaskMessageQueue so
that dequeue (popleft) runs in O(1) amortised time instead of the
O(n) incurred by list.pop(0), which must shift every remaining
element on each call.
@giulio-leone
Copy link
Author

@Kludex Pipeline is green — all checks pass (pre-commit, 20 test matrices across 3.10-3.14, conformance, readme-snippets). Let me know if you see any remaining issues.

@giulio-leone
Copy link
Author

Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks!

@giulio-leone
Copy link
Author

Pipeline is green now — all checks passing across all Python versions (3.10–3.14), pre-commit, readme-snippets, and conformance checks. ✅

@giulio-leone
Copy link
Author

All CI checks pass. Ready for review.

@giulio-leone
Copy link
Author

@Kludex Pipeline is now green — all 26 checks passing. Ready for review when you have a moment.

@giulio-leone
Copy link
Author

Pipeline is green now ✅. All checks passing.

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.

2 participants