Skip to content

Transfer resize iterator buffers without cloning#34

Merged
bghira merged 2 commits into
mainfrom
agent/remove-resize-iterator-clone
Jul 17, 2026
Merged

Transfer resize iterator buffers without cloning#34
bghira merged 2 commits into
mainfrom
agent/remove-resize-iterator-clone

Conversation

@bghira

@bghira bghira commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • move each completed resize buffer directly into its NumPy ndarray
  • replace indexed storage with a queue so iteration can transfer ownership in O(1)
  • make len(iterator) report the number of remaining results
  • add coverage for values, shapes, exhaustion, and remaining length

Root cause

ResizeIterator.__next__ cloned the full Vec<u8> before constructing every ndarray. The API was described as zero-copy, but consuming it performed one avoidable full-output memory copy per image.

Performance

Median end-to-end iterator consumption on Apple M3 Max:

  • 16 images, 1024x1024 -> 512x512: 2.831 ms -> 1.749 ms (38% faster)
  • 64 images, 512x512 -> 224x224: 2.342 ms -> 1.646 ms (30% faster)

Validation

  • added iterator ownership/length/exhaustion regression coverage
  • cargo test --features opencv,simd (60 passed)
  • Python test suite (69 passed)
  • repository pre-commit hooks, including competitive performance benchmarks

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

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 OpenCV-backed Python resize iterator bindings to avoid per-item buffer cloning by transferring ownership of each completed resize buffer directly into the yielded NumPy ndarray, while also making len(iterator) reflect the number of remaining results.

Changes:

  • Replace indexed buffer storage with a VecDeque so iteration can pop_front() and move buffers into ndarray without cloning.
  • Update iterator construction paths to initialize/convert buffer storage appropriately.
  • Add Python regression coverage for yielded values/shapes, exhaustion behavior, and remaining length reporting.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/python_bindings.rs Switch ResizeIterator to queue-based ownership transfer and remove cloning during __next__.
tests/test_python_bindings.py Add coverage asserting correct shapes/values, exhaustion, and len(iterator) remaining-count semantics.

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

Comment thread src/python_bindings.rs
…ror instead

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@bghira
bghira merged commit 636f7ce into main Jul 17, 2026
10 checks passed
@bghira
bghira deleted the agent/remove-resize-iterator-clone branch July 17, 2026 15:53
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