Repository navigation
perf: inline content stream setup - #750
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors request body reading in lib/read.js by inlining the content stream initialization into read, keeping stream and content-length handling local and avoiding temporary mutation of req.length / stream.length.
Changes:
- Inline
content-encodingdetection and inflate gating directly intoread. - Only read
content-lengthwhencontent-encodingisidentityand usereqdirectly as the stream. - Remove the now-unnecessary private
contentstreamhelper.
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Inline content stream initialization into `read` to keep the stream and content-length state local. Only read `content-length` when needed and avoid temporary mutations of `req.length` and `stream.length`.
4212abe to
987a00a
Compare
kilisamemarisaaa
left a comment
There was a problem hiding this comment.
I ran a direct baseline comparison to help answer the performance question on this PR. The code paths were tested at exact PR head 987a00a36149c6bd8d9b5f88d8e69d3a7a5a6d6c and base b1f3d588c5dca4b3a4ca7d5d3765c9129118926a on Windows with Node v24.12.0.
Method: an http server using bodyParser.json(), a 25-byte JSON body, keep-alive agent, 50 concurrent workers, 1,000-request warm-up, then ten 5,000-request batches per process. I ran both base/head and head/base orderings to reduce ordering bias.
Median results (5,000 requests):
- base: 0.3618 s (13,819 req/s), head: 0.3836 s (13,033 req/s)
- base: 0.3815 s (13,105 req/s), head: 0.4099 s (12,199 req/s)
- reverse order β head: 0.4028 s (12,413 req/s), base: 0.3792 s (13,187 req/s)
- reverse order β head: 0.3812 s (13,116 req/s), base: 0.3722 s (13,434 req/s)
This noisy microbenchmark does not show a measurable improvement; the PR head was 2%β7% slower in these paired runs. The existing test suite and lint are green, so I am not reporting a functional regression, but the current performance claim is not supported by this measurement. Could you add a reproducible benchmark (including workload and sample size) or explain which workload is expected to benefit? That would let maintainers evaluate whether the extra inlining complexity buys a real gain.
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [body-parser](https://github.com/expressjs/body-parser) | [`2.3.0` β `2.4.0`](https://renovatebot.com/diffs/npm/body-parser/2.3.0/2.4.0) |  |  | --- >β οΈ **Warning** > > Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/99) for more information. --- ### Release Notes <details> <summary>expressjs/body-parser (body-parser)</summary> ### [`v2.4.0`](https://github.com/expressjs/body-parser/blob/HEAD/HISTORY.md#240) [Compare Source](expressjs/body-parser@v2.3.0...v2.4.0) \===== - Security fix for [GHSA-27r3-jxmh-p828](GHSA-27r3-jxmh-p828) (CVE-2026-111181) - feat: add `zstd` content-encoding support ([#​586](expressjs/body-parser#586)) - Requires Node.js `^22.15.0` or `>=23.8.0`; older versions keep returning `415` for `zstd` encoded bodies - fix: avoid mutating the parser options and the request object while reading the body ([#​742](expressjs/body-parser#742), [#​750](expressjs/body-parser#750)) - perf(json): avoid first-char regex for direct JSON bodies ([#​744](expressjs/body-parser#744)) - deps: - content-type@^2.1.0 - iconv-lite^0.7.3 - qs@^6.16.1 </details> --- ### Configuration π **Schedule**: (UTC) - Branch creation - "before 9am on tuesday" - Automerge - At any time (no schedule defined) π¦ **Automerge**: Disabled by config. Please merge this manually once you are satisfied. β» **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. π **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNzUuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjE3NS4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==--> Reviewed-on: https://code.futo.org/harbor/harbor/pulls/2043
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [body-parser](https://github.com/expressjs/body-parser) | [`2.3.0` β `2.4.0`](https://renovatebot.com/diffs/npm/body-parser/2.3.0/2.4.0) |  |  | --- >β οΈ **Warning** > > Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/99) for more information. --- ### Release Notes <details> <summary>expressjs/body-parser (body-parser)</summary> ### [`v2.4.0`](https://github.com/expressjs/body-parser/blob/HEAD/HISTORY.md#240) [Compare Source](expressjs/body-parser@v2.3.0...v2.4.0) \===== - Security fix for [GHSA-27r3-jxmh-p828](GHSA-27r3-jxmh-p828) (CVE-2026-111181) - feat: add `zstd` content-encoding support ([#​586](expressjs/body-parser#586)) - Requires Node.js `^22.15.0` or `>=23.8.0`; older versions keep returning `415` for `zstd` encoded bodies - fix: avoid mutating the parser options and the request object while reading the body ([#​742](expressjs/body-parser#742), [#​750](expressjs/body-parser#750)) - perf(json): avoid first-char regex for direct JSON bodies ([#​744](expressjs/body-parser#744)) - deps: - content-type@^2.1.0 - iconv-lite^0.7.3 - qs@^6.16.1 </details> --- ### Configuration π **Schedule**: (UTC) - Branch creation - "before 9am on tuesday" - Automerge - At any time (no schedule defined) π¦ **Automerge**: Disabled by config. Please merge this manually once you are satisfied. β» **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. π **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNzUuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjE3NS4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==--> Reviewed-on: https://code.futo.org/harbor/harbor/pulls/2043
Inline content stream initialization into
readto keep the stream and content-length state local. Only readcontent-lengthwhen needed and avoid temporary mutations ofreq.lengthandstream.length.