Skip to content

Commit 5e1f574

Browse files
committed
dgram: add fast api for get-send-queue-size method
1 parent 3c8c1ef commit 5e1f574

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const dgram = require('dgram');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [5e6],
8+
method: ['size', 'count'],
9+
});
10+
11+
function main({ n, method }) {
12+
const server = dgram.createSocket('udp4');
13+
const client = dgram.createSocket('udp4');
14+
15+
server.bind(0, () => {
16+
client.connect(server.address().port, () => {
17+
for (let i = 0; i < 999; i++) client.send('Hello');
18+
19+
bench.start();
20+
21+
if (method === 'size') {
22+
for (let i = 0; i < n; ++i) server.getSendQueueSize();
23+
} else {
24+
for (let i = 0; i < n; ++i) server.getSendQueueCount();
25+
}
26+
27+
bench.end(n);
28+
29+
client.close();
30+
server.close();
31+
});
32+
});
33+
}

src/udp_wrap.cc

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "permission/permission.h"
3030
#include "req_wrap-inl.h"
3131
#include "util-inl.h"
32+
#include "v8-fast-api-calls.h"
3233

3334
namespace node {
3435

@@ -38,6 +39,7 @@ using v8::ArrayBuffer;
3839
using v8::BackingStore;
3940
using v8::BackingStoreInitializationMode;
4041
using v8::Boolean;
42+
using v8::CFunction;
4143
using v8::Context;
4244
using v8::DontDelete;
4345
using v8::FunctionCallbackInfo;
@@ -48,6 +50,7 @@ using v8::Isolate;
4850
using v8::Local;
4951
using v8::MaybeLocal;
5052
using v8::Object;
53+
using v8::ObjectTemplate;
5154
using v8::PropertyAttribute;
5255
using v8::ReadOnly;
5356
using v8::Signature;
@@ -74,6 +77,11 @@ void SetLibuvInt32(const FunctionCallbackInfo<Value>& args) {
7477
}
7578
} // namespace
7679

80+
static CFunction fast_get_send_queue_size_(
81+
CFunction::Make(&UDPWrap::FastGetSendQueueSize));
82+
static CFunction fast_get_send_queue_count_(
83+
CFunction::Make(&UDPWrap::FastGetSendQueueCount));
84+
7785
class SendWrap : public ReqWrap<uv_udp_send_t> {
7886
public:
7987
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
@@ -215,9 +223,20 @@ void UDPWrap::Initialize(Local<Object> target,
215223
isolate, t, "setBroadcast", SetLibuvInt32<uv_udp_set_broadcast>);
216224
SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32<uv_udp_set_ttl>);
217225
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
218-
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
219-
SetProtoMethodNoSideEffect(
220-
isolate, t, "getSendQueueCount", GetSendQueueCount);
226+
227+
Local<ObjectTemplate> instance = t->InstanceTemplate();
228+
229+
SetFastMethodNoSideEffect(isolate,
230+
instance,
231+
"getSendQueueSize",
232+
GetSendQueueSize,
233+
&fast_get_send_queue_size_);
234+
235+
SetFastMethodNoSideEffect(isolate,
236+
instance,
237+
"getSendQueueCount",
238+
GetSendQueueCount,
239+
&fast_get_send_queue_count_);
221240

222241
t->Inherit(HandleWrap::GetConstructorTemplate(env));
223242

@@ -266,6 +285,8 @@ void UDPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
266285
registry->Register(BufferSize);
267286
registry->Register(GetSendQueueSize);
268287
registry->Register(GetSendQueueCount);
288+
registry->Register(fast_get_send_queue_size_);
289+
registry->Register(fast_get_send_queue_count_);
269290
}
270291

271292
void UDPWrap::New(const FunctionCallbackInfo<Value>& args) {
@@ -668,6 +689,19 @@ ReqWrap<uv_udp_send_t>* UDPWrap::CreateSendWrap(size_t msg_size) {
668689
return req_wrap;
669690
}
670691

692+
double UDPWrap::FastGetSendQueueSize(Local<Value> receiver) {
693+
UDPWrap* wrap = BaseObject::Unwrap<UDPWrap>(receiver.As<Object>());
694+
if (wrap == nullptr) return static_cast<double>(UV_EBADF);
695+
return static_cast<double>(
696+
uv_udp_get_send_queue_size(wrap->GetLibuvHandle()));
697+
}
698+
699+
double UDPWrap::FastGetSendQueueCount(Local<Value> receiver) {
700+
UDPWrap* wrap = BaseObject::Unwrap<UDPWrap>(receiver.As<Object>());
701+
if (wrap == nullptr) return static_cast<double>(UV_EBADF);
702+
return static_cast<double>(
703+
uv_udp_get_send_queue_count(wrap->GetLibuvHandle()));
704+
}
671705

672706
void UDPWrap::Send(const FunctionCallbackInfo<Value>& args) {
673707
DoSend(args, AF_INET);

src/udp_wrap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class UDPWrap final : public HandleWrap,
152152
static void GetSendQueueCount(
153153
const v8::FunctionCallbackInfo<v8::Value>& args);
154154

155+
static double FastGetSendQueueSize(v8::Local<v8::Value> receiver);
156+
static double FastGetSendQueueCount(v8::Local<v8::Value> receiver);
157+
155158
// UDPListener implementation
156159
uv_buf_t OnAlloc(size_t suggested_size) override;
157160
void OnRecv(ssize_t nread,

0 commit comments

Comments
 (0)