-
Notifications
You must be signed in to change notification settings - Fork 72
⚡️ Speed up function zoom_image by 56%
#442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚡️ Speed up function zoom_image by 56%
#442
Conversation
The optimized code achieves a **55% speedup** through three key memory optimization techniques: **1. Reduced Memory Allocations** - Moved `kernel = np.ones((1, 1), np.uint8)` outside the resize operation to avoid unnecessary intermediate allocations - Used `np.asarray(image)` instead of `np.array(image)` to avoid copying when the PIL image is already a numpy-compatible array **2. In-Place Operations** - Added `dst=new_image` parameter to both `cv2.dilate()` and `cv2.erode()` operations, making them modify the existing array in-place rather than creating new copies - This eliminates two major memory allocations that were consuming 32% of the original runtime (16.7% + 15.8% from the profiler) **3. Memory Access Pattern Improvements** The profiler shows the most dramatic improvements in the morphological operations: - `cv2.dilate` time reduced from 54.8ms to 0.5ms (99% reduction) - `cv2.erode` time reduced from 52.1ms to 0.2ms (99.6% reduction) **Performance Characteristics** The optimization shows consistent improvements across all test cases, with particularly strong gains for: - Large images (15-30% speedup on 500x400+ images) - Extreme scaling operations (30% improvement on extreme downscaling) - Memory-intensive scenarios where avoiding copies provides the most benefit The core image processing logic remains identical - only memory management was optimized to eliminate unnecessary allocations and copies during the morphological operations.
|
@claude Have look and critique this. |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
The claude review error has been other encountered by other users and discussed here -> anthropics/claude-code-action#223 , hope it's resolved soon. |
|
Claude finished @qued's task —— View job Code Review AnalysisI'll review this optimization PR that claims a 56% speedup for the Todo List:
Code Review Findings✅ Positive Aspects
|
|
Closing in favor of the in-repo duplicate #445 |
📄 56% (0.56x) speedup for
zoom_imageinunstructured_inference/models/tables.py⏱️ Runtime :
296 milliseconds→190 milliseconds(best of15runs)📝 Explanation and details
The optimized code achieves a 55% speedup through three key memory optimization techniques:
1. Reduced Memory Allocations
kernel = np.ones((1, 1), np.uint8)outside the resize operation to avoid unnecessary intermediate allocationsnp.asarray(image)instead ofnp.array(image)to avoid copying when the PIL image is already a numpy-compatible array2. In-Place Operations
dst=new_imageparameter to bothcv2.dilate()andcv2.erode()operations, making them modify the existing array in-place rather than creating new copies3. Memory Access Pattern Improvements
The profiler shows the most dramatic improvements in the morphological operations:
cv2.dilatetime reduced from 54.8ms to 0.5ms (99% reduction)cv2.erodetime reduced from 52.1ms to 0.2ms (99.6% reduction)Performance Characteristics
The optimization shows consistent improvements across all test cases, with particularly strong gains for:
The core image processing logic remains identical - only memory management was optimized to eliminate unnecessary allocations and copies during the morphological operations.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
models/test_tables.py::test_zoom_image🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_test_unstructured_inference__replay_test_0.py::test_unstructured_inference_models_tables_zoom_imageTo edit these changes
git checkout codeflash/optimize-zoom_image-metaix6eand push.Note
Optimizes
zoom_imageinunstructured_inference/models/tables.pyusingnp.asarrayand in-place cv2 morphology, and bumps version to1.0.8-dev2with changelog entry.zoom_imageinunstructured_inference/models/tables.py:np.asarrayfor image conversion.cv2.dilate/cv2.erodeoperate in-place viadst.__version__to1.0.8-dev2inunstructured_inference/__version__.py.1.0.8-dev2entry notingzoom_imageoptimization.Written by Cursor Bugbot for commit 1cfe7e7. This will update automatically on new commits. Configure here.