⚡️ Speed up method ResourceTemplate.matches by 139%#3
Open
codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom
codeflash/optimize-ResourceTemplate.matches-ma2xdwmiSaga4/python-sdk:codeflash/optimize-ResourceTemplate.matches-ma2xdwmiCopy head branch name to clipboard
Open
⚡️ Speed up method ResourceTemplate.matches by 139%#3codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom codeflash/optimize-ResourceTemplate.matches-ma2xdwmiSaga4/python-sdk:codeflash/optimize-ResourceTemplate.matches-ma2xdwmiCopy head branch name to clipboard
ResourceTemplate.matches by 139%#3codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom
codeflash/optimize-ResourceTemplate.matches-ma2xdwmiSaga4/python-sdk:codeflash/optimize-ResourceTemplate.matches-ma2xdwmiCopy head branch name to clipboard
Conversation
Here’s the optimized version of your program, using Python 3.11+ features, and your profiling results. The hottest spot is `re.match` (almost all the runtime), and the second hottest is the on-the-fly string replacement for the regex pattern. **Optimizations:** - **Precompile the regex**: Instead of reconstructing and recompiling the regex every call, compile it once and cache on the instance (in `__init__`). This eliminates repeated regex compilation and pattern construction. - **Efficient pattern construction**: Instead of chained `replace()` calls, use `re.sub` for more reliable and faster parsing, especially for many parameters. - **Thread-safe cache**: Since Pydantic models can be copied or mutated, cache the regex as a private attribute (`_compiled_uri_re`), initializing it once per instance. **Code (with comments preserved):** **Key points:** - Pattern is only constructed and compiled once per instance, not every call. - Matching now only needs `regex.match`, not string ops or compile. - Fully backward compatible (return values are identical).
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.
📄 139% (1.39x) speedup for
ResourceTemplate.matchesinsrc/mcp/server/fastmcp/resources/templates.py⏱️ Runtime :
50.4 microseconds→21.1 microseconds(best of132runs)📝 Explanation and details
Here’s the optimized version of your program, using Python 3.11+ features, and your profiling results. The hottest spot is
re.match(almost all the runtime), and the second hottest is the on-the-fly string replacement for the regex pattern.Optimizations:
__init__). This eliminates repeated regex compilation and pattern construction.replace()calls, usere.subfor more reliable and faster parsing, especially for many parameters._compiled_uri_re), initializing it once per instance.Code (with comments preserved):
Key points:
regex.match, not string ops or compile.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-ResourceTemplate.matches-ma2xdwmiand push.