Skip to content

Write and read HTTP/2 frames with fewer copies and writes - #1487

Merged
quinnj merged 4 commits into
masterfrom
claude/h2-frame-writes
Oct 22, 2026
Merged

quinnj merged 4 commits into
masterfrom
claude/h2-frame-writes

Conversation

@quinnj

@quinnj quinnj commented Oct 22, 2026

Copy link
Copy Markdown
Member

Every HTTP/2 frame the client or server wrote went through a fresh IOBuffer: _serialize_frame copied the payload, the 9-byte header went into its own vector, both were written into the IOBuffer, and take! produced the bytes to send. Returning flow-control credit wrote two WINDOW_UPDATE frames (connection and stream) as two separately locked writes, after every body_read!. Reading allocated a 9-byte vector for each frame header and a 4-byte payload for each WINDOW_UPDATE.

Now:

  • A frame is serialized straight into one exact-size vector (_frame_bytes), without the payload copy or IOBuffer, and write_frame! writes it in one call.
  • A credit's connection and stream WINDOW_UPDATE frames are built in one 26-byte vector and sent in one write under one lock (_window_update_frames), on both client and server.
  • When the connection reader already buffers them, a frame header and a WINDOW_UPDATE frame are parsed in place.
  • _header_block_frames no longer copies a header block that fits in one frame; every caller writes the frame immediately.

The bytes on the wire are unchanged, in the same order.

Whole-process allocations for a 1 MiB HTTP/2 upload (client and server in one process, --threads=4,1, minimum of 5):

Julia master this PR
1.10 1899 901
1.11 2633 1157
1.12 2811 1162
1.13 2532 1156

Julia 1.12, in-process round trips (latency: median and min–max of per-run medians over 4 interleaved runs per build, heavily loaded machine):

master this PR
1 MiB POST, allocations per request 2884 1258
1 MiB POST, latency 24.1 ms (22.8–26.1) 23.3 ms (18.3–25.6)
GET, allocations per request 449 410
GET, latency 443 µs (340–495) 433 µs (372–480)
socket writes per credit returned 2 1

Reading a buffered WINDOW_UPDATE frame allocates 16 bytes (the returned frame) instead of 144–160 on Julia 1.10 through 1.13.

Verification

  • New test in test/http2_frame_tests.jl: _window_update_frames produces the same bytes as write_frame! of the two frames; frames read through a 4-byte _ConnReader buffer (headers and payloads split across reads) and through a prefilled one parse the same; reading a buffered WINDOW_UPDATE allocates at most 64 bytes (144–160 on master).
  • http2_frame_tests.jl, http2_server_tests.jl, http2_client_tests.jl, http2_settings_tests.jl, http2_refused_stream_retry_tests.jl pass on 1.12. Rebased on Fix HTTP/2 server receive-window accounting #1452, whose receive-window accounting in _send_h2_server_window_updates! is kept. The full suite with this and my other open allocation changes combined passes on 1.12 except trim_compile_tests.jl, which fails the same way on master locally (a verifier warning inside Reseau.TLS._read_some!).

Review

  • _serialize_frame has one caller, _frame_bytes, which copies the payload into the new vector, so returning the frame's own payload vector is safe.
  • The in-place paths run only for a _ConnReader with the whole header (9 bytes) or WINDOW_UPDATE payload (4 bytes) buffered; otherwise the generic path runs. The fast WINDOW_UPDATE path applies the same length, reserved-bit mask, and zero-increment checks and errors as the generic one.
  • Window updates were previously two lock acquisitions, so another writer's frame could fall between them; now they are adjacent. Both orders are valid HTTP/2. Errors are handled as before (EOF and closing errors are ignored; others rethrown), and the client still sets its write deadline before writing.
  • The client serializes a frame before taking the write lock rather than inside it; frames are immutable and serialization touches no shared state.

🤖 Generated with Claude Code

quinnj and others added 3 commits October 22, 2026 02:35
Every frame the client or server wrote went through a fresh IOBuffer:
the payload was copied into a tuple, the header into its own vector,
both into the IOBuffer, and out with take!. Each flow-control credit
became two separately locked writes, one per WINDOW_UPDATE frame, and
every frame read allocated a 9-byte header vector. A 1 MiB HTTP/2 upload
made about 2,800 allocations in client and server together.

Frames are now serialized into one exact-size vector, a credit's
connection and stream WINDOW_UPDATE frames go out in one write, and
frame headers and WINDOW_UPDATE frames already buffered in the
connection reader are parsed in place. The bytes on the wire are
unchanged. The same upload now makes about 1,160 allocations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.61%. Comparing base (84a7fe5) to head (33afa85).
⚠️ Report is 29 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1487      +/-   ##
==========================================
+ Coverage   91.55%   91.61%   +0.05%     
==========================================
  Files          31       31              
  Lines       12972    13081     +109     
==========================================
+ Hits        11877    11984     +107     
- Misses       1095     1097       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

# Conflicts:
#	CHANGELOG.md
#	src/http2_server.jl
@quinnj
quinnj merged commit a6eebc4 into master Oct 22, 2026
0 of 8 checks passed
@quinnj
quinnj deleted the claude/h2-frame-writes branch October 22, 2026 12:32
quinnj referenced this pull request Oct 22, 2026
URIs 1.7.2 rejects "http://[::1]evil.com/" when building the URI, so the test now accepts either rejection; with older URIs the URI still parses and HTTP must throw ArgumentError. Test-only change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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