Skip to content

Avoid quadratic copying when sending large HTTP/2 request bodies - #1127

Merged
Kludex merged 2 commits into
mainfrom
http2-offset-send
Aug 9, 2026
Merged

Avoid quadratic copying when sending large HTTP/2 request bodies#1127
Kludex merged 2 commits into
mainfrom
http2-offset-send

Conversation

@Kludex

@Kludex Kludex commented Aug 9, 2026

Copy link
Copy Markdown
Member

Closes #1039.

_send_stream_data rebuilt the remaining body tail as a fresh bytes object on every frame, copying ~n²/2k bytes for an n byte body sent in k byte frames. This replaces it with an offset-based loop so each byte is sliced once.

Benchmark sending a single bytes body through HTTP2Connection over a MockStream (16 KiB frames):

Body size Before After
16 MiB 0.153s 0.005s
64 MiB 2.226s 0.021s
256 MiB 35.748s 0.088s

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

Review in cubic

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

@codspeed-hq

codspeed-hq Bot commented Aug 9, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by ×290

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 16 untouched benchmarks
⏩ 7 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory test_bench_http2_send_large_body 16,375.9 KB 55.9 KB ×290

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing http2-offset-send (e891529) with main (d6d7ec3)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4dcdf3457d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpcore2/httpcore2/_sync/http2.py

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

Re-trigger cubic

@Kludex
Kludex merged commit 586f968 into main Aug 9, 2026
19 checks passed
@Kludex
Kludex deleted the http2-offset-send branch August 9, 2026 08:34
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.

Quadratic behaviour when streaming large bytes bodies as HTTP/2

1 participant