Skip to content

http2: copy small DATA chunks out of socket reads - #67640

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:http2-copy-small-data-chunks
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:http2-copy-small-data-chunks

Conversation

@ShogunPanda

@ShogunPanda ShogunPanda commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

A DATA chunk handed to JavaScript was always a slice of the ArrayBuffer holding the whole socket read. A small chunk queued in a paused stream therefore kept every other byte of that read alive (other frames, padding, ...).

This copies a chunk into a right-sized backing store when the socket read is more than four times larger than the chunk, and keeps slicing it otherwise. With the default limits, full-size DATA frames in a full socket read are still sliced, so bulk transfers keep avoiding the copy.

The new test sends one-byte DATA frames, each sharing a socket read with a large ignored extension frame, leaves the request body unconsumed and checks that every queued chunk is backed by a small ArrayBuffer.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. labels Nov 20, 2026
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.73%. Comparing base (50ba1ec) to head (8a32586).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/node_http2.cc 81.81% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67640      +/-   ##
==========================================
+ Coverage   90.71%   90.73%   +0.01%     
==========================================
  Files         801      801              
  Lines      280906   280914       +8     
  Branches    54246    54249       +3     
==========================================
+ Hits       254834   254890      +56     
+ Misses      16251    16195      -56     
- Partials     9821     9829       +8     
Files with missing lines Coverage Δ
src/node_http2.cc 82.25% <81.81%> (+0.14%) ⬆️

... and 38 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina
mcollina requested review from jasnell and pimterry November 20, 2026 11:00
@mcollina

Copy link
Copy Markdown
Member

Can you run a benchmark?

A DATA chunk handed to JavaScript was always a slice of the ArrayBuffer
holding the whole socket read. A small chunk queued in a paused stream
therefore kept alive every other byte of that read, such as other
frames or padding.

Copy a chunk into a right-sized backing store when the socket read is
more than four times larger than the chunk, and keep slicing it
otherwise, so full-size DATA frames in a full read still avoid the
copy.

Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@ShogunPanda
ShogunPanda force-pushed the http2-copy-small-data-chunks branch from 3c2177e to 8a32586 Compare November 20, 2026 11:20
@lpinca

lpinca commented Nov 20, 2026

Copy link
Copy Markdown
Member

See also #67327.

@ShogunPanda

Copy link
Copy Markdown
Contributor Author

I ran the existing benchmark/http2 suite plus an ad-hoc http2/small-data.js that pumps many small DATA frames, which is the case this change actually copies. 30 runs each; old is the merge-base (50ba1ec), new is this PR (8a32586). Significance is from a Welch t-test (same stars as compare.R). Host: idle-ish Intel i7-6700, powersave governor.

The stock benchmarks that stream large bodies (simple, respond-with-fd, compat, headers) show no statistically significant change. The test-double-http2 ones are noisy.

http2/write.js (100 concurrent streams, 64 KiB and 1 MiB responses) is a consistent ~5% regression (***, 95% CIs about −5.2% to −4.3% and −6.0% to −5.1%). http2/full-duplex.js (4 MiB each way) is a smaller but also significant −1.7% (CI −2.2% to −1.2%).

The small-chunk bench is flat at 16, 256, 1024 and 16384 bytes. At 4096-byte chunks it is significantly faster (+15–17% both upload and download). That size is under the 4× slice threshold for a typical 64 KiB socket read, so the copy path is taken; not retaining the whole read buffer may be helping.

Happy to adjust the threshold or add this bench to the tree if useful.

Re #67327: that change gives small stream reads their own buffer (TLS/slab). HTTP/2 DATA chunks were still views over the whole socket read, which is what this PR addresses.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not convinced this is worth doing

@ShogunPanda

Copy link
Copy Markdown
Contributor Author

@mcollina Let me know what to do: shall I close it or what?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants