From 38fac196e4426241f720e21c6d1b891ed145de1e Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 4 Dec 2025 06:13:05 +0000 Subject: [PATCH] Optimize GoogleGmailDataSource.users_settings_get_language MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves an 80% runtime speedup (8.14ms → 4.51ms) through two key optimizations: **1. Method Chain Caching**: The most significant optimization caches the Gmail API method reference `self.client.users().settings().getLanguage` during initialization as `self._get_language`. The line profiler shows this eliminates the expensive method chain traversal that consumed 69.3% of execution time (21.8ms) in the original code, reducing it to just 47.5% (8.8ms) - a ~60% reduction in this critical path. **2. Conditional Dictionary Allocation**: Replaces the always-allocating `kwargs or {}` pattern with conditional logic that only creates new dictionaries when necessary. When `userId` is None, it reuses the original kwargs directly instead of creating a copy. The line profiler data clearly shows the optimization's impact: the method call line drops from 47,420ns per hit to 19,114ns per hit - a 60% reduction per operation. This optimization is particularly valuable for high-frequency Gmail API operations, as evidenced by the test cases showing concurrent calls and large-scale operations (100 calls). Note that while runtime improves significantly, throughput shows a slight decrease (21,714 → 20,790 ops/sec, -4.3%). This suggests the optimization reduces individual call latency but may introduce slight overhead in high-concurrency scenarios, possibly due to the cached reference or memory access patterns. However, the substantial runtime improvement makes this trade-off beneficial for most real-world usage patterns where individual call performance matters more than peak theoretical throughput. --- .../app/sources/external/google/gmail/gmail.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/python/app/sources/external/google/gmail/gmail.py b/backend/python/app/sources/external/google/gmail/gmail.py index 7d6993ee82..7ebc6b79b3 100644 --- a/backend/python/app/sources/external/google/gmail/gmail.py +++ b/backend/python/app/sources/external/google/gmail/gmail.py @@ -21,6 +21,9 @@ def __init__( """ self.client = client + # Cache references to the users().settings().getLanguage method for performance + self._get_language = self.client.users().settings().getLanguage + async def users_get_profile( self, userId: str, @@ -1217,11 +1220,19 @@ async def users_settings_get_language( Returns: Dict[str, Any]: API response """ - kwargs = kwargs or {} + # Avoid unnecessary dict allocation: only create a new dict if kwargs is None or not a dict + # Don't mutate input kwargs if it might be shared; instead, copy only if we insert. + if kwargs is None: + local_kwargs = {} + else: + # Only copy if we need to insert userId, else use as-is + local_kwargs = kwargs if userId is None else dict(kwargs) + if userId is not None: - kwargs['userId'] = userId + local_kwargs['userId'] = userId - request = self.client.users().settings().getLanguage(**kwargs) # type: ignore + # Use cached method reference for performance + request = self._get_language(**local_kwargs) # type: ignore return request.execute() async def users_settings_update_language(