← Back to Pull requests

Speed up HTTP header materialization #66257

Closed
unknown closed this pull request without merging on Sep 26, 2026 18:27 UTC
View on GitHub
unknown unknown commented on Sep 24, 2026 10:29 UTC
Author

When you read headers from a request or response, it's lazily instantiated by iterating over raw headers and calling _addHeaderLine to combine each value into the final object. This method calls matchKnownFields to check how duplicate headers should be combined based on various known header names.

To do this, matchKnownFields returned a composite string embedding the merge rule: a 0/1/2 control character defining the merge rule (combine with comma, with semi-colon, or set-cookie array special case) concatenated with the header name itself, like \u0002cookie. Only known single-value headers (like host) were passed through untouched.

Then, in _addHeaderLine we had to check and and read the control character, slice out the rest of the header name, and manage duplicates according to the relevant control char rule.

This results in a string concat, chatAt(0) check, and then slice(1) with a new string allocation for each header in most cases. These fresh string slices are then each used as keys in the resulting headers object, which is itself expensive for fresh strings. Each of these is more allocations and work on a hot & frequently used path.

With this PR, I've replaced the control char approach with pregenerated { name, merge } objects with a fixed string & merge value for each known header. Unknown headers are returned as lowercase strings (so they keep lowercasing as before when required, but lose the \u0000 concat and then slice step). Then matchKnownFields just returns the existing object, and _addHeaderLine just checks the merge policy and uses the existing string from the pregenerated object. No allocations on the hot path for known headers or lowercase unknown headers.

This PR also fixes the incoming_headers benchmark, which shadowed the headers param so this was never actually used, and extends it with a new param to test performance when req.headers is read.

I've looked at other extensions like precalculating more of this in C++ up front - I think this captures most of the benefit here already quite simply with no downside, so we can leave exploring that more to future PRs.

Microbenchmarking, the change here speeds up req.headers and res.headers up to 45% in the best case (a small set of known headers) down to about 7% speed-up for sets of mostly unknown headers.

End to end, I see incoming headers with headers=20 and req.headers boosting RPS up by 4%, using the existing (but fixed) incoming_headers benchmark example. In best cases (small sets of all-known headers) this boost RPS for me up to 13% end to end. No performance regressions visible anywhere else.

<!--
If you are submitting a pull request for the first time,
check out the guide for first-time contributors for tips and answers to FAQs:
https://github.com/nodejs/node/blob/HEAD/doc/contributing/first-contributions.md

Before submitting a pull request, please read:

  • the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md

  • the commit message formatting guidelines at

  • https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

    For code changes:
    1. Include tests for any bug fixes or new features.
    2. Update documentation if relevant.
    3. Ensure that make -j4 test (UNIX), or vcbuild test (Windows) passes.

    If you believe this PR should be highlighted in the Node.js CHANGELOG
    please add the notable-change label.

    Developer's Certificate of Origin 1.1

    By making a contribution to this project, I certify that:

    (a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

    (b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

    (c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

    (d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
    -->

    Diff: 581 lines (+435 / -146) across 7 files Reviewer: efekrskl Review duration: 3358m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 581 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.381
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #66257"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.9% (MEDIUM) · Higher = larger blast radius
    49.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%
    Repository