⚡️ Speed up function zoom_image by 12%
#3
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.
📄 12% (0.12x) speedup for
zoom_imageinunstructured/partition/utils/ocr_models/tesseract_ocr.py⏱️ Runtime :
18.1 milliseconds→16.1 milliseconds(best of12runs)📝 Explanation and details
The optimization removes unnecessary morphological operations (dilation followed by erosion) that were being performed with a 1x1 kernel. Since a 1x1 kernel has no effect on the image during dilation and erosion operations, these steps were pure computational overhead.
Key changes:
np.ones((1, 1), np.uint8))cv2.dilate()andcv2.erode()calls that used this ineffective kernelWhy this leads to speedup:
The line profiler shows that the morphological operations consumed 27.7% of the total runtime (18.5% for dilation + 9.2% for erosion). A 1x1 kernel performs no actual morphological transformation - it's equivalent to applying the identity operation. Removing these no-op calls eliminates unnecessary OpenCV function overhead and memory operations.
Performance impact based on function references:
The
zoom_imagefunction is called within Tesseract OCR processing, specifically inget_layout_from_image()when text height falls outside optimal ranges. This optimization will improve OCR preprocessing performance, especially beneficial since OCR is typically a computationally intensive operation that may be called repeatedly on document processing pipelines.Test case analysis:
The optimization shows consistent 7-35% speedups across various test cases, with particularly strong gains for:
The optimization maintains identical visual output since the removed operations were mathematically no-ops, ensuring OCR accuracy is preserved while reducing processing time.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
partition/pdf_image/test_ocr.py::test_zoom_image🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-zoom_image-mjcb2smband push.