Skip to content

Use row copies for contiguous image crops#32

Merged
bghira merged 2 commits into
mainfrom
agent/fast-contiguous-crops
Jul 17, 2026
Merged

Use row copies for contiguous image crops#32
bghira merged 2 commits into
mainfrom
agent/fast-contiguous-crops

Conversation

@bghira

@bghira bghira commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • add a contiguous-array fast path that copies each crop row directly into the returned ndarray allocation
  • preserve the generic ndarray path for strided and otherwise non-contiguous inputs
  • route native batch center crops through the same optimized implementation
  • reject overflowing crop coordinates and output sizes

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:

  • default API before: 1.380 ms
  • default API after: 0.154 ms
  • specialized TSR zero-copy API: 0.221 ms
  • NumPy slice plus .copy(): 0.179 ms

Validation

  • added a regression test for the non-contiguous fallback
  • cargo test --features opencv,simd (61 passed)
  • Python test suite (68 passed)
  • repository pre-commit hooks, including competitive performance benchmarks

@bghira
bghira marked this pull request as ready for review July 17, 2026 15:30
@bghira
bghira requested a review from Copilot July 17, 2026 15:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_array that uses crop_raw_buffer to 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_arrays through crop_image_array and 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.

@bghira
bghira merged commit e8a2156 into main Jul 17, 2026
10 checks passed
@bghira
bghira deleted the agent/fast-contiguous-crops branch July 17, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants