Skip to content

Debug2 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 48 commits into
base: main
Choose a base branch
from
Draft

Debug2 #37

wants to merge 48 commits into from

Conversation

KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Jul 25, 2025

No description provided.

adtyavrdhn and others added 30 commits July 21, 2025 22:52
…sage part instead of stringifying (pydantic#2173)

Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
codeflash-ai bot added a commit that referenced this pull request Jul 25, 2025
…`debug2`)

Here's the optimized version of your program. The optimizations focus on.

- **Avoiding repeated `guess_type` lookups.** If `_infer_media_type` is called multiple times for the same instance, cache the result, as URL and thus media type do not change during the instance lifetime. This saves on repeated computation and any internal calls.
- **Micro-optimization:** Move the exception creation out of the main execution path.
- **Other imports and class hierarchy** stay unchanged as per your requirements.

All existing docstrings and code comments are preserved because your snippet doesn't have extra comments.




**Summary of changes:**
- Added `self._cached_media_type` to cache the result of mimetype guessing, improving performance when called repeatedly per instance.
- No changes to the function signatures, docstrings, or visible behavior.

If `_infer_media_type` is only called once per instance, the benefit is small, but if called multiple times, this saves time and avoids recomputation. This achieves optimal runtime without altering external behavior.
Copy link

codeflash-ai bot commented Jul 25, 2025

⚡️ Codeflash found optimizations for this PR

📄 20% (0.20x) speedup for DocumentUrl._infer_media_type in pydantic_ai_slim/pydantic_ai/messages.py

⏱️ Runtime : 14.7 milliseconds 12.3 milliseconds (best of 29 runs)

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch debug2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.