Skip to content

perf: lazily materialize raw h2 headers#5123

Open
mcollina wants to merge 1 commit intoperf/h2-header-buffer-cachefrom
perf/h2-lazy-raw-headers
Open

perf: lazily materialize raw h2 headers#5123
mcollina wants to merge 1 commit intoperf/h2-header-buffer-cachefrom
perf/h2-lazy-raw-headers

Conversation

@mcollina
Copy link
Copy Markdown
Member

Summary

This stacks on top of #5122.

It keeps the bounded common-header buffer cache, and adds a follow-up optimization for the HTTP/2 response path:

  • pass parsed response headers as an object to the H2 request handlers
  • defer raw header materialization until controller.rawHeaders is actually accessed
  • update the API handlers to only read controller.rawHeaders when responseHeaders === 'raw'

The goal is to avoid paying the parseH2Headers() allocation cost on the hot path when callers only need the normal parsed header object.

Validation

  • npx eslint lib/core/request.js lib/dispatcher/client-h2.js lib/api/api-request.js lib/api/api-stream.js lib/api/api-connect.js lib/api/api-upgrade.js lib/api/api-pipeline.js
  • npm run test:h2:core
  • npm run test:h2:fetch

Benchmark notes

I verified this against commit 63a1b0ff from #5122 using the same controlled H2 .request() harness and interleaved runs.

Average over 5 runs (80 rounds x 200 parallel requests):

  • 63a1b0ff: 2409.08ms
  • this branch: 2396.64ms

That is roughly a 0.5% improvement on top of the previous optimization.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.86207% with 14 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (perf/h2-header-buffer-cache@63a1b0f). Learn more about missing BASE report.

Files with missing lines Patch % Lines
lib/api/api-pipeline.js 25.00% 6 Missing ⚠️
lib/api/api-stream.js 25.00% 3 Missing ⚠️
lib/api/api-upgrade.js 25.00% 3 Missing ⚠️
lib/api/api-connect.js 33.33% 2 Missing ⚠️
Additional details and impacted files
@@                      Coverage Diff                       @@
##             perf/h2-header-buffer-cache    #5123   +/-   ##
==============================================================
  Coverage                               ?   93.11%           
==============================================================
  Files                                  ?      110           
  Lines                                  ?    35992           
  Branches                               ?        0           
==============================================================
  Hits                                   ?    33514           
  Misses                                 ?     2478           
  Partials                               ?        0           

☔ View full report in Codecov by Sentry.
📢 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.

@trivikr
Copy link
Copy Markdown
Member

trivikr commented Apr 26, 2026

There's an alternative PR which avoids response headers reserialization in #5085, which shows 5-7% improvement for similar tests.

@metcoder95
Copy link
Copy Markdown
Member

Agree with the optimization of #5085 but I also consider that this improvement is worth it, as keeping the raw headers on the controller can enable other use cases around

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.

4 participants