Skip to content

perf: optimize http/2 request hot path#5483

Merged
mcollina merged 1 commit into
mainfrom
perf/h2-hot-path
Jul 2, 2026
Merged

perf: optimize http/2 request hot path#5483
mcollina merged 1 commit into
mainfrom
perf/h2-hot-path

Conversation

@mcollina

@mcollina mcollina commented Jul 2, 2026

Copy link
Copy Markdown
Member

Profiled the H2 client and removed the main client-side hot spots:

  • completeRequest: fast-path in-order completions by advancing kRunningIdx (like H1) instead of an O(n) indexOf+splice; out-of-order completions keep the splice path
  • use on instead of once for self-removing stream handlers, and skip listener removal on already-closed streams (handlers null-guard on the stream state)
  • single pre-shaped stream state object (stable hidden class), finalizeRequest/openStream hoisted out of per-request closures
  • cache the session ref state to skip redundant ref()/unref() calls (session ref proxies to the socket)
  • memoize getProtocolFromUrlString

Benchmarks (single connection, local H2 server, interleaved A/B vs main): +5-6% req/s at 100 parallel requests, +27% at 1000, +51% at 2000; +15% with a 10-connection Pool. Remaining cost is dominated by node core session.request() internals.

🤖 Generated with Claude Code

Fast-path in-order completions in completeRequest, avoid once wrappers
and dead-stream listener removal, use a single pre-shaped stream state
object, cache the session ref state, and memoize
getProtocolFromUrlString.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina force-pushed the perf/h2-hot-path branch from 95e5ca3 to 86faa6a Compare July 2, 2026 05:34
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.08911% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.43%. Comparing base (7d78f7d) to head (86faa6a).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
lib/dispatcher/client-h2.js 90.05% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5483      +/-   ##
==========================================
- Coverage   93.45%   93.43%   -0.03%     
==========================================
  Files         110      110              
  Lines       37147    37247     +100     
==========================================
+ Hits        34717    34800      +83     
- Misses       2430     2447      +17     

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

@mcollina mcollina merged commit b39c77d into main Jul 2, 2026
36 of 38 checks passed
@mcollina mcollina deleted the perf/h2-hot-path branch July 2, 2026 14:31
@github-actions github-actions Bot mentioned this pull request Jul 4, 2026
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.

3 participants