⚡️ Speed up method DocumentUrl._infer_media_type by 12% in PR #35 (trigger-cf-workflow)
#36
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.
⚡️ This pull request contains optimizations for PR #35
If you approve this dependent PR, these changes will be merged into the original PR branch
trigger-cf-workflow.📄 12% (0.12x) speedup for
DocumentUrl._infer_media_typeinpydantic_ai_slim/pydantic_ai/messages.py⏱️ Runtime :
23.8 milliseconds→21.3 milliseconds(best of30runs)📝 Explanation and details
Here is an optimized version of your Python program. Major optimizations.
guess_typeper unique URL usingfunctools.lru_cache, which reduces repeated MIME type computations (especially on large scale repeated calls).FileUrl, it is best to avoid repeating the dataclass and repr decorators if already present in the parent (maintaining runtime correctness and consistency).Notes.
_guess_type_cachedhelper is a staticmethod, so it's shared across all instances and efficiently caches guess_type results.maxsize=Noneto cache unlimited.dataclassandreprdecorators are not required here becauseFileUrlalready establishes the base data model and behaviors for you.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr35-2025-07-25T03.11.24and push.