From db6a7aebe26a7ccd8bd41edbfd0854d677e0c43e Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 19 Dec 2025 05:28:39 +0000 Subject: [PATCH] Optimize OneNoteDataSource.me_onenote_notebooks_notebook_sections_onenote_section_copy_to_section_group The optimization achieves a **9% runtime speedup** by eliminating unnecessary object creation and attribute assignments when no query parameters are provided. **Key optimization**: The original code always created a `RequestConfiguration()` object for `query_params` and assigned it to `config.query_parameters`, even when no query parameters were specified. The optimized version uses **conditional lazy initialization** - it only creates the `query_params` object when at least one parameter (`select`, `expand`, `filter`, etc.) is actually provided. **Why this works**: From the line profiler data, the original code spent significant time on: - `query_params = RequestConfiguration()` (463,863 ns, 10.7% of total time) - `config.query_parameters = query_params` (120,416 ns, 2.8% of total time) These operations occurred on every call (417 hits), but were often unnecessary since most calls don't provide query parameters. The optimization reduces this overhead by checking `if select or expand or filter or orderby or search or top is not None or skip is not None:` before creating the query parameters object. **Performance impact**: The test results show this optimization is particularly effective for basic API calls without query parameters (most common use case), while maintaining identical functionality. The 9% speedup comes from avoiding approximately 13.5% of the original function's object allocation overhead when no parameters are needed. **Workload benefits**: This optimization benefits high-frequency OneNote API operations where most calls use only the required `notebook_id` and `onenoteSection_id` parameters without additional query options - a common pattern in bulk operations or simple data retrieval scenarios. --- .../external/microsoft/one_note/one_note.py | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 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..1ae3cd4f0c 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 @@ -8400,27 +8398,28 @@ async def me_onenote_notebooks_notebook_sections_onenote_section_copy_to_section """ # 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 - # Create proper typed request configuration config = RequestConfiguration() - config.query_parameters = query_params + + # Only create query_params object if parameters are provided + if select or expand or filter or orderby or search or top is not None or skip is not None: + 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 if headers: config.headers = headers