feat(Core/Optimization): Correctly document sendBuffer size and optimize it. (#18647)

* Fix comment documenting WorldSocket

The buffer is constructed with 4096 bytes but later resized in
WorldSocketThread::SocketAdded() according to the configuration
setting Network.OutUBuff (currently 65536 bytes)

* Reuse calculated packet size

Instead of recalculating the current packet size three times at
worst, calculate it once and reuse it when required.

* Reduce reserved buffer size per WorldSocket

Don't reserve 64kB of memory for every WorldSocket's output
buffer.

Instead, start with a 4kB baseline for every WorldSocket and grow
the buffer size dynamically when we have single packets that do
not fit the current buffer.
This commit is contained in:
Johaine
2024-04-06 15:22:32 +02:00
committed by GitHub
parent c80ad3d779
commit 3a93ae1af1
2 changed files with 19 additions and 12 deletions

View File

@@ -165,6 +165,7 @@ bool WorldSocket::Update()
{
// Allocate buffer only when it's needed but not on every Update() call.
MessageBuffer buffer(_sendBufferSize);
std::size_t currentPacketSize;
do
{
queued->CompressIfNeeded();
@@ -172,26 +173,32 @@ bool WorldSocket::Update()
if (queued->NeedsEncryption())
_authCrypt.EncryptSend(header.header, header.getHeaderLength());
if (buffer.GetRemainingSpace() < queued->size() + header.getHeaderLength())
currentPacketSize = queued->size() + header.getHeaderLength();
if (buffer.GetRemainingSpace() < currentPacketSize)
{
QueuePacket(std::move(buffer));
buffer.Resize(_sendBufferSize);
}
if (buffer.GetRemainingSpace() >= queued->size() + header.getHeaderLength())
if (buffer.GetRemainingSpace() >= currentPacketSize)
{
buffer.Write(header.header, header.getHeaderLength());
if (!queued->empty())
buffer.Write(queued->contents(), queued->size());
}
else // single packet larger than 4096 bytes
else // Single packet larger than current buffer size
{
MessageBuffer packetBuffer(queued->size() + header.getHeaderLength());
packetBuffer.Write(header.header, header.getHeaderLength());
if (!queued->empty())
packetBuffer.Write(queued->contents(), queued->size());
// Resize buffer to fit current packet
buffer.Resize(currentPacketSize);
QueuePacket(std::move(packetBuffer));
// Grow future buffers to current packet size if still below limit
if (currentPacketSize <= 65536)
_sendBufferSize = currentPacketSize;
buffer.Write(header.header, header.getHeaderLength());
if (!queued->empty())
buffer.Write(queued->contents(), queued->size());
}
delete queued;