⚡️ Speed up function get_dependency_query_params by 18% - #3
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up function get_dependency_query_params by 18%#3codeflash-ai[bot] wants to merge 1 commit into
get_dependency_query_params by 18%#3codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimized code achieves an 18% speedup through two key optimizations that address the most expensive operations identified in the profiler: **1. Dependant Caching (Major Impact)** The original code calls `get_dependant()` on every invocation, which consumed 53.7% of runtime. The optimization introduces a global cache using the dependency function's `id()` as key, stored in `get_dependant.__globals__`. This eliminates redundant parsing of the same dependency functions - a common scenario given the function references show it's called from `extract_query_params()` which iterates over dependency lists, and `deserialize_query_params()` which likely processes the same dependencies repeatedly. **2. QueryParams Construction Optimization (Secondary Impact)** The original always converts Dict params through `urlencode()`, even for simple cases. The optimization adds fast paths: - Direct use when params is already `QueryParams` - Empty string for empty dicts - Direct string joining for simple dicts without list/tuple values (avoiding urlencode overhead) - Falls back to `urlencode()` only for complex cases with sequences **Performance Impact by Test Case:** - Small/simple dependencies see 6-12x speedups (most common case based on test results) - Large-scale tests with 100+ parameters show modest 1-5% gains, as `request_params_to_args()` dominates runtime - List parameters maintain performance, with the optimization gracefully handling complex cases The caching is particularly valuable given the function references show this is called in loops (`extract_query_params`) and likely in request processing pipelines where the same dependency functions are reused across requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 18% (0.18x) speedup for
get_dependency_query_paramsinsrc/titiler/core/titiler/core/utils.py⏱️ Runtime :
78.6 milliseconds→66.5 milliseconds(best of98runs)📝 Explanation and details
The optimized code achieves an 18% speedup through two key optimizations that address the most expensive operations identified in the profiler:
1. Dependant Caching (Major Impact)
The original code calls
get_dependant()on every invocation, which consumed 53.7% of runtime. The optimization introduces a global cache using the dependency function'sid()as key, stored inget_dependant.__globals__. This eliminates redundant parsing of the same dependency functions - a common scenario given the function references show it's called fromextract_query_params()which iterates over dependency lists, anddeserialize_query_params()which likely processes the same dependencies repeatedly.2. QueryParams Construction Optimization (Secondary Impact)
The original always converts Dict params through
urlencode(), even for simple cases. The optimization adds fast paths:QueryParamsurlencode()only for complex cases with sequencesPerformance Impact by Test Case:
request_params_to_args()dominates runtimeThe caching is particularly valuable given the function references show this is called in loops (
extract_query_params) and likely in request processing pipelines where the same dependency functions are reused across requests.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_dependency_query_params-mi8cmxz2and push.