TCPServer: optimized internal write buffers

This commit is contained in:
SChernykh 2023-01-20 17:03:59 +01:00
parent 595196b5ec
commit f5ac485ac8
3 changed files with 18 additions and 7 deletions

View file

@ -758,7 +758,7 @@ void StratumServer::on_blobs_ready()
} }
const bool result = send(client, const bool result = send(client,
[data, target, hashing_blob, &job_id](void* buf, size_t buf_size) [data, target, hashing_blob, job_id](void* buf, size_t buf_size)
{ {
log::hex_buf target_hex(reinterpret_cast<const uint8_t*>(&target), sizeof(uint64_t)); log::hex_buf target_hex(reinterpret_cast<const uint8_t*>(&target), sizeof(uint64_t));

View file

@ -102,9 +102,10 @@ public:
struct WriteBuf struct WriteBuf
{ {
Client* m_client = nullptr;
uv_write_t m_write = {}; uv_write_t m_write = {};
std::vector<uint8_t> m_data; Client* m_client = nullptr;
void* m_data = nullptr;
size_t m_dataCapacity = 0;
}; };
std::vector<WriteBuf*> m_writeBuffers; std::vector<WriteBuf*> m_writeBuffers;

View file

@ -518,13 +518,22 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
return true; return true;
} }
buf->m_client = client;
buf->m_write.data = buf; buf->m_write.data = buf;
buf->m_data.reserve(round_up(bytes_written, 64)); buf->m_client = client;
buf->m_data.assign(callback_buf, callback_buf + bytes_written);
if (buf->m_dataCapacity < bytes_written) {
buf->m_dataCapacity = round_up(bytes_written, 64);
buf->m_data = realloc_hook(buf->m_data, buf->m_dataCapacity);
if (!buf->m_data) {
LOGERR(0, "failed to allocate " << buf->m_dataCapacity << " bytes to send data");
PANIC_STOP();
}
}
memcpy(buf->m_data, callback_buf, bytes_written);
uv_buf_t bufs[1]; uv_buf_t bufs[1];
bufs[0].base = reinterpret_cast<char*>(buf->m_data.data()); bufs[0].base = reinterpret_cast<char*>(buf->m_data);
bufs[0].len = static_cast<int>(bytes_written); bufs[0].len = static_cast<int>(bytes_written);
const int err = uv_write(&buf->m_write, reinterpret_cast<uv_stream_t*>(&client->m_socket), bufs, 1, Client::on_write); const int err = uv_write(&buf->m_write, reinterpret_cast<uv_stream_t*>(&client->m_socket), bufs, 1, Client::on_write);
@ -562,6 +571,7 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::loop(void* data)
} }
for (WriteBuf* buf : server->m_writeBuffers) { for (WriteBuf* buf : server->m_writeBuffers) {
free_hook(buf->m_data);
delete buf; delete buf;
} }
server->m_writeBuffers.clear(); server->m_writeBuffers.clear();