Skip to content

Avoid full-frame copies in default OpenCV resize#31

Merged
bghira merged 2 commits into
mainfrom
agent/zero-copy-default-resize
Jul 17, 2026
Merged

Avoid full-frame copies in default OpenCV resize#31
bghira merged 2 commits into
mainfrom
agent/zero-copy-default-resize

Conversation

@bghira

@bghira bghira commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • create OpenCV Mat headers over the existing contiguous ndarray input
  • write resize output directly into the returned ndarray
  • retain RGB, contiguity, and interpolation behavior while checking OpenCV dimension conversions

Root cause

The default resize API copied every input into an owned OpenCV Mat and copied every resized Mat back into a second ndarray allocation. Those memory passes dominated otherwise-fast OpenCV resize calls, especially for large images and batches.

Performance

Median local latency on Apple M3 Max:

  • 1024x768 -> 512x384: 0.176 ms -> 0.033 ms
  • 2048x1536 -> 1024x768: 0.567 ms -> 0.226 ms
  • 4032x3024 -> 1024x768: 1.379 ms -> 0.435 ms
  • batch of 16 at 1024x768 -> 512x384: 3.871 ms -> 1.046 ms

The default path is now within a few percent of batch_resize_images_zero_copy.

Validation

  • cargo test --features opencv,simd (60 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 PR optimizes the default OpenCV-backed resize path by avoiding full-frame memory copies, creating lightweight cv::Mat headers over existing ndarray storage and resizing directly into the returned ndarray buffer.

Changes:

  • Build zero-copy OpenCV Mat headers over contiguous ArrayView3<u8> input buffers.
  • Preallocate the output Array3<u8> and resize directly into its backing memory.
  • Remove the now-unneeded Mat→ndarray bulk-copy helper from this implementation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/opencv_ops.rs Outdated
@bghira
bghira merged commit 76e64f9 into main Jul 17, 2026
10 checks passed
@bghira
bghira deleted the agent/zero-copy-default-resize branch July 17, 2026 16:08
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