⚡️ Speed up function pixel_perfect_resolution by 311%
#97
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.
📄 311% (3.11x) speedup for
pixel_perfect_resolutionininvokeai/app/util/controlnet_utils.py⏱️ Runtime :
846 microseconds→206 microseconds(best of159runs)📝 Explanation and details
The optimization achieves a 310% speedup by eliminating three major performance bottlenecks:
Key optimizations:
Eliminated unnecessary
float()conversions: Removedfloat(target_H),float(raw_H),float(target_W), andfloat(raw_W)calls. In Python 3, integer division already produces floats automatically, making these conversions redundant overhead.Replaced
min(raw_H, raw_W)with inline conditional: Changedfloat(min(raw_H, raw_W))tomHW = raw_H if raw_H < raw_W else raw_W. This avoids function call overhead and the additionalfloat()conversion.Replaced
np.round()with built-inround(): The most impactful change - swappedint(np.round(estimation))withint(round(estimation)). For scalar values, Python's built-inround()is dramatically faster than NumPy's vectorizednp.round().Performance impact by line:
np.round()call originally consumed 74.1% of total runtime (1.21ms out of 1.64ms)Test results show consistent 300-500% speedups across all scenarios - from small images (1x1) to large ones (1000x1000), and across different resize modes. The optimization is particularly effective for this function since it performs simple mathematical operations that don't benefit from NumPy's vectorization advantages, making the built-in Python functions more efficient.
The changes preserve exact mathematical behavior and all edge cases while dramatically reducing computational overhead through more appropriate function choices for scalar operations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pixel_perfect_resolution-mhn97ibland push.