From 3adc61fe89c4ad56734186f9f8fb3b8695cc479d Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:28:24 +0000 Subject: [PATCH] Optimize OneNoteDataSource.me_onenote_delete_notebooks The optimization achieves a **12% runtime improvement** by reducing unnecessary object creation and streamlining control flow in the OneNote deletion method. **Key optimizations applied:** 1. **Conditional object creation**: The optimized code only creates `RequestConfiguration` objects when actually needed, using `has_params` and `has_custom_headers` flags to determine necessity. This eliminates wasteful object instantiation in the common case where no parameters are provided. 2. **Reordered error checking**: In `_handle_onenote_response`, the dict error check (`isinstance(response, dict) and 'error' in response`) is moved before the `hasattr(response, 'error')` check. This provides a faster path for dict responses, which are more common in API responses. **Why this improves performance:** - **Reduced allocations**: Line profiler shows the original code always creates `RequestConfiguration` objects (line 87: 335,484 ns total), while the optimized version only creates them when needed (line 11: 15,976 ns total for 5 hits vs 345 hits). - **Faster error path**: The reordered checks in error handling reduce the time spent on `hasattr` operations for common dict responses. - **Preserved functionality**: All error handling, parameter validation, and API behavior remains identical. **Impact on workloads:** The optimization particularly benefits scenarios with: - **High-frequency deletion calls** with minimal parameters (most common case) - **Batch operations** where many notebooks are deleted with default settings - **Throughput-sensitive applications** processing many OneNote operations The 12% runtime improvement translates directly to faster response times for OneNote deletion operations, especially valuable in enterprise scenarios where OneNote operations may be called frequently for content management or cleanup tasks. --- .../external/microsoft/one_note/one_note.py | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 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..527ccd339b 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 @@ -115,16 +113,16 @@ def _handle_onenote_response(self, response: object) -> OneNoteResponse: error_msg = None # Enhanced error response handling for OneNote - if hasattr(response, 'error'): - success = False - error_msg = str(response.error) - elif isinstance(response, dict) and 'error' in response: + if isinstance(response, dict) and 'error' in response: success = False error_info = response['error'] if isinstance(error_info, dict): error_msg = f"{error_info.get('code', 'Unknown')}: {error_info.get('message', 'No message')}" else: error_msg = str(error_info) + elif hasattr(response, 'error'): + success = False + error_msg = str(response.error) elif hasattr(response, 'code') and hasattr(response, 'message'): success = False error_msg = f"{response.code}: {response.message}" @@ -5628,36 +5626,44 @@ async def me_onenote_delete_notebooks( """ # Build query parameters including OData for OneNote try: - # Use typed query parameters - query_params = RequestConfiguration() - # 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 create configuration objects if needed + has_params = any([select, expand, filter, orderby, search, top is not None, skip is not None]) + has_custom_headers = headers is not None + + if not has_params and not has_custom_headers and not search: + config = None + else: + config = RequestConfiguration() + + if has_params: + # Use typed query parameters + query_params = RequestConfiguration() + # 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 + + config.query_parameters = query_params - # Create proper typed request configuration - config = RequestConfiguration() - 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' response = await self.client.me.onenote.notebooks.by_notebook_id(notebook_id).delete(request_configuration=config) return self._handle_onenote_response(response)