Skip to content

Commit c8fc217

Browse files
trevnorrisFishrock123
authored andcommitted
node: improve GetActiveHandles performance
Improve performance of process._getActiveHandles by sending handles in batches to JS to be set on the passed Array. Add test to check proper active handles are returned. Alter implementation of GetActiveRequests to match GetActiveHandles' implementation. PR-URL: nodejs#3780 Reviewed-By: Fedor Indutny <[email protected]>
1 parent e742422 commit c8fc217

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

src/node.cc

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,27 +1608,21 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
16081608
Local<Array> ary = Array::New(args.GetIsolate());
16091609
Local<Context> ctx = env->context();
16101610
Local<Function> fn = env->push_values_to_array_function();
1611-
static const size_t argc = 8;
1612-
Local<Value> argv[argc];
1613-
size_t i = 0;
1611+
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
1612+
size_t idx = 0;
16141613

16151614
for (auto w : *env->req_wrap_queue()) {
1616-
if (w->persistent().IsEmpty() == false) {
1617-
argv[i++ % argc] = w->object();
1618-
if ((i % argc) == 0) {
1619-
HandleScope scope(env->isolate());
1620-
fn->Call(ctx, ary, argc, argv).ToLocalChecked();
1621-
for (auto&& arg : argv) {
1622-
arg = Local<Value>();
1623-
}
1624-
}
1615+
if (w->persistent().IsEmpty())
1616+
continue;
1617+
argv[idx] = w->object();
1618+
if (++idx >= ARRAY_SIZE(argv)) {
1619+
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
1620+
idx = 0;
16251621
}
16261622
}
16271623

1628-
const size_t remainder = i % argc;
1629-
if (remainder > 0) {
1630-
HandleScope scope(env->isolate());
1631-
fn->Call(ctx, ary, remainder, argv).ToLocalChecked();
1624+
if (idx > 0) {
1625+
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
16321626
}
16331627

16341628
args.GetReturnValue().Set(ary);
@@ -1641,7 +1635,10 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
16411635
Environment* env = Environment::GetCurrent(args);
16421636

16431637
Local<Array> ary = Array::New(env->isolate());
1644-
int i = 0;
1638+
Local<Context> ctx = env->context();
1639+
Local<Function> fn = env->push_values_to_array_function();
1640+
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
1641+
size_t idx = 0;
16451642

16461643
Local<String> owner_sym = env->owner_string();
16471644

@@ -1652,7 +1649,14 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
16521649
Local<Value> owner = object->Get(owner_sym);
16531650
if (owner->IsUndefined())
16541651
owner = object;
1655-
ary->Set(i++, owner);
1652+
argv[idx] = owner;
1653+
if (++idx >= ARRAY_SIZE(argv)) {
1654+
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
1655+
idx = 0;
1656+
}
1657+
}
1658+
if (idx > 0) {
1659+
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
16561660
}
16571661

16581662
args.GetReturnValue().Set(ary);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
const NUM = 8;
7+
const connections = [];
8+
const clients = [];
9+
var clients_counter = 0;
10+
11+
const server = net.createServer(function listener(c) {
12+
connections.push(c);
13+
}).listen(common.PORT, function makeConnections() {
14+
for (var i = 0; i < NUM; i++) {
15+
net.connect(common.PORT, function connected() {
16+
clientConnected(this);
17+
});
18+
}
19+
});
20+
21+
22+
function clientConnected(client) {
23+
clients.push(client);
24+
if (++clients_counter >= NUM)
25+
checkAll();
26+
}
27+
28+
29+
function checkAll() {
30+
const handles = process._getActiveHandles();
31+
32+
clients.forEach(function(item) {
33+
assert.ok(handles.indexOf(item) > -1);
34+
item.destroy();
35+
});
36+
37+
connections.forEach(function(item) {
38+
assert.ok(handles.indexOf(item) > -1);
39+
item.end();
40+
});
41+
42+
assert.ok(handles.indexOf(server) > -1);
43+
server.close();
44+
}

0 commit comments

Comments
 (0)