⚡️ Speed up method MetadataToBoolInvocation.invoke by 15% #27
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
MetadataToBoolInvocation.invokeininvokeai/app/invocations/metadata_linked.py⏱️ Runtime :
11.3 microseconds→9.77 microseconds(best of36runs)📝 Explanation and details
The optimization achieves a 15% speedup by eliminating redundant attribute lookups and consolidating conditional logic into cleaner local variable assignments.
Key optimizations applied:
Reduced attribute lookups: The original code accessed
self.metadata,self.label, andself.default_valuemultiple times. The optimized version stores these in local variables (metadata,label,default_value) to avoid repeated attribute access overhead.Eliminated complex ternary expression: The original single-line ternary
str(self.custom_label if self.label == CUSTOM_LABEL else self.label)was replaced with explicit if/else logic that pre-computes thelookup_key, reducing string conversion calls and making the logic more efficient.Moved constant definition:
CUSTOM_LABELis now defined locally in the module rather than requiring a global lookup, making the comparisonlabel == CUSTOM_LABELfaster.Cleaner data dictionary creation: The metadata handling is now explicit with separate if/else branches rather than a ternary expression, which is more readable and slightly faster.
Why this leads to speedup: In Python, attribute lookups (
self.attribute) are more expensive than local variable access. The line profiler shows the original complex expression took 17.1% of total time (3738ns), while the optimized version splits this into multiple cheaper operations. Local variables are stored in a fast array and accessed by index, while attributes require dictionary lookups.Test case performance: The annotated tests show consistent 14-16% improvements across edge cases, particularly benefiting scenarios where
metadatais None (a common case that now has a cleaner fast-path).This optimization is especially valuable for metadata processing workflows where this function may be called repeatedly, as the per-invocation savings compound over many calls.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-MetadataToBoolInvocation.invoke-mhlkij2tand push.