From d20326d33a4f2f0c3ff5b1b99c6eb557acd23034 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 5 Feb 2026 00:11:59 +0000 Subject: [PATCH] Optimize BenchmarkDetail.to_dict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This optimization achieves a **30% runtime improvement** (from 581μs to 445μs) by replacing manual dictionary construction with direct access to the Pydantic dataclass's `__dict__` attribute. **Key Performance Gains:** 1. **Eliminates Repeated Attribute Lookups**: The original code performed 5 separate `self.attribute` lookups (one per field), each requiring Python's attribute resolution mechanism. The line profiler shows each lookup taking 200-250ns. By using `self.__dict__`, we access all fields in a single operation. 2. **Reduces Dictionary Construction Overhead**: The original approach created a new dictionary literal with explicit key-value pairs, which requires the interpreter to build the dictionary incrementally. The optimized version directly copies an existing dictionary (already maintained by Pydantic), which is a faster C-level operation. 3. **Leverages Pydantic's Internal Optimization**: Pydantic dataclasses automatically maintain a `__dict__` attribute with the exact field mappings we need. Using this pre-existing structure eliminates redundant work. **Performance Characteristics from Tests:** - **Small objects** (single calls): 3-47% faster per invocation, with edge cases like infinity values showing up to 46.9% improvement - **Repeated conversions**: The `test_large_scale_multiple_conversions` test shows 31-32% speedup when calling `to_dict()` 1000 times, demonstrating consistent performance gains - **All data types preserved**: Tests confirm that special float values (NaN, inf, -inf), Unicode strings, and edge cases maintain correctness **Trade-off**: The optimization relies on Pydantic's `__dict__` containing exactly the fields we want to expose. This is safe for this dataclass since all fields are explicitly defined and should be serialized, but it couples the serialization to Pydantic's internal representation rather than being explicitly declarative about which fields to include. This optimization is particularly valuable if `BenchmarkDetail.to_dict()` is called frequently during benchmark result aggregation or reporting workflows. --- codeflash/models/models.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/codeflash/models/models.py b/codeflash/models/models.py index 9234a3256..a50c8143b 100644 --- a/codeflash/models/models.py +++ b/codeflash/models/models.py @@ -210,13 +210,7 @@ def to_string(self) -> str: ) def to_dict(self) -> dict[str, Any]: - return { - "benchmark_name": self.benchmark_name, - "test_function": self.test_function, - "original_timing": self.original_timing, - "expected_new_timing": self.expected_new_timing, - "speedup_percent": self.speedup_percent, - } + return self.__dict__.copy() @dataclass