Skip to content

Conversation

@misrasaurabh1
Copy link
Contributor

📄 85% (0.85x) speedup for Usage.opentelemetry_attributes in pydantic_ai_slim/pydantic_ai/usage.py

⏱️ Runtime : 2.09 milliseconds 1.13 milliseconds (best of 77 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:

  • 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.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 2004 Passed
⏪ Replay Tests 243 Passed
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from collections import namedtuple
# function to test
from copy import copy
from dataclasses import dataclass
from typing import Optional

# imports
import pytest  # used for our unit tests
from pydantic_ai.usage import Usage


# Dummy _utils for repr to avoid import error
class _utils:
    @staticmethod
    def dataclasses_no_defaults_repr(self):
        return f"Usage(requests={self.requests}, request_tokens={self.request_tokens}, response_tokens={self.response_tokens}, total_tokens={self.total_tokens}, details={self.details})"
from pydantic_ai.usage import Usage

# unit tests

# Basic Test Cases





def test_empty_usage():
    # All fields are None or 0, should return empty dict
    usage = Usage()
    codeflash_output = usage.opentelemetry_attributes(); attrs = codeflash_output # 1.76μs -> 668ns (163% faster)

# Edge Test Cases

















from copy import copy
from dataclasses import dataclass

# imports
import pytest  # used for our unit tests
from pydantic_ai.usage import Usage


# Dummy _utils for repr to avoid dependency on pydantic_ai._utils
class _utils:
    @staticmethod
    def dataclasses_no_defaults_repr(self):
        return f"Usage({self.requests}, {self.request_tokens}, {self.response_tokens}, {self.total_tokens}, {self.details})"
from pydantic_ai.usage import Usage

# unit tests

# 1. Basic Test Cases











def test_edge_all_none():
    # All fields None or default
    usage = Usage()
    codeflash_output = usage.opentelemetry_attributes(); attrs = codeflash_output # 1.73μs -> 614ns (181% faster)









def test_large_scale_tokens_and_details_none():
    # All fields None, large number of Usage objects
    usages = [Usage() for _ in range(1000)]
    for usage in usages:
        codeflash_output = usage.opentelemetry_attributes() # 417μs -> 178μs (134% faster)



from pydantic_ai.usage import Usage

def test_Usage_opentelemetry_attributes():
    Usage.opentelemetry_attributes(Usage(requests=0, request_tokens=None, response_tokens=None, total_tokens=None, details={'': -1}))

def test_Usage_opentelemetry_attributes_2():
    Usage.opentelemetry_attributes(Usage(requests=0, request_tokens=None, response_tokens=0, total_tokens=None, details=None))
⏪ Replay Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_t7q2nx26/tmpwofuw3vl/test_concolic_coverage.py::test_Usage_opentelemetry_attributes 2.32μs 1.41μs ✅65.2%
codeflash_concolic_t7q2nx26/tmpwofuw3vl/test_concolic_coverage.py::test_Usage_opentelemetry_attributes_2 1.81μs 691ns ✅163%
test_pytest_inlinesnapshotdisable_testsproviderstest_bedrock_py_testsproviderstest_google_gla_py_teststes__replay_test_0.py::test_pydantic_ai_usage_Usage_opentelemetry_attributes 138μs 82.8μs ✅67.6%
test_pytest_inlinesnapshotdisable_teststest_messages_py_teststest_mcp_py_teststest_deps_py__replay_test_0.py::test_pydantic_ai_usage_Usage_opentelemetry_attributes 9.00μs 4.72μs ✅90.4%

To edit these changes git checkout codeflash/optimize-Usage.opentelemetry_attributes-md0bn5hj and push.

Codeflash

codeflash-ai bot and others added 3 commits July 12, 2025 14:08
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.
@DouweM DouweM merged commit 8f63ba3 into pydantic:main Jul 17, 2025
18 checks passed
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.

2 participants