⚡️ Speed up function _establish_grid_size by 75%
#787
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.