Skip to content

dgram: improve get-send-queue-info performance #59054

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions benchmark/dgram/get-send-queue-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const common = require('../common.js');
const dgram = require('dgram');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [5e6],
method: ['size', 'count'],
}, {
flags: ['--test-udp-no-try-send'],
});

function main({ n, method }) {
const server = dgram.createSocket('udp4');
const client = dgram.createSocket('udp4');

server.bind(0, () => {
client.connect(server.address().port, () => {
// warm-up: keep packets unsent using --test-udp-no-try-send flag
for (let i = 0; i < 999; i++) client.send('Hello');

let size = 0;
let count = 0;

bench.start();

if (method === 'size') {
for (let i = 0; i < n; ++i) size += client.getSendQueueSize();
} else {
for (let i = 0; i < n; ++i) count += client.getSendQueueCount();
}

bench.end(n);

client.close();
server.close();

assert.ok(size >= 0);
assert.ok(count >= 0);
});
});
}
40 changes: 37 additions & 3 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "permission/permission.h"
#include "req_wrap-inl.h"
#include "util-inl.h"
#include "v8-fast-api-calls.h"

namespace node {

Expand All @@ -38,6 +39,7 @@ using v8::ArrayBuffer;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Boolean;
using v8::CFunction;
using v8::Context;
using v8::DontDelete;
using v8::FunctionCallbackInfo;
Expand All @@ -48,6 +50,7 @@ using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::ObjectTemplate;
using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::Signature;
Expand All @@ -74,6 +77,11 @@ void SetLibuvInt32(const FunctionCallbackInfo<Value>& args) {
}
} // namespace

static CFunction fast_get_send_queue_size_(
CFunction::Make(&UDPWrap::FastGetSendQueueSize));
static CFunction fast_get_send_queue_count_(
CFunction::Make(&UDPWrap::FastGetSendQueueCount));

class SendWrap : public ReqWrap<uv_udp_send_t> {
public:
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
Expand Down Expand Up @@ -215,9 +223,20 @@ void UDPWrap::Initialize(Local<Object> target,
isolate, t, "setBroadcast", SetLibuvInt32<uv_udp_set_broadcast>);
SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32<uv_udp_set_ttl>);
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
SetProtoMethodNoSideEffect(
isolate, t, "getSendQueueCount", GetSendQueueCount);

Local<ObjectTemplate> instance = t->InstanceTemplate();

SetFastMethodNoSideEffect(isolate,
instance,
"getSendQueueSize",
GetSendQueueSize,
&fast_get_send_queue_size_);

SetFastMethodNoSideEffect(isolate,
instance,
"getSendQueueCount",
GetSendQueueCount,
&fast_get_send_queue_count_);

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

Expand Down Expand Up @@ -266,6 +285,8 @@ void UDPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(BufferSize);
registry->Register(GetSendQueueSize);
registry->Register(GetSendQueueCount);
registry->Register(fast_get_send_queue_size_);
registry->Register(fast_get_send_queue_count_);
}

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

double UDPWrap::FastGetSendQueueSize(Local<Value> receiver) {
UDPWrap* wrap = BaseObject::Unwrap<UDPWrap>(receiver.As<Object>());
if (wrap == nullptr) return static_cast<double>(UV_EBADF);
return static_cast<double>(
uv_udp_get_send_queue_size(wrap->GetLibuvHandle()));
}

double UDPWrap::FastGetSendQueueCount(Local<Value> receiver) {
UDPWrap* wrap = BaseObject::Unwrap<UDPWrap>(receiver.As<Object>());
if (wrap == nullptr) return static_cast<double>(UV_EBADF);
return static_cast<double>(
uv_udp_get_send_queue_count(wrap->GetLibuvHandle()));
}

void UDPWrap::Send(const FunctionCallbackInfo<Value>& args) {
DoSend(args, AF_INET);
Expand Down
3 changes: 3 additions & 0 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class UDPWrap final : public HandleWrap,
static void GetSendQueueCount(
const v8::FunctionCallbackInfo<v8::Value>& args);

static double FastGetSendQueueSize(v8::Local<v8::Value> receiver);
static double FastGetSendQueueCount(v8::Local<v8::Value> receiver);

// UDPListener implementation
uv_buf_t OnAlloc(size_t suggested_size) override;
void OnRecv(ssize_t nread,
Expand Down
Loading