Skip to content

perf: Free decode buffers sooner when restoring original elements - #4524

Draft
CyMule wants to merge 8 commits into
mainfrom
perf/release-orig-element-decode-buffers
Draft

CyMule wants to merge 8 commits into
mainfrom
perf/release-orig-element-decode-buffers

Conversation

@CyMule

@CyMule CyMule commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Restoring orig_elements now releases each intermediate buffer before the next stage builds its own copy. Previously the compressed bytes, decompressed bytes and JSON text all stayed alive until the end.

Restoring one element that carries a 39-megapixel image:

  • Peak memory: about 149 MiB lower (589,124 → 436,792 KiB).
  • Under a 400 MiB limit, main is OOM-killed and this branch completes.
  • Restored elements are identical.

Behavior

  • Unchanged:
    • the Base64, zlib, UTF-8, JSON and element constructors;
    • restored values, metadata order and error behavior;
    • the existing 200 MiB decompressed-byte cap.
  • No new cap is added. The existing cap bounds UTF-8 bytes, not Python string size, so wide-Unicode JSON can still grow in memory. The full element graph is still returned.
  • This removes avoidable overlap between buffers. It does not guarantee that every input fits a fixed memory budget.

Validation

  • Staging tests: 143 passed, 1 skipped. Ten new regression tests cover Unicode, nested originals, metadata and order, malformed input, trailing data and the exact size boundary.
  • A tracemalloc test checks that json.loads and element construction each start with only their own input alive. Removing any one of the three releases fails it.
  • The ten new tests also pass in an isolated Linux container against the module from this commit (import path and SHA256 verified).
  • Included benchmark: one element with a 39-megapixel BMP stored as a 156,000,072-character Base64 payload.
    • At a 400 MiB limit, main is OOM-killed (exit 137). This branch completes with identical metadata, text and payload hash.
    • At 1 GiB, peak RSS falls from 589,124 to 436,792 KiB. Decode takes 0.745 s on main and 0.768 s on this branch.
    • Escaped wide-Unicode text and raw UTF-8 mixed with the image payload also restore identically. The raw UTF-8 case falls from 1,046,436 to 893,940 KiB at 2 GiB.
  • Ruff, diff checks and the version checker pass.

Release

Proposes 0.27.24, after #4419 (0.27.19), #4516 (0.27.20), #4517 (0.27.21), #4357 (0.27.22) and #4523 (0.27.23). Renumber if that order changes.

#4517 changes the serializing side of orig_elements in the same module. A test merge with this branch conflicts only on version and changelog lines, passes the staging, element and chunking suites, and round-trips identical elements.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@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 5 files

Shadow auto-approve: would not auto-approve. This PR does not meet the repository auto-approval settings.

Re-trigger cubic

@CyMule
CyMule marked this pull request as draft October 2, 2026 03:45
@CyMule CyMule changed the title perf: Release consumed original element decode buffers perf: Free decode buffers sooner when restoring original elements Oct 5, 2026

This branch has not been deployed

No deployments
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