Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

β€Žsrc/quic/bindingdata.ccβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace node {
2222
using mem::kReserveSizeAndAlign;
2323
using v8::Function;
2424
using v8::FunctionTemplate;
25+
using v8::HandleScope;
2526
using v8::Local;
2627
using v8::Object;
2728
using v8::String;
@@ -154,6 +155,16 @@ BindingData& BindingData::Get(Environment* env) {
154155

155156
BindingData::~BindingData() {
156157
quic_alloc_state.binding = nullptr;
158+
if (flush_check_initialized_) {
159+
uv_check_stop(&flush_check_);
160+
flush_check_started_ = false;
161+
// The check handle is closed inline here. Because BindingData destruction
162+
// happens during Environment cleanup, the handle will be finalized by
163+
// libuv's close phase.
164+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_check_), nullptr);
165+
flush_check_initialized_ = false;
166+
}
167+
pending_flush_sessions_.clear();
157168
}
158169

159170
ngtcp2_mem* BindingData::ngtcp2_allocator() {
@@ -221,6 +232,11 @@ void BindingData::RegisterExternalReferences(
221232
BindingData::BindingData(Realm* realm, Local<Object> object)
222233
: BaseObject(realm, object) {
223234
MakeWeak();
235+
CHECK_EQ(uv_check_init(env()->event_loop(), &flush_check_), 0);
236+
flush_check_.data = this;
237+
// Unref so the check handle doesn't keep the event loop alive on its own.
238+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_check_));
239+
flush_check_initialized_ = true;
224240
}
225241

226242
SessionManager& BindingData::session_manager() {
@@ -230,6 +246,45 @@ SessionManager& BindingData::session_manager() {
230246
return *session_manager_;
231247
}
232248

249+
voidBindingData::ScheduleSessionFlush(const BaseObjectPtr<Session>& session) {
250+
pending_flush_sessions_.push_back(session);
251+
if (!flush_check_started_) {
252+
uv_check_start(&flush_check_, OnFlushCheck);
253+
flush_check_started_ = true;
254+
}
255+
}
256+
257+
voidBindingData::OnFlushCheck(uv_check_t* handle) {
258+
auto* binding = static_cast<BindingData*>(handle->data);
259+
if (binding->pending_flush_sessions_.empty()) {
260+
uv_check_stop(&binding->flush_check_);
261+
binding->flush_check_started_ = false;
262+
return;
263+
}
264+
265+
HandleScope scope(binding->env()->isolate());
266+
267+
// Swap to a local vector before iterating. SendPendingData may trigger
268+
// MakeCallback which runs JS that could cause more packet receives via
269+
// re-entry (e.g., a stream data callback that synchronously writes to
270+
// another session). Any sessions added during the flush remain in
271+
// pending_flush_sessions_ and are picked up on the next check tick.
272+
auto sessions = std::move(binding->pending_flush_sessions_);
273+
for (auto& session : sessions) {
274+
session->pending_flush_ = false;
275+
if (!session->is_destroyed()) {
276+
session->FlushPendingData();
277+
}
278+
}
279+
280+
// If no new sessions were added during the flush, stop the check
281+
// to avoid per-tick callback overhead when idle.
282+
if (binding->pending_flush_sessions_.empty()) {
283+
uv_check_stop(&binding->flush_check_);
284+
binding->flush_check_started_ = false;
285+
}
286+
}
287+
233288
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
234289
#defineV(name, _) tracker->TrackField(#name, name##_callback());
235290

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include<ngtcp2/ngtcp2_crypto.h>
1111
#include<node.h>
1212
#include<node_mem.h>
13+
#include<uv.h>
1314
#include<v8.h>
1415
#include<memory>
1516
#include<unordered_map>
17+
#include<vector>
1618
#include"defs.h"
1719

1820
namespacenode::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
voidScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
staticvoidOnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

β€Žsrc/quic/endpoint.ccβ€Ž

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
intEndpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) returnUV_EBADF;
498+
uv_buf_t buf = *packet;
499+
returnuv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
voidEndpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
voidEndpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately β€” no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path β€” the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
returnSend(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
voidEndpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
constauto accept = [&](const Session::Config& config, Store&& store) {

β€Žsrc/quic/endpoint.hβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
voidSend(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
voidSendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
voidClose();
302309
intSend(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
intTrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
returnReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
boolSession::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
returnfalse;
22542264
}
22552265

2266+
voidSession::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
voidSession::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

β€Žsrc/quic/session.hβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
357357
const SocketAddress& local_address,
358358
const SocketAddress& remote_address);
359359

360+
// ReadPacket processes a single inbound packet through ngtcp2 without
361+
// triggering SendPendingData. This is the building block for batched
362+
// receive processing: the caller (Endpoint::Receive) accumulates
363+
// dirty sessions and a uv_check callback flushes them after all
364+
// packets in the I/O burst have been read.
365+
// Receive() is kept as a convenience wrapper that calls ReadPacket()
366+
// then triggers SendPendingData (for paths like Connect that need
367+
// immediate response).
368+
boolReadPacket(Store&& store,
369+
const SocketAddress& local_address,
370+
const SocketAddress& remote_address);
371+
372+
// Called by BindingData's flush callback to trigger SendPendingData
373+
// on this session. Encapsulates the application() access so that
374+
// bindingdata.cc doesn't need the full Application type definition.
375+
voidFlushPendingData();
376+
360377
voidSend(Packet::Ptr packet);
361378
voidSend(Packet::Ptr packet, const PathStorage& path);
362379
datagram_id SendDatagram(Store&& data);
@@ -572,11 +589,22 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
572589
bool in_ngtcp2_callback_scope_ = false;
573590
bool in_nghttp3_callback_scope_ = false;
574591
bool destroy_deferred_ = false;
592+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
593+
// Cleared by the flush callback before calling SendPendingData.
594+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
595+
// burst is only scheduled for flush once.
596+
bool pending_flush_ = false;
597+
// When true, Session::Send prefers synchronous delivery via
598+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
599+
// Set during FlushPendingData to avoid the one-tick latency of
600+
// async-only sends from the uv_check callback.
601+
bool prefer_try_send_ = false;
575602
QuicConnectionPointer connection_;
576603
std::unique_ptr<TLSSession> tls_session_;
577604
friendstructNgTcp2CallbackScope;
578605
friendstructNgHttp3CallbackScope;
579606
friendclassApplication;
607+
friendclassBindingData;
580608
friendclassDefaultApplication;
581609
friendclassHttp3ApplicationImpl;
582610
friendclassEndpoint;

0 commit comments

Comments
Β (0)