Skip to content
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

http: use v8::Array::New() with a prebuilt vector #46447

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Changes from 1 commit
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
51 changes: 22 additions & 29 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1037,68 +1037,60 @@ void ConnectionsList::New(const FunctionCallbackInfo<Value>& args) {

void ConnectionsList::All(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Local<Array> all = Array::New(isolate);
ConnectionsList* list;

ASSIGN_OR_RETURN_UNWRAP(&list, args.Holder());

uint32_t i = 0;
std::vector<Local<Value>> result;
ronag marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

What about using MaybeStackBuffer to avoid allocations if number of elements is not too high?

Copy link
Member Author

@joyeecheung joyeecheung Feb 4, 2023

Choose a reason for hiding this comment

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

I think v8::Array -> std::vector would make sense (to avoid C++ -> JS invocation costs) but std::vector -> MaybeStackBuffer probably would not make much difference...(and personally I prefer STL unless the home-grown alternatives makes a significant difference)

for (auto parser : list->all_connections_) {
if (all->Set(context, i++, parser->object()).IsNothing()) {
return;
}
result.emplace_back(parser->object());
}

return args.GetReturnValue().Set(all);
return args.GetReturnValue().Set(
Array::New(isolate, result.data(), result.size())
);
}

void ConnectionsList::Idle(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Local<Array> idle = Array::New(isolate);
ConnectionsList* list;

ASSIGN_OR_RETURN_UNWRAP(&list, args.Holder());

uint32_t i = 0;
std::vector<Local<Value>> result;
ronag marked this conversation as resolved.
Show resolved Hide resolved
for (auto parser : list->all_connections_) {
if (parser->last_message_start_ == 0) {
if (idle->Set(context, i++, parser->object()).IsNothing()) {
return;
}
result.emplace_back(parser->object());
}
}

return args.GetReturnValue().Set(idle);
return args.GetReturnValue().Set(
Array::New(isolate, result.data(), result.size())
);
}

void ConnectionsList::Active(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Local<Array> active = Array::New(isolate);
ConnectionsList* list;

ASSIGN_OR_RETURN_UNWRAP(&list, args.Holder());

uint32_t i = 0;
std::vector<Local<Value>> result;
ronag marked this conversation as resolved.
Show resolved Hide resolved
for (auto parser : list->active_connections_) {
if (active->Set(context, i++, parser->object()).IsNothing()) {
return;
}
result.emplace_back(parser->object());
}

return args.GetReturnValue().Set(active);
return args.GetReturnValue().Set(
Array::New(isolate, result.data(), result.size())
);
}

void ConnectionsList::Expired(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Local<Array> expired = Array::New(isolate);
ConnectionsList* list;

ASSIGN_OR_RETURN_UNWRAP(&list, args.Holder());
Expand All @@ -1110,7 +1102,7 @@ void ConnectionsList::Expired(const FunctionCallbackInfo<Value>& args) {
static_cast<uint64_t>(args[1].As<Uint32>()->Value()) * 1000000;

if (headers_timeout == 0 && request_timeout == 0) {
return args.GetReturnValue().Set(expired);
return args.GetReturnValue().Set(Array::New(isolate, 0));
} else if (request_timeout > 0 && headers_timeout > request_timeout) {
std::swap(headers_timeout, request_timeout);
}
Expand All @@ -1121,9 +1113,10 @@ void ConnectionsList::Expired(const FunctionCallbackInfo<Value>& args) {
const uint64_t request_deadline =
request_timeout > 0 ? now - request_timeout : 0;

uint32_t i = 0;
auto iter = list->active_connections_.begin();
auto end = list->active_connections_.end();

std::vector<Local<Value>> result;
ronag marked this conversation as resolved.
Show resolved Hide resolved
while (iter != end) {
Parser* parser = *iter;
iter++;
Expand All @@ -1136,15 +1129,15 @@ void ConnectionsList::Expired(const FunctionCallbackInfo<Value>& args) {
request_deadline > 0 &&
parser->last_message_start_ < request_deadline)
) {
if (expired->Set(context, i++, parser->object()).IsNothing()) {
return;
}
result.emplace_back(parser->object());

list->active_connections_.erase(parser);
}
}

return args.GetReturnValue().Set(expired);
return args.GetReturnValue().Set(
Array::New(isolate, result.data(), result.size())
);
}

const llhttp_settings_t Parser::settings = {
Expand Down