⚡️ Speed up method MetadataToFloatCollectionInvocation.invoke by 16% #29
+17 −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.
📄 16% (0.16x) speedup for
MetadataToFloatCollectionInvocation.invokeininvokeai/app/invocations/metadata_linked.py⏱️ Runtime :
22.1 microseconds→19.1 microseconds(best of64runs)📝 Explanation and details
The optimized code achieves a 15% speedup by eliminating unnecessary object creation and string conversions in the metadata extraction path. Here are the key optimizations:
What was optimized:
Eliminated intermediate dictionary allocation: The original code always created a
datadictionary (either empty{}orself.metadata.root), which required memory allocation and assignment. The optimized version uses direct conditional logic to avoid this.Avoided redundant string conversion: The original code unconditionally called
str()on the label, even when it was already a string. The optimized version includes a type check to only convert non-string keys, with proper exception handling as a fallback.Reduced temporary variable assignments: The optimized code eliminates the intermediate
outputvariable and directly passes the result toFloatCollectionOutput().Why this leads to speedup:
isinstance(key, str)is faster than unconditionalstr()conversion when the input is already a stringPerformance benefits based on test cases:
test_large_scale_metadata_none_large_defaultcase (8% improvement) demonstrates the optimization works well even with large default valuesThe optimization is especially valuable since this appears to be a metadata extraction utility that could be called frequently in AI inference pipelines, where even small per-call improvements compound significantly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-MetadataToFloatCollectionInvocation.invoke-mhllpo9dand push.