Skip to content

perf(client): parse h1 content-length statelessly#5124

Merged
mcollina merged 1 commit intonodejs:mainfrom
trivikr:header-content-length-statelss
Apr 30, 2026
Merged

perf(client): parse h1 content-length statelessly#5124
mcollina merged 1 commit intonodejs:mainfrom
trivikr:header-content-length-statelss

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented Apr 26, 2026

This relates to...

N/A

Rationale

Avoid allocating and parsing Content-Length response headers through strings.
Since llhttp already validates Content-Length as 1*DIGIT and rejects duplicate values, the H1 parser can parse the numeric value directly from the header buffer and use -1 to represent absence.

Changes

  • Parse H1 response Content-Length directly from the header buffer.
  • Use -1 as the parser sentinel for missing Content-Length.
  • Check response body length against the parsed numeric value on message completion.

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

Assisted-by: openai:gpt-5.4

@trivikr

This comment was marked as outdated.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.13%. Comparing base (5c96f7d) to head (676da7e).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5124      +/-   ##
==========================================
- Coverage   93.14%   93.13%   -0.01%     
==========================================
  Files         110      110              
  Lines       35898    36109     +211     
==========================================
+ Hits        33436    33629     +193     
- Misses       2462     2480      +18     

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

Assisted-by: openai:gpt-5.4
Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Co-authored-by: tsctx <91457664+tsctx@users.noreply.github.com>
@trivikr trivikr force-pushed the header-content-length-statelss branch from 9576bfa to 676da7e Compare April 26, 2026 16:00
@trivikr
Copy link
Copy Markdown
Member Author

trivikr commented Apr 26, 2026

The benchmark improved after using for loop instead of array.reduce

benchmark                   avg (min … max) p75 / p99    (min … top 1%)
------------------------------------------- -------------------------------
• parse content-length
------------------------------------------- -------------------------------
legacy                       392.90 ns/iter 390.98 ns  █                   
                      (376.62 ns … 1.64 µs) 434.16 ns ██▇▃ ▄               
                    ( 95.61  b … 310.16  b) 104.99  b ██████▆▅▂▃▂▂▃▁▁▁▂▁▁▁▁

stateless                     38.53 ns/iter  38.28 ns               █      
                     (28.36 ns … 104.78 ns)  42.76 ns               █      
                    (  0.09  b …  80.11  b)   0.26  b ▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▃▁▁▁▁▁

Marking this PR as ready for review.

@trivikr trivikr marked this pull request as ready for review April 26, 2026 16:01
Comment on lines +458 to +460
for (let i = 0; i < buf.length; i++) {
this.contentLength = (this.contentLength * 10) + (buf[i] - 0x30)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so basically parseInt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But takes 10X less time #5124 (comment)

@trivikr trivikr requested a review from Uzlopak April 30, 2026 01:49
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit cad3f70 into nodejs:main Apr 30, 2026
35 checks passed
@trivikr trivikr deleted the header-content-length-statelss branch May 1, 2026 00:29
@github-actions github-actions Bot mentioned this pull request May 1, 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.

6 participants