Skip to content

Commit 46165da

Browse files
committed
sv2: optimistic send for hot paths
Expose TryOptimisticSend so TemplateProvider can flush send queues immediately after queuing responses and work updates. This reduces the latency for frequent message flows while leaving the infrequent connection-setup messages untouched. Assisted-by: GitHub Copilot Assisted-by: OpenAI GPT-5-Codex
1 parent 6b340ca commit 46165da

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/sv2/connman.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ std::pair<size_t, bool> Sv2Connman::SendMessagesAsBytes(Sv2Client& client)
174174
return {total_sent, expected_more.value_or(false)};
175175
}
176176

177+
void Sv2Connman::TryOptimisticSend(Sv2Client& client)
178+
{
179+
AssertLockHeld(client.cs_send);
180+
SendMessagesAsBytes(client);
181+
}
182+
177183
void Sv2Connman::EventReadyToSend(NodeId node_id, bool& cancel_recv)
178184
{
179185
AssertLockNotHeld(m_clients_mutex);

src/sv2/connman.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ class Sv2Connman : SockMan
216216
*/
217217
void ProcessSv2Message(const node::Sv2NetMsg& sv2_header, Sv2Client& client);
218218

219+
/**
220+
* Attempt to flush the send queue immediately, without waiting for the
221+
* socket layer to signal readiness. Falls back to the regular send path if
222+
* the socket would block.
223+
*/
224+
void TryOptimisticSend(Sv2Client& client) EXCLUSIVE_LOCKS_REQUIRED(client.cs_send);
225+
219226
std::shared_ptr<Sv2Client> GetClientById(NodeId node_id) const EXCLUSIVE_LOCKS_REQUIRED(m_clients_mutex);
220227

221228
using Sv2ClientFn = std::function<void(Sv2Client&)>;

src/sv2/template_provider.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ void Sv2TemplateProvider::RequestTransactionData(Sv2Client& client, node::Sv2Req
437437
client.m_id);
438438
LOCK(client.cs_send);
439439
client.m_send_messages.emplace_back(request_tx_data_success);
440+
m_connman->TryOptimisticSend(client);
440441
}
441442

442443
void Sv2TemplateProvider::SubmitSolution(node::Sv2SubmitSolutionMsg solution)
@@ -561,8 +562,6 @@ bool Sv2TemplateProvider::SendWork(Sv2Client& client, uint64_t template_id, Bloc
561562
template_id,
562563
future_template};
563564

564-
// TODO: use optimistic send instead of adding to the queue
565-
566565
LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x71 NewTemplate id=%lu future=%d to client id=%zu\n", template_id, future_template, client.m_id);
567566
{
568567
LOCK(client.cs_send);
@@ -573,6 +572,8 @@ bool Sv2TemplateProvider::SendWork(Sv2Client& client, uint64_t template_id, Bloc
573572
LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x72 SetNewPrevHash to client id=%zu\n", client.m_id);
574573
client.m_send_messages.emplace_back(new_prev_hash);
575574
}
575+
576+
m_connman->TryOptimisticSend(client);
576577
}
577578

578579
CAmount total_fees{0};

0 commit comments

Comments
 (0)