⚡️ Speed up function thin_one_time by 557%
#96
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.
📄 557% (5.57x) speedup for
thin_one_timeininvokeai/app/util/controlnet_utils.py⏱️ Runtime :
40.7 milliseconds→6.20 milliseconds(best of152runs)📝 Explanation and details
The optimized code achieves a 556% speedup by eliminating the expensive
np.where()operation and replacing it with more efficient NumPy operations.Key optimizations:
Replaced
np.where()with boolean masking: The original code usednp.where(objects > 127)which returns tuple of indices and required 66.9% of total execution time. The optimized version converts the morphology result directly to a boolean mask usingobjects.astype(bool), which is much faster since OpenCV'sMORPH_HITMISSoutputs binary values (0 or 255).Direct boolean indexing: Instead of using the tuple of indices from
np.where()for assignment, the optimized code uses direct boolean mask indexing (x[mask] = 0), which is significantly more efficient in NumPy.Efficient existence check: Replaced
objects[0].shape[0] > 0withnp.any(mask)to check if any updates are needed, avoiding tuple unpacking and shape operations.Performance impact by test case type:
np.where()callThe optimization is particularly effective for morphological operations on large images with many pattern matches, which is typical in computer vision workflows where ControlNet utilities are commonly used.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-thin_one_time-mhn8qh8wand push.