perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165
Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Member
|
Please fix the pipeline. |
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.
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. |
Author
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
Author
|
Pipeline is green now — all checks passing across all Python versions (3.10–3.14), pre-commit, readme-snippets, and conformance checks. ✅ |
Author
|
All CI checks pass. Ready for review. |
Author
|
@Kludex Pipeline is now green — all 26 checks passing. Ready for review when you have a moment. |
Author
|
Pipeline is green now ✅. All checks passing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
InMemoryTaskMessageQueue.dequeue()useslist.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]withcollections.deque[QueuedMessage]so thatpopleft()runs in O(1) amortised time.append(), indexed access (queue[0]forpeek), and iteration (list(queue)forclear) 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