TCPServer: reduced memory usage

pull/137/head
SChernykh 2 years ago
parent 2209e0cc70
commit 7ab21c6afd

@ -113,6 +113,8 @@ FORCEINLINE uint64_t udiv128(uint64_t hi, uint64_t lo, uint64_t divisor, uint64_
}
#endif
template<typename T> FORCEINLINE T round_up(T a, size_t granularity) { return static_cast<T>(((a + (granularity - static_cast<size_t>(1))) / granularity) * granularity); }
struct hash
{
alignas(8) uint8_t h[HASH_SIZE];

@ -801,7 +801,7 @@ void P2PServer::on_broadcast()
}
return p - p0;
});
});
}
}
}

@ -98,7 +98,7 @@ public:
{
Client* m_client;
uv_write_t m_write;
char m_data[WRITE_BUF_SIZE];
std::vector<uint8_t> m_data;
};
uv_mutex_t m_writeBuffersLock;

@ -548,10 +548,12 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
buf = new WriteBuf();
}
const size_t bytes_written = callback(buf->m_data);
// callback_buf is used in only 1 thread, so it's safe
static uint8_t callback_buf[WRITE_BUF_SIZE];
const size_t bytes_written = callback(callback_buf);
if (bytes_written > sizeof(buf->m_data)) {
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << sizeof(buf->m_data) << " bytes");
if (bytes_written > WRITE_BUF_SIZE) {
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << WRITE_BUF_SIZE << " bytes");
panic();
}
@ -566,9 +568,11 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
buf->m_client = client;
buf->m_write.data = buf;
buf->m_data.reserve(round_up(bytes_written, 64));
buf->m_data.assign(callback_buf, callback_buf + bytes_written);
uv_buf_t bufs[1];
bufs[0].base = buf->m_data;
bufs[0].base = reinterpret_cast<char*>(buf->m_data.data());
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);

Loading…
Cancel
Save