From 03e3f5e404cc23e5b2f505d8ab29835ab34802c4 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sun, 21 Dec 2025 07:31:01 +0000 Subject: [PATCH] Optimize OneNoteDataSource.groups_onenote_section_groups_get_section_groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **20% runtime improvement** by implementing **lazy object instantiation** - only creating expensive Microsoft Graph SDK objects when actually needed. **Key optimization:** - **Conditional object creation**: Added `any_query_params` check to determine if query parameters exist before instantiating `OnenoteRequestBuilderGetQueryParameters()` (10.2% → 0.4% of total time) - **Deferred config instantiation**: Only creates `OnenoteRequestBuilderGetRequestConfiguration()` when query params, headers, or search are present (8.8% → 0.3% of total time) - **Early returns for common cases**: When no parameters are provided (233 out of 235 test calls), both objects are set to `None`, avoiding unnecessary allocations **Why this works:** In Python, object instantiation has overhead, especially for complex SDK objects. The original code always created these objects regardless of whether they were needed. The optimization recognizes that many OneNote API calls use default parameters and skips object creation entirely for those cases. **Performance impact:** - **Runtime**: 762μs → 634μs (20% faster) - **Best case scenarios**: API calls with no query parameters, headers, or search (the most common usage pattern based on test data) - **No throughput change**: The optimization affects per-call overhead rather than concurrent processing capacity The line profiler shows the Microsoft Graph API call itself remains the bottleneck (41.6% of time), but reducing setup overhead provides meaningful gains for high-frequency OneNote operations. --- .../external/microsoft/one_note/one_note.py | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/backend/python/app/sources/external/microsoft/one_note/one_note.py b/backend/python/app/sources/external/microsoft/one_note/one_note.py index 76dbc9ad19..38307907c3 100644 --- a/backend/python/app/sources/external/microsoft/one_note/one_note.py +++ b/backend/python/app/sources/external/microsoft/one_note/one_note.py @@ -1,5 +1,3 @@ - - import json import logging from dataclasses import asdict @@ -19121,36 +19119,48 @@ async def groups_onenote_section_groups_get_section_groups( """ # Build query parameters including OData for OneNote try: - # Use typed query parameters - query_params = OnenoteRequestBuilder.OnenoteRequestBuilderGetQueryParameters() - # Set query parameters using typed object properties - if select: - query_params.select = select if isinstance(select, list) else [select] - if expand: - query_params.expand = expand if isinstance(expand, list) else [expand] - if filter: - query_params.filter = filter - if orderby: - query_params.orderby = orderby - if search: - query_params.search = search - if top is not None: - query_params.top = top - if skip is not None: - query_params.skip = skip + # Only instantiate query parameter object if necessary + any_query_params = ( + select is not None or expand is not None or filter is not None or orderby is not None + or search is not None or top is not None or skip is not None + ) + if any_query_params: + query_params = OnenoteRequestBuilder.OnenoteRequestBuilderGetQueryParameters() + # Set query parameters using typed object properties + if select: + query_params.select = select if isinstance(select, list) else [select] + if expand: + query_params.expand = expand if isinstance(expand, list) else [expand] + if filter: + query_params.filter = filter + if orderby: + query_params.orderby = orderby + if search: + query_params.search = search + if top is not None: + query_params.top = top + if skip is not None: + query_params.skip = skip + else: + query_params = None - # Create proper typed request configuration - config = OnenoteRequestBuilder.OnenoteRequestBuilderGetRequestConfiguration() - config.query_parameters = query_params + # Only instantiate config if needed + if query_params is not None or headers is not None or search: + # Create proper typed request configuration + config = OnenoteRequestBuilder.OnenoteRequestBuilderGetRequestConfiguration() + if query_params is not None: + config.query_parameters = query_params - if headers: - config.headers = headers + if headers: + config.headers = headers - # Add consistency level for search operations in OneNote - if search: - if not config.headers: - config.headers = {} - config.headers['ConsistencyLevel'] = 'eventual' + # Add consistency level for search operations in OneNote + if search: + if not config.headers: + config.headers = {} + config.headers['ConsistencyLevel'] = 'eventual' + else: + config = None response = await self.client.groups.by_group_id(group_id).onenote.section_groups.by_section_group_id(sectionGroup_id).section_groups.by_section_group_id(sectionGroup_id1).get(request_configuration=config) return self._handle_onenote_response(response)