mirror of
https://github.com/SChernykh/p2pool.git
synced 2024-08-20 05:28:47 -06:00
TCPServer: reduced memory usage
This commit is contained in:
parent
2209e0cc70
commit
7ab21c6afd
@ -113,6 +113,8 @@ FORCEINLINE uint64_t udiv128(uint64_t hi, uint64_t lo, uint64_t divisor, uint64_
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
struct hash
|
||||||
{
|
{
|
||||||
alignas(8) uint8_t h[HASH_SIZE];
|
alignas(8) uint8_t h[HASH_SIZE];
|
||||||
|
@ -98,7 +98,7 @@ public:
|
|||||||
{
|
{
|
||||||
Client* m_client;
|
Client* m_client;
|
||||||
uv_write_t m_write;
|
uv_write_t m_write;
|
||||||
char m_data[WRITE_BUF_SIZE];
|
std::vector<uint8_t> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
uv_mutex_t m_writeBuffersLock;
|
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();
|
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)) {
|
if (bytes_written > WRITE_BUF_SIZE) {
|
||||||
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << sizeof(buf->m_data) << " bytes");
|
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << WRITE_BUF_SIZE << " bytes");
|
||||||
panic();
|
panic();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -566,9 +568,11 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
|
|||||||
|
|
||||||
buf->m_client = client;
|
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_data.assign(callback_buf, callback_buf + bytes_written);
|
||||||
|
|
||||||
uv_buf_t bufs[1];
|
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);
|
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user