⚡️ Speed up function get_dependency_query_params by 32% - #14
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up function get_dependency_query_params by 32%#14codeflash-ai[bot] wants to merge 1 commit into
get_dependency_query_params by 32%#14codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimization avoids unnecessary URL encoding and string parsing for the common case where dictionary parameters contain only scalar values (strings, numbers, booleans) rather than lists or tuples. **Key Changes:** - Added a check `any(isinstance(v, (list, tuple)) for v in params.values())` to detect if any parameter values are multi-valued - For scalar-only dictionaries, pass the dict directly to `QueryParams(params)` instead of going through `urlencode` - Only use the expensive `urlencode(params, doseq=True)` path when multi-valued parameters are detected **Why This is Faster:** The original code always called `urlencode(params, doseq=True)` which converts the dictionary to a URL-encoded string, then `QueryParams` parses that string back into key-value pairs. This double conversion (dict → string → parsed structure) is costly. The optimization bypasses this for the common case where parameters are simple scalars. **Performance Impact:** From the profiler results, the expensive `urlencode` line dropped from 53% of total time to just 8% when needed. The largest gains are seen in the "large scale" tests - up to 164% speedup for cases with many parameters, since each parameter avoids the encoding/parsing overhead. **Hot Path Benefits:** Based on the function references, `get_dependency_query_params` is called from `deserialize_query_params` and `extract_query_params`, which likely process user input parameters frequently. The optimization particularly benefits workloads with many simple parameters (common in web APIs), while preserving full compatibility for complex multi-valued parameters.
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.
📄 32% (0.32x) speedup for
get_dependency_query_paramsinsrc/titiler/core/titiler/core/utils.py⏱️ Runtime :
14.5 milliseconds→10.9 milliseconds(best of250runs)📝 Explanation and details
The optimization avoids unnecessary URL encoding and string parsing for the common case where dictionary parameters contain only scalar values (strings, numbers, booleans) rather than lists or tuples.
Key Changes:
any(isinstance(v, (list, tuple)) for v in params.values())to detect if any parameter values are multi-valuedQueryParams(params)instead of going throughurlencodeurlencode(params, doseq=True)path when multi-valued parameters are detectedWhy This is Faster:
The original code always called
urlencode(params, doseq=True)which converts the dictionary to a URL-encoded string, thenQueryParamsparses that string back into key-value pairs. This double conversion (dict → string → parsed structure) is costly. The optimization bypasses this for the common case where parameters are simple scalars.Performance Impact:
From the profiler results, the expensive
urlencodeline dropped from 53% of total time to just 8% when needed. The largest gains are seen in the "large scale" tests - up to 164% speedup for cases with many parameters, since each parameter avoids the encoding/parsing overhead.Hot Path Benefits:
Based on the function references,
get_dependency_query_paramsis called fromdeserialize_query_paramsandextract_query_params, which likely process user input parameters frequently. The optimization particularly benefits workloads with many simple parameters (common in web APIs), while preserving full compatibility for complex multi-valued parameters.✅ 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-mifo2ot6and push.