⚡️ Speed up function create_filled_image by 12%
#107
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.
📄 12% (0.12x) speedup for
create_filled_imageininvokeai/backend/image_util/infill_methods/tile.py⏱️ Runtime :
3.31 milliseconds→2.95 milliseconds(best of191runs)📝 Explanation and details
The optimized code achieves a 12% speedup by eliminating expensive random number generation from the inner loop. The key optimization is batch generation of all random tile indices in a single vectorized call (
rng.integers(len(tile_pool), size=(num_tiles_y, num_tiles_x))), replacing hundreds of individual RNG calls that were consuming 26.9% of the original execution time.Key changes:
y_coords,x_coords) to avoid repeated range operationsspace_y,space_x) outside the inner loop where possible to minimize redundant computationsPerformance impact:
The optimization is most effective for large-scale test cases where many tiles are placed. Test results show 32-69% speedups for large images (100x100+ pixels), while smaller images see 15-27% slowdowns due to setup overhead. This suggests the function is likely called on substantial images where the vectorized RNG approach pays off significantly.
The batch RNG generation transforms O(tiles) random calls into O(1), making the optimization particularly valuable when filling large images with many tile positions, which appears to be the primary use case based on the test performance patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_filled_image-mhoaffdxand push.