Use row copies for contiguous image crops#32
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request improves the performance of crop_image_array by adding a fast path for C-contiguous ndarray inputs, performing row-wise bulk copies into the output allocation while preserving a correct fallback for non-contiguous views. It also consolidates batch center-cropping to reuse the same optimized implementation and adds overflow-safe bounds/size validation.
Changes:
- Add a contiguous-array fast path in
crop_image_arraythat usescrop_raw_bufferto copy rows directly into a preallocated output buffer. - Preserve behavior for non-contiguous/strided inputs by keeping the existing
slice(...).to_owned()fallback. - Route
batch_center_crop_arraysthroughcrop_image_arrayand add a regression test covering the non-contiguous fallback.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/cropping.rs | Adds contiguous fast path with overflow-safe bounds checks; reuses the same implementation for batch center crops. |
| src/tests.rs | Adds coverage for cropping from a non-contiguous (strided) ndarray view to ensure the fallback path remains correct. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Root cause
The default crop APIs materialized an ndarray slice with
to_owned(). For RGB crops this performed generic element traversal instead of bulk row copies, even though consumer inputs are normally C-contiguous NumPy arrays.Performance
Median local latency on Apple M3 Max for a 2048x1536 crop from a 4032x3024 RGB image:
.copy(): 0.179 msValidation
cargo test --features opencv,simd(61 passed)