⚡️ Speed up method MCPClientBase.get_system_prompt by 9%
#118
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.
📄 9% (0.09x) speedup for
MCPClientBase.get_system_promptinsrc/mistralai/extra/mcp/base.py⏱️ Runtime :
5.69 milliseconds→5.22 milliseconds(best of169runs)📝 Explanation and details
Two micro-optimizations deliver a 9% runtime improvement and 3% throughput boost:
1. Simplified comparison in
_convert_content:Changed
not mcp_content.type == "text"tomcp_content.type != "text". This eliminates the overhead of thenotoperator and chained comparison, reducing execution time from 354.9ns to 337.8ns per hit (5% faster per call).2. Removed unnecessary
typing.castwrapper:Eliminated the
typing.cast()call in the list comprehension withinget_system_prompt. The cast provided no runtime value since the dictionary already matches the expected type structure. This reduces the overhead in the message processing loop, improving from 7.01ms to 5.20ms total time (26% faster for the list comprehension).The optimizations are particularly effective for workloads with:
typing.castscales linearly with message count_convert_contentcallsThese changes preserve all functionality while eliminating unnecessary Python overhead in hot code paths.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-MCPClientBase.get_system_prompt-mh4gofovand push.