⚡️ Speed up function extract_query_params by 110% - #4
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimized code achieves a **109% speedup** through two key optimizations that target the most expensive operations: **1. Caching `get_dependant` Results (78% of time savings)** The original code calls `get_dependant(path="", call=dependency)` on every invocation, which the profiler shows takes 68.8% of execution time (157ms out of 228ms). The optimization adds function-level caching using a `_titiler_gdp_cached_dep` attribute on each dependency callable. When the same dependency is processed multiple times, subsequent calls retrieve the cached result instead of re-analyzing the dependency structure. **2. Hoisting `QueryParams` Creation (Additional efficiency)** In `extract_query_params`, the original code performed `isinstance(params, Dict)` and `urlencode()` inside `get_dependency_query_params` for every dependency. The optimization moves this check outside the loop, converting `Dict` to `QueryParams` once and reusing it across all dependencies. **Performance Impact by Test Case:** - **Repeated dependencies** see dramatic gains (995% faster for 100 identical dependencies) - **Single dependency calls** show 6-8x improvements due to reduced `get_dependant` overhead - **Large-scale scenarios** benefit most, with 50+ dependencies seeing 55-99% speedups - **Simple cases** still improve significantly (261-795% faster) The caching is safe because `get_dependant` results are deterministic per callable and don't change during runtime. These optimizations are particularly valuable in web frameworks where the same dependencies are processed repeatedly 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.
📄 110% (1.10x) speedup for
extract_query_paramsinsrc/titiler/core/titiler/core/utils.py⏱️ Runtime :
57.2 milliseconds→27.2 milliseconds(best of214runs)📝 Explanation and details
The optimized code achieves a 109% speedup through two key optimizations that target the most expensive operations:
1. Caching
get_dependantResults (78% of time savings)The original code calls
get_dependant(path="", call=dependency)on every invocation, which the profiler shows takes 68.8% of execution time (157ms out of 228ms). The optimization adds function-level caching using a_titiler_gdp_cached_depattribute on each dependency callable. When the same dependency is processed multiple times, subsequent calls retrieve the cached result instead of re-analyzing the dependency structure.2. Hoisting
QueryParamsCreation (Additional efficiency)In
extract_query_params, the original code performedisinstance(params, Dict)andurlencode()insideget_dependency_query_paramsfor every dependency. The optimization moves this check outside the loop, convertingDicttoQueryParamsonce and reusing it across all dependencies.Performance Impact by Test Case:
get_dependantoverheadThe caching is safe because
get_dependantresults are deterministic per callable and don't change during runtime. These optimizations are particularly valuable in web frameworks where the same dependencies are processed repeatedly across requests.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-extract_query_params-mi8cxfojand push.