Skip to content

http: improve performance by removing async_hooks #57938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/http/bench-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function main({ len, n }) {
bench.start();
for (let i = 0; i < n; i++) {
parser.execute(header, 0, header.length);
parser.initialize(REQUEST, {});
parser.initialize(REQUEST);
}
bench.end(n);
}

function newParser(type) {
const parser = new HTTPParser();
parser.initialize(type, {});
parser.initialize(type);

parser.headers = [];

Expand Down
8 changes: 0 additions & 8 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ function validateHost(host, name) {
return host;
}

class HTTPClientAsyncResource {
constructor(type, req) {
this.type = type;
this.req = req;
}
}

function ClientRequest(input, options, cb) {
OutgoingMessage.call(this);

Expand Down Expand Up @@ -824,7 +817,6 @@ function tickOnSocket(req, socket) {
const lenient = req.insecureHTTPParser === undefined ?
isLenient() : req.insecureHTTPParser;
parser.initialize(HTTPParser.RESPONSE,
new HTTPClientAsyncResource('HTTPINCOMINGMESSAGE', req),
req.maxHeaderSize || 0,
lenient ? kLenientAll : kLenientNone);
parser.socket = socket;
Expand Down
4 changes: 0 additions & 4 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ function freeParser(parser, req, socket) {
// Make sure the parser's stack has unwound before deleting the
// corresponding C++ object through .close().
setImmediate(closeParserInstance, parser);
} else {
// Since the Parser destructor isn't going to run the destroy() callbacks
// it needs to be triggered manually.
parser.free();
}
}
if (req) {
Expand Down
3 changes: 1 addition & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ function connectionListenerInternal(server, socket) {
// https://github.com/nodejs/node/pull/21313
parser.initialize(
HTTPParser.REQUEST,
new HTTPServerAsyncResource('HTTPINCOMINGMESSAGE', socket),
server.maxHeaderSize || 0,
lenient ? kLenientAll : kLenientNone,
server[kConnections],
Expand Down Expand Up @@ -997,7 +996,7 @@ function resOnFinish(req, res, socket, state, server) {
// If the user never called req.read(), and didn't pipe() or
// .resume() or .on('data'), then we call req._dump() so that the
// bytes will be pulled off the wire.
if (!req._consuming && !req._readableState.resumeScheduled)
if (!req._consuming && !req._readableState.resumeScheduled && !req._readableState.paused)
req._dump();

res.detachSocket(socket);
Expand Down
2 changes: 0 additions & 2 deletions src/async_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ namespace node {
V(HTTP2STREAM) \
V(HTTP2PING) \
V(HTTP2SETTINGS) \
V(HTTPINCOMINGMESSAGE) \
V(HTTPCLIENTREQUEST) \
V(JSSTREAM) \
V(JSUDPWRAP) \
V(MESSAGEPORT) \
Expand Down
99 changes: 33 additions & 66 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "node_buffer.h"
#include "util.h"

#include "async_wrap-inl.h"
#include "env-inl.h"
#include "llhttp.h"
#include "memory_tracker-inl.h"
Expand Down Expand Up @@ -250,17 +249,16 @@ class ConnectionsList : public BaseObject {
std::set<Parser*, ParserComparator> active_connections_;
};

class Parser : public AsyncWrap, public StreamListener {
class Parser : public BaseObject, public StreamListener {
friend class ConnectionsList;
friend struct ParserComparator;

public:
Parser(BindingData* binding_data, Local<Object> wrap)
: AsyncWrap(binding_data->env(), wrap),
: BaseObject(binding_data->env(), wrap),
current_buffer_len_(0),
current_buffer_data_(nullptr),
binding_data_(binding_data) {
}
binding_data_(binding_data) {}

SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(Parser)
Expand Down Expand Up @@ -289,13 +287,7 @@ class Parser : public AsyncWrap, public StreamListener {
Local<Value> cb = object()->Get(env()->context(), kOnMessageBegin)
.ToLocalChecked();
if (cb->IsFunction()) {
InternalCallbackScope callback_scope(
this, InternalCallbackScope::kSkipTaskQueues);

MaybeLocal<Value> r = cb.As<Function>()->Call(
env()->context(), object(), 0, nullptr);

if (r.IsEmpty()) callback_scope.MarkAsFailed();
USE(cb.As<Function>()->Call(env()->context(), object(), 0, nullptr));
}

return 0;
Expand Down Expand Up @@ -442,14 +434,8 @@ class Parser : public AsyncWrap, public StreamListener {

argv[A_UPGRADE] = Boolean::New(env()->isolate(), parser_.upgrade);

MaybeLocal<Value> head_response;
{
InternalCallbackScope callback_scope(
this, InternalCallbackScope::kSkipTaskQueues);
head_response = cb.As<Function>()->Call(
env()->context(), object(), arraysize(argv), argv);
if (head_response.IsEmpty()) callback_scope.MarkAsFailed();
}
MaybeLocal<Value> head_response = cb.As<Function>()->Call(
env()->context(), object(), arraysize(argv), argv);

int64_t val;

Expand Down Expand Up @@ -478,9 +464,10 @@ class Parser : public AsyncWrap, public StreamListener {

Local<Value> buffer = Buffer::Copy(env, at, length).ToLocalChecked();

MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 1, &buffer);
v8::TryCatch try_catch(env->isolate());
USE(cb.As<Function>()->Call(env->context(), object(), 1, &buffer));

if (r.IsEmpty()) {
if (try_catch.HasCaught()) {
got_exception_ = true;
llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception");
return HPE_USER;
Expand Down Expand Up @@ -516,15 +503,11 @@ class Parser : public AsyncWrap, public StreamListener {
if (!cb->IsFunction())
return 0;

MaybeLocal<Value> r;
{
InternalCallbackScope callback_scope(
this, InternalCallbackScope::kSkipTaskQueues);
r = cb.As<Function>()->Call(env()->context(), object(), 0, nullptr);
if (r.IsEmpty()) callback_scope.MarkAsFailed();
}

if (r.IsEmpty()) {
v8::TryCatch try_catch(env()->isolate());
USE(cb.As<Function>()->Call(env()->context(), object(), 0, nullptr));

if (try_catch.HasCaught()) {
got_exception_ = true;
return -1;
}
Expand Down Expand Up @@ -571,17 +554,6 @@ class Parser : public AsyncWrap, public StreamListener {
delete parser;
}


static void Free(const FunctionCallbackInfo<Value>& args) {
Parser* parser;
ASSIGN_OR_RETURN_UNWRAP(&parser, args.This());

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

static void Remove(const FunctionCallbackInfo<Value>& args) {
Parser* parser;
ASSIGN_OR_RETURN_UNWRAP(&parser, args.This());
Expand Down Expand Up @@ -638,25 +610,24 @@ class Parser : public AsyncWrap, public StreamListener {
ConnectionsList* connectionsList = nullptr;

CHECK(args[0]->IsInt32());
CHECK(args[1]->IsObject());

if (args.Length() > 2) {
CHECK(args[2]->IsNumber());
if (args.Length() > 1) {
CHECK(args[1]->IsNumber());
max_http_header_size =
static_cast<uint64_t>(args[2].As<Number>()->Value());
static_cast<uint64_t>(args[1].As<Number>()->Value());
}
if (max_http_header_size == 0) {
max_http_header_size = env->options()->max_http_header_size;
}

if (args.Length() > 3) {
CHECK(args[3]->IsInt32());
lenient_flags = args[3].As<Int32>()->Value();
if (args.Length() > 2) {
CHECK(args[2]->IsInt32());
lenient_flags = args[2].As<Int32>()->Value();
}

if (args.Length() > 4 && !args[4]->IsNullOrUndefined()) {
CHECK(args[4]->IsObject());
ASSIGN_OR_RETURN_UNWRAP(&connectionsList, args[4]);
if (args.Length() > 3 && !args[3]->IsNullOrUndefined()) {
CHECK(args[3]->IsObject());
ASSIGN_OR_RETURN_UNWRAP(&connectionsList, args[3]);
}

llhttp_type_t type =
Expand All @@ -668,13 +639,6 @@ class Parser : public AsyncWrap, public StreamListener {
// Should always be called from the same context.
CHECK_EQ(env, parser->env());

AsyncWrap::ProviderType provider =
(type == HTTP_REQUEST ?
AsyncWrap::PROVIDER_HTTPINCOMINGMESSAGE
: AsyncWrap::PROVIDER_HTTPCLIENTREQUEST);

parser->set_provider_type(provider);
parser->AsyncReset(args[1].As<Object>());
parser->Init(type, max_http_header_size, lenient_flags);

if (connectionsList != nullptr) {
Expand Down Expand Up @@ -820,7 +784,13 @@ class Parser : public AsyncWrap, public StreamListener {
current_buffer_len_ = nread;
current_buffer_data_ = buf.base;

MakeCallback(cb.As<Function>(), 1, &ret);
v8::TryCatch try_catch(env()->isolate());
USE(cb.As<Function>()->Call(env()->context(), object(), 1, &ret));

if (try_catch.HasCaught()) {
got_exception_ = true;
return;
}

current_buffer_len_ = 0;
current_buffer_data_ = nullptr;
Expand Down Expand Up @@ -935,12 +905,12 @@ class Parser : public AsyncWrap, public StreamListener {
url_.ToString(env())
};

MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
arraysize(argv),
argv);
v8::TryCatch try_catch(env()->isolate());
USE(cb.As<Function>()->Call(env()->context(), object(), arraysize(argv), argv));

if (r.IsEmpty())
if (try_catch.HasCaught()) {
got_exception_ = true;
}

url_.Reset();
have_flushed_ = true;
Expand Down Expand Up @@ -1299,9 +1269,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data,
t->Set(FIXED_ONE_BYTE_STRING(isolate, "kLenientAll"),
Integer::NewFromUnsigned(isolate, kLenientAll));

t->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
SetProtoMethod(isolate, t, "close", Parser::Close);
SetProtoMethod(isolate, t, "free", Parser::Free);
SetProtoMethod(isolate, t, "remove", Parser::Remove);
SetProtoMethod(isolate, t, "execute", Parser::Execute);
SetProtoMethod(isolate, t, "finish", Parser::Finish);
Expand Down Expand Up @@ -1372,7 +1340,6 @@ void CreatePerContextProperties(Local<Object> target,
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Parser::New);
registry->Register(Parser::Close);
registry->Register(Parser::Free);
registry->Register(Parser::Remove);
registry->Register(Parser::Execute);
registry->Register(Parser::Finish);
Expand Down
6 changes: 0 additions & 6 deletions test/async-hooks/test-graph.http.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ process.on('exit', () => {
{ type: 'TCPCONNECTWRAP',
id: 'tcpconnect:1',
triggerAsyncId: 'tcp:1' },
{ type: 'HTTPCLIENTREQUEST',
id: 'httpclientrequest:1',
triggerAsyncId: 'tcpserver:1' },
{ type: 'TCPWRAP', id: 'tcp:2', triggerAsyncId: 'tcpserver:1' },
{ type: 'HTTPINCOMINGMESSAGE',
id: 'httpincomingmessage:1',
triggerAsyncId: 'tcp:2' },
{ type: 'Timeout',
id: 'timeout:1',
triggerAsyncId: null },
Expand Down
75 changes: 0 additions & 75 deletions test/async-hooks/test-httpparser-reuse.js

This file was deleted.

Loading