⚡️ Speed up method ResourceTemplate.from_function by 19%#2
Open
codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom
codeflash/optimize-ResourceTemplate.from_function-ma2x6cycSaga4/python-sdk:codeflash/optimize-ResourceTemplate.from_function-ma2x6cycCopy head branch name to clipboard
Open
⚡️ Speed up method ResourceTemplate.from_function by 19%#2codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom codeflash/optimize-ResourceTemplate.from_function-ma2x6cycSaga4/python-sdk:codeflash/optimize-ResourceTemplate.from_function-ma2x6cycCopy head branch name to clipboard
ResourceTemplate.from_function by 19%#2codeflash-ai[bot] wants to merge 1 commit intomainSaga4/python-sdk:mainfrom
codeflash/optimize-ResourceTemplate.from_function-ma2x6cycSaga4/python-sdk:codeflash/optimize-ResourceTemplate.from_function-ma2x6cycCopy head branch name to clipboard
Conversation
Here is an optimized version of your `ResourceTemplate.from_function` method. Key optimizations. - Avoid calling `fn.__doc__` and `"text/plain"` repeatedly inside the return statement by computing all needed default/fallback values before the object construction. - Only call `validate_call(fn)` if needed, assigning directly otherwise. - Use local variables to minimize attribute lookup (which can slightly help, though recent Pythons are better). - Assign the lambda check to a local variable for speed. - Avoid unnecessary string concatenation; defaults are computed with simple short-circuit logic. - The most expensive line is the `TypeAdapter(fn).json_schema()`, which is intrinsic to extracting the schema; there's no faster way using Pydantic v2, so this is kept but put as early as possible to fail fast. **All comments are preserved (except where lines have been improved/clarified).** **Net result:** - Reduced repeated lookups and conditionals in the return statement. - Branching/casting is “flattened” for best interpreter throughput. - Calls stay single-pass; main logic flow is not changed. - All side effects/return values remain exactly the same. If you need further runtime gains, consider caching the result of `TypeAdapter(fn)` at a higher level if you'll be constructing many templates for the same function object(s). Otherwise, this is already approaching the limits given the dependence on Pydantic's dynamic typing machinery.
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.
📄 19% (0.19x) speedup for
ResourceTemplate.from_functioninsrc/mcp/server/fastmcp/resources/templates.py⏱️ Runtime :
420 milliseconds→351 milliseconds(best of19runs)📝 Explanation and details
Here is an optimized version of your
ResourceTemplate.from_functionmethod.Key optimizations.
fn.__doc__and"text/plain"repeatedly inside the return statement by computing all needed default/fallback values before the object construction.validate_call(fn)if needed, assigning directly otherwise.TypeAdapter(fn).json_schema(), which is intrinsic to extracting the schema; there's no faster way using Pydantic v2, so this is kept but put as early as possible to fail fast.All comments are preserved (except where lines have been improved/clarified).
Net result:
If you need further runtime gains, consider caching the result of
TypeAdapter(fn)at a higher level if you'll be constructing many templates for the same function object(s). Otherwise, this is already approaching the limits given the dependence on Pydantic's dynamic typing machinery.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-ResourceTemplate.from_function-ma2x6cycand push.