⚡️ Speed up function get_dependency_query_params by 14% - #20
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up function get_dependency_query_params by 14%#20codeflash-ai[bot] wants to merge 1 commit into
get_dependency_query_params by 14%#20codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimization implements **function-level caching for the expensive `get_dependant()` call**. The key insight is that `get_dependant(path="", call=dependency)` performs complex introspection on the dependency callable, but this result is deterministic and can be cached. **Key changes:** - Added a function attribute `_dependant_cache` dictionary to cache `get_dependant()` results keyed by the dependency callable - Cache check before calling `get_dependant()`, only computing when not cached - Cache storage after computation for future calls **Why this speeds up the code:** The line profiler shows `get_dependant()` consumes 52.1% of execution time in the original code (24.9ms out of 47.8ms total). In the optimized version, this drops to 49.6% but with fewer actual calls (26 vs 29 hits), indicating cache hits are avoiding expensive computations. The per-hit cost remains similar (~860μs), confirming the optimization works by reducing call frequency rather than making individual calls faster. **Performance impact based on usage patterns:** From the function references, this function is called in two key scenarios: 1. `deserialize_query_params()` - single dependency processing 2. `extract_query_params()` - **iterating over multiple dependencies in a loop** The loop usage in `extract_query_params()` makes this optimization particularly valuable, as the same dependencies are likely processed repeatedly across requests. The test results show consistent 1-5% improvements across various parameter scenarios, with larger gains for complex dependencies. **Best performance gains occur when:** - The same dependency callable is processed multiple times (common in web request handling) - Dependencies have complex signatures requiring expensive introspection - Applications process many similar requests with the same endpoint dependencies
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.
📄 14% (0.14x) speedup for
get_dependency_query_paramsinsrc/titiler/core/titiler/core/utils.py⏱️ Runtime :
10.5 milliseconds→9.20 milliseconds(best of250runs)📝 Explanation and details
The optimization implements function-level caching for the expensive
get_dependant()call. The key insight is thatget_dependant(path="", call=dependency)performs complex introspection on the dependency callable, but this result is deterministic and can be cached.Key changes:
_dependant_cachedictionary to cacheget_dependant()results keyed by the dependency callableget_dependant(), only computing when not cachedWhy this speeds up the code:
The line profiler shows
get_dependant()consumes 52.1% of execution time in the original code (24.9ms out of 47.8ms total). In the optimized version, this drops to 49.6% but with fewer actual calls (26 vs 29 hits), indicating cache hits are avoiding expensive computations. The per-hit cost remains similar (~860μs), confirming the optimization works by reducing call frequency rather than making individual calls faster.Performance impact based on usage patterns:
From the function references, this function is called in two key scenarios:
deserialize_query_params()- single dependency processingextract_query_params()- iterating over multiple dependencies in a loopThe loop usage in
extract_query_params()makes this optimization particularly valuable, as the same dependencies are likely processed repeatedly across requests. The test results show consistent 1-5% improvements across various parameter scenarios, with larger gains for complex dependencies.Best performance gains occur when:
✅ 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-mih9uo97and push.