⚡️ Speed up function zoom_image
by 56%
#442
Open
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.
📄 56% (0.56x) speedup for
zoom_image
inunstructured_inference/models/tables.py
⏱️ Runtime :
296 milliseconds
→190 milliseconds
(best of15
runs)📝 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_image
parameter 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.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:
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_image
To edit these changes
git checkout codeflash/optimize-zoom_image-metaix6e
and push.Note
Optimizes
zoom_image
inunstructured_inference/models/tables.py
usingnp.asarray
and in-place cv2 morphology, and bumps version to1.0.8-dev2
with changelog entry.zoom_image
inunstructured_inference/models/tables.py
:np.asarray
for image conversion.cv2.dilate
/cv2.erode
operate in-place viadst
.__version__
to1.0.8-dev2
inunstructured_inference/__version__.py
.1.0.8-dev2
entry notingzoom_image
optimization.Written by Cursor Bugbot for commit 1cfe7e7. This will update automatically on new commits. Configure here.