⚡️ Speed up function remove_image_padding by 7%#10
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization achieves a 7% speedup through three key improvements: **1. Optimized Channel Handling for RGB Images** The original code applies `np.any()` directly to multi-dimensional arrays, which processes all dimensions simultaneously. The optimized version first collapses the color channel (axis=2) for 3D images before computing row/column projections. This reduces the computational load in subsequent operations and improves cache locality. **2. More Efficient Index Finding** Replaced `np.where(rows)[0][[0, -1]]` with `np.flatnonzero(rows_any)` followed by direct indexing. `np.flatnonzero()` is specifically optimized for finding non-zero indices and avoids the overhead of the more general `np.where()` function plus additional array indexing operations. **3. Better Memory Access Patterns** The two-step approach (projection → row/col analysis) creates better data locality. For RGB images, processing the channel dimension first creates a smaller intermediate array that fits better in CPU cache during subsequent row/column operations. **Performance Characteristics by Test Case:** - **RGB/multichannel images** see the largest gains (8-11% faster) due to optimized channel handling - **Large images with padding** benefit significantly (9% faster) from improved cache usage - **Simple grayscale cases** show modest improvements (3-7% faster) - **Edge cases with sparse non-zero pixels** may be slightly slower due to additional branching overhead, but the overall workload benefits from the optimizations The optimization is particularly effective for typical document processing scenarios involving larger RGB images with padding.
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.
📄 7% (0.07x) speedup for
remove_image_paddingindoctr/utils/geometry.py⏱️ Runtime :
7.55 milliseconds→7.03 milliseconds(best of93runs)📝 Explanation and details
The optimization achieves a 7% speedup through three key improvements:
1. Optimized Channel Handling for RGB Images
The original code applies
np.any()directly to multi-dimensional arrays, which processes all dimensions simultaneously. The optimized version first collapses the color channel (axis=2) for 3D images before computing row/column projections. This reduces the computational load in subsequent operations and improves cache locality.2. More Efficient Index Finding
Replaced
np.where(rows)[0][[0, -1]]withnp.flatnonzero(rows_any)followed by direct indexing.np.flatnonzero()is specifically optimized for finding non-zero indices and avoids the overhead of the more generalnp.where()function plus additional array indexing operations.3. Better Memory Access Patterns
The two-step approach (projection → row/col analysis) creates better data locality. For RGB images, processing the channel dimension first creates a smaller intermediate array that fits better in CPU cache during subsequent row/column operations.
Performance Characteristics by Test Case:
The optimization is particularly effective for typical document processing scenarios involving larger RGB images with padding.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
common/test_utils_geometry.py::test_remove_image_padding🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-remove_image_padding-mg7st23pand push.