Skip to content

fix(layout): bound model input copy memory#518

Open
CyMule wants to merge 2 commits into
mainfrom
fix/bound-model-input-copy-memory
Open

fix(layout): bound model input copy memory#518
CyMule wants to merge 2 commits into
mainfrom
fix/bound-model-input-copy-memory

Conversation

@CyMule

@CyMule CyMule commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Copy model inputs into their temporary file in 1 MiB chunks.
  • Avoid reading the complete input into a single bytes allocation.
  • Add regression coverage verifying that the source is never read without a size bound.

Why

process_data_with_model currently calls data.read() before writing the temporary file. This creates an additional allocation proportional to the complete input size and can increase peak memory substantially for large documents.

Chunked copying bounds the temporary copy buffer without changing the resulting file, API behavior, or supported file sizes.

Impact

In a representative end-to-end partition API benchmark using a 70.4 MB two-page PDF:

  • Request-attributable process RSS decreased by 67.2 MiB.
  • Request-attributable container memory decreased by 67.3 MiB.
  • Median latency changed from 16.774s to 18.070s (+7.7%).

The memory reduction is approximately proportional to input size. The current 1 MiB chunk size introduces a measurable throughput tradeoff and may benefit from tuning before merge.

Validation

  • uv run pytest test_unstructured_inference/inference/test_layout.py::test_process_data_with_model_copies_input_in_bounded_chunks -q — 1 passed.
  • uv run ruff check unstructured_inference/inference/layout.py test_unstructured_inference/inference/test_layout.py — passed.

Review in cubic

@CyMule
CyMule marked this pull request as ready for review July 23, 2026 12:48

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 4 files

Shadow auto-approve: would auto-approve. Memory optimization replacing a full read with chunked copy; no behavioral change, API unchanged, regression test added. No operational tradeoff requires human judgement.

Re-trigger cubic

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.

1 participant