- Notifications
You must be signed in to change notification settings - Fork1k
⚡️ Speed up methodUsage.opentelemetry_attributes
by 85%#2198
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
DouweM merged 3 commits intopydantic:mainfrommisrasaurabh1:codeflash/optimize-Usage.opentelemetry_attributes-md0bn5hjJul 17, 2025
Merged
⚡️ Speed up methodUsage.opentelemetry_attributes
by 85%#2198
DouweM merged 3 commits intopydantic:mainfrommisrasaurabh1:codeflash/optimize-Usage.opentelemetry_attributes-md0bn5hjJul 17, 2025
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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
Here is the optimized version of your program for maximal runtime efficiency and reduced memory allocations, especially for the bottleneck section.**Performance improvements:**- Avoided unconditional allocation and population of dictionary keys with possibly falsy values, which were later filtered out at return. This eliminates the need to create then filter unnecessary key-value pairs, and the dict comprehension at the end.- Reuses the string prefix instead of f-strings in the tight loop for minor efficiency improvement.- Now we only touch details if it's not None (no allocation of empty dict).- No post-process dict comprehension; only desired keys added directly.The function returns exactly the same result as the original, but avoids unnecessary work and memory churn.
8f63ba3
intopydantic:main 18 checks passed
Uh oh!
There was an error while loading.Please reload this page.
Sign up for freeto 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.
📄 85% (0.85x) speedup for
Usage.opentelemetry_attributes
inpydantic_ai_slim/pydantic_ai/usage.py
⏱️ Runtime :
2.09 milliseconds
→1.13 milliseconds
(best of77
runs)📝 Explanation and details
Here is the optimized version of your program for maximal runtime efficiency and reduced memory allocations, especially for the bottleneck section.
Performance improvements:
The function returns exactly the same result as the original, but avoids unnecessary work and memory churn.
✅Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
codeflash_concolic_t7q2nx26/tmpwofuw3vl/test_concolic_coverage.py::test_Usage_opentelemetry_attributes
codeflash_concolic_t7q2nx26/tmpwofuw3vl/test_concolic_coverage.py::test_Usage_opentelemetry_attributes_2
test_pytest_inlinesnapshotdisable_testsproviderstest_bedrock_py_testsproviderstest_google_gla_py_teststes__replay_test_0.py::test_pydantic_ai_usage_Usage_opentelemetry_attributes
test_pytest_inlinesnapshotdisable_teststest_messages_py_teststest_mcp_py_teststest_deps_py__replay_test_0.py::test_pydantic_ai_usage_Usage_opentelemetry_attributes
To edit these changes
git checkout codeflash/optimize-Usage.opentelemetry_attributes-md0bn5hj
and push.