⚡️ Speed up method Algorithms.list by 15%
#6
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.
📄 15% (0.15x) speedup for
Algorithms.listinsrc/titiler/core/titiler/core/algorithm/__init__.py⏱️ Runtime :
46.6 microseconds→40.6 microseconds(best of250runs)📝 Explanation and details
The optimization replaces
list(self.data.keys())with[*self.data](unpacking operator), achieving a 14% speedup by eliminating the intermediate.keys()view object creation.Key Changes:
[*self.data]directly unpacks dictionary keys into a list.keys()method call andlist()constructor overheaddata = attr.ib()for proper attrs compatibilityWhy It's Faster:
In Python,
dict.keys()creates a dictionary view object that must then be converted to a list. The unpacking operator[*self.data]bypasses this intermediate step by directly iterating over the dictionary keys during list construction, reducing function call overhead and object creation.Performance Context:
Based on function_references, this method is called in hot paths within titiler's factory routing system, particularly when:
self.supported_tms.list())Literal[tuple(self.supported_tms.list())]Test Results Analysis:
The optimization shows consistent improvements across all test cases:
Since this method appears to be called frequently in web request processing pipelines, even small per-call improvements compound into significant overall performance gains.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Algorithms.list-mi8djae6and push.