From c6bbb440115686e7bd936aeeab571788fa9f27e6 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sat, 25 Oct 2025 12:52:13 +0000 Subject: [PATCH] Optimize SearchQueryVector.as_dict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces a dictionary comprehension approach with direct conditional assignment, eliminating the overhead of creating an intermediate dictionary and iterating through it. **Key Changes:** - **Eliminated intermediate dictionary creation**: The original code created a full dictionary with all fields first, then filtered out `None` values using a dictionary comprehension - **Direct conditional construction**: The optimized version builds the result dictionary incrementally, only adding keys when their values are not `None` - **Single attribute access**: Each attribute (`self.values`, `self.sparse_values`, `self.sparse_indices`) is accessed only once and stored in a local variable **Performance Benefits:** The line profiler shows the original dictionary comprehension (`{k: v for k, v in d.items() if v is not None}`) consumed 60% of the total runtime. This operation involved: 1. Creating `d.items()` iterator 2. Checking each key-value pair for `None` 3. Building a new dictionary The optimized version eliminates this expensive iteration entirely, resulting in a **118% speedup** (from 50.7μs to 23.3μs). **Test Case Performance:** The optimization is particularly effective for cases with many `None` fields - the annotated tests show 158-203% improvements when most or all fields are `None`, as the optimized version skips unnecessary dictionary operations entirely. --- .../db_data/dataclasses/search_query_vector.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pinecone/db_data/dataclasses/search_query_vector.py b/pinecone/db_data/dataclasses/search_query_vector.py index d829102f6..4fa2ee350 100644 --- a/pinecone/db_data/dataclasses/search_query_vector.py +++ b/pinecone/db_data/dataclasses/search_query_vector.py @@ -30,9 +30,15 @@ def as_dict(self) -> dict: """ Returns the SearchQueryVector as a dictionary. """ - d = { - "values": self.values, - "sparse_values": self.sparse_values, - "sparse_indices": self.sparse_indices, - } - return {k: v for k, v in d.items() if v is not None} + # Directly construct the dictionary only with non-None attributes + result = {} + values = self.values + if values is not None: + result["values"] = values + sparse_values = self.sparse_values + if sparse_values is not None: + result["sparse_values"] = sparse_values + sparse_indices = self.sparse_indices + if sparse_indices is not None: + result["sparse_indices"] = sparse_indices + return result