Skip to content

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Feb 9, 2026

Summary

  • Add V8 Fast API support for HTTP parser methods: free, remove, pause, resume, and unconsume
  • These methods bypass V8's argument marshaling overhead on the fast path
  • Provides modest performance improvements (2-3%) in HTTP client request body handling benchmarks

Implementation Details

  • Added *Impl() helper functions to share logic between slow and fast paths
  • Fast API functions use TRACK_V8_FAST_API_CALL for tracing
  • Methods registered on instance template using SetFastMethod
  • External references registered for snapshot support

Note: Close() and Consume() cannot use Fast API because:

  • Close() deletes the parser object (unsafe in fast path)
  • Consume() requires unwrapping StreamBase from JS object

Benchmark Results (10 runs, statistically significant)

Benchmark Change Confidence
client-request-body.js method='end' len=1024 type='buf' +3.19% **
client-request-body.js method='write' len=256 type='utf' +3.56% *
client-request-body.js method='write' len=256 type='buf' +2.86% *
client-request-body.js method='end' len=256 type='buf' +2.67% **
headers.js group='fewHeaders' len=5 n=10 +2.65% *
simple.js chunkedEnc=0 c=50 chunks=1 len=4 type='buffer' +2.27% **

Test plan

  • All HTTP/HTTPS tests pass (730 tests)
  • Benchmark comparison with main branch

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. needs-ci PRs that need a full CI run. labels Feb 9, 2026
@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 73.46939% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.72%. Comparing base (1e8a639) to head (b83b335).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/node_http_parser.cc 73.46% 12 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61751      +/-   ##
==========================================
- Coverage   89.73%   89.72%   -0.01%     
==========================================
  Files         675      675              
  Lines      204525   204672     +147     
  Branches    39314    39320       +6     
==========================================
+ Hits       183523   183645     +122     
+ Misses      13300    13296       -4     
- Partials     7702     7731      +29     
Files with missing lines Coverage Δ
src/node_http_parser.cc 83.96% <73.46%> (-0.77%) ⬇️

... and 50 files with indirect coverage changes

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

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds V8 Fast API support to several HTTPParser instance methods in src/node_http_parser.cc to reduce argument marshaling overhead and improve HTTP client request body handling performance.

Changes:

  • Added Fast API variants for free, remove, pause, resume, and unconsume, with shared *Impl() helpers for slow/fast paths.
  • Registered Fast API methods via SetFastMethod(...) and added v8::CFunction external references for snapshot compatibility.
  • Added Fast API call tracing via TRACK_V8_FAST_API_CALL.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 632 to 636
// Since the Parser destructor isn't going to run the destroy() callbacks
// it needs to be triggered manually.
parser->EmitTraceEventDestroy();
parser->EmitDestroy();
}
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

FreeImpl() calls EmitTraceEventDestroy()/EmitDestroy(). AsyncWrap::EmitDestroy() can schedule SetImmediate / microtasks and touch V8 handles, which is not safe to run from a V8 Fast API callback. The fast path should either be removed for free, or FastFree should take FastApiCallbackOptions& and force a fallback to the slow callback whenever destroy hooks are enabled (or whenever it would call into V8).

Copilot uses AI. Check for mistakes.
Comment on lines +771 to +775
static void FastPause(Local<Object> receiver) {
if constexpr (should_pause) {
TRACK_V8_FAST_API_CALL("http_parser.pause");
} else {
TRACK_V8_FAST_API_CALL("http_parser.resume");
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

FastPause()/FastResume() bypass the context invariant enforced in the slow path (CHECK_EQ(env, parser->env())). That means cross-context usage would no longer be caught on the fast path, potentially masking bugs and diverging behavior. Consider adding a fast-path guard (e.g., obtain the current Environment* and CHECK_EQ, or use FastApiCallbackOptions& to fall back to the slow callback when the current context/Environment does not match).

Copilot uses AI. Check for mistakes.
Add V8 Fast API support for the following HTTP parser methods:
- free(): triggers destroy callbacks
- remove(): removes parser from connections list
- pause(): pauses the HTTP parser
- resume(): resumes the HTTP parser
- unconsume(): removes parser as stream listener

The following methods were intentionally not converted:
- close(): deletes the parser object, unsafe in Fast API
- consume(): requires unwrapping StreamBase from JS object

This improves performance by bypassing V8's argument marshaling
overhead for these frequently called methods.
@mcollina mcollina force-pushed the http-parser-fast-api branch from a8e3a7f to b83b335 Compare February 9, 2026 17:58
@gurgunday gurgunday added the needs-benchmark-ci PR that need a benchmark CI run. label Feb 9, 2026
Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

Looks great! I tried doing this a few months ago and didn't get any performance improvements. But maybe I did something wrong. The code LGTM.

gurgunday

This comment was marked as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants