Skip to content

Avoid cloning immutable decode inputs#36

Merged
bghira merged 1 commit into
mainfrom
agent/zero-copy-decode-input
Jul 17, 2026
Merged

Avoid cloning immutable decode inputs#36
bghira merged 1 commit into
mainfrom
agent/zero-copy-decode-input

Conversation

@bghira

@bghira bghira commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • retain Python bytes storage with PyBackedBytes while decoding outside the GIL
  • remove the unconditional encoded-buffer to_vec() copy
  • preserve safe behavior for mutable bytearray inputs through PyO3-owned snapshot storage
  • add decode coverage for both input types

Root cause

The GIL-release path cloned every encoded input before decode so the borrowed Python slice could cross the allow_threads boundary. PyO3 already provides a Send/Sync owned view that keeps immutable Python bytes alive without copying.

Performance

The avoided copy is proportional to encoded input size. A 32 MiB rejected-input probe fell from 0.500 ms to effectively decoder-call overhead (0.0005 ms), isolating the removed copy. For real images, decoder work still dominates; this is a smaller win than the resize/crop changes.

Validation

  • bytes and bytearray decode regression tests
  • cargo test --features opencv,simd (60 passed)
  • Python test suite (70 passed)
  • repository pre-commit hooks, including competitive performance benchmarks

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

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 optimizes the Python imdecode_py binding to avoid an unconditional clone of immutable encoded inputs when decoding outside the GIL, while adding regression coverage for both immutable and mutable input buffers.

Changes:

  • Switch imdecode_py to accept pyo3::pybacked::PyBackedBytes and remove the unconditional to_vec() copy before allow_threads.
  • Add a Python test to validate decoding works for both bytes and bytearray inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_python_bindings.py Adds coverage ensuring imdecode_py works for both bytes and bytearray buffers.
src/python_bindings.rs Removes the unconditional encoded-buffer clone by using PyBackedBytes across the allow_threads boundary.

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

@bghira
bghira merged commit 6b803ab into main Jul 17, 2026
11 checks passed
@bghira
bghira deleted the agent/zero-copy-decode-input 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