⚡️ Speed up function extract_query_params by 110%
#4
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.