⚡️ Speed up function _establish_grid_size by 75%#787
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization achieves a **75% speedup** through two key changes that eliminate redundant function calls: **What was optimized:** 1. **Replaced `np.sqrt()` with `math.sqrt()`** in `_negotiate_grid_size()` - NumPy's sqrt is designed for arrays and has overhead when used on scalars 2. **Cached `len(images)` as `num_images`** to avoid repeated length calculations within the same function **Why this leads to speedup:** - `math.sqrt()` is significantly faster than `np.sqrt()` for scalar values (avoiding NumPy's array handling overhead) - Caching `len(images)` eliminates redundant list traversals, especially impactful for larger image lists **Performance impact by test case:** - **Largest gains** (300-480% faster) occur when `_negotiate_grid_size()` is called, particularly with 4+ images that require square grid calculation - **Minimal impact** (0-6% variation) for cases that bypass `_negotiate_grid_size()` (explicit grid dimensions) - **Best performance** with large image counts (1000 images: 373% faster) where the sqrt calculation dominates **How this impacts existing workloads:** Based on the function reference, `_establish_grid_size()` is called from `create_tiles()` - a utility for arranging multiple images into a grid layout. This optimization will significantly improve performance when: - Processing batches of images for visualization dashboards - Generating image grids for inference results display - Creating tiled layouts where grid dimensions need to be calculated automatically The optimization maintains identical behavior while delivering substantial performance gains for the common case of automatic grid size negotiation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 75% (0.75x) speedup for
_establish_grid_sizeininference/core/utils/drawing.py⏱️ Runtime :
162 microseconds→92.2 microseconds(best of42runs)📝 Explanation and details
The optimization achieves a 75% speedup through two key changes that eliminate redundant function calls:
What was optimized:
np.sqrt()withmath.sqrt()in_negotiate_grid_size()- NumPy's sqrt is designed for arrays and has overhead when used on scalarslen(images)asnum_imagesto avoid repeated length calculations within the same functionWhy this leads to speedup:
math.sqrt()is significantly faster thannp.sqrt()for scalar values (avoiding NumPy's array handling overhead)len(images)eliminates redundant list traversals, especially impactful for larger image listsPerformance impact by test case:
_negotiate_grid_size()is called, particularly with 4+ images that require square grid calculation_negotiate_grid_size()(explicit grid dimensions)How this impacts existing workloads:
Based on the function reference,
_establish_grid_size()is called fromcreate_tiles()- a utility for arranging multiple images into a grid layout. This optimization will significantly improve performance when:The optimization maintains identical behavior while delivering substantial performance gains for the common case of automatic grid size negotiation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_establish_grid_size-miqm4k49and push.