perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165
perf: use deque for InMemoryTaskMessageQueue FIFO operations#2165giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
|
Please fix the pipeline. |
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.
|
@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. |
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
|
Pipeline is green now — all checks passing across all Python versions (3.10–3.14), pre-commit, readme-snippets, and conformance checks. ✅ |
|
All CI checks pass. Ready for review. |
|
@Kludex Pipeline is now green — all 26 checks passing. Ready for review when you have a moment. |
|
Pipeline is green now ✅. All checks passing. |
|
@giulio-leone in the future please do not open PRs when there is not already an issue. Also, do not ping maintainers over and over again, this can be seen as quite rude. Since this is such a simple change to experimental code I'll approve it anyway. |
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