From bc8e8f0360857d9d71b095c790645bd5680be474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:27:08 +0200 Subject: [PATCH] Reduce heap allocations with small_vector --- src/openrct2/network/NetworkConnection.cpp | 6 ++++-- src/openrct2/network/NetworkPacket.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/openrct2/network/NetworkConnection.cpp b/src/openrct2/network/NetworkConnection.cpp index bcaf80a386..853d8e4999 100644 --- a/src/openrct2/network/NetworkConnection.cpp +++ b/src/openrct2/network/NetworkConnection.cpp @@ -17,6 +17,8 @@ #include "Socket.h" #include "network.h" + #include + using namespace OpenRCT2; static constexpr size_t kNetworkDisconnectReasonBufSize = 256; @@ -101,7 +103,7 @@ NetworkReadPacket NetworkConnection::ReadPacket() return NetworkReadPacket::MoreData; } -static std::vector serializePacket(const NetworkPacket& packet) +static sfl::small_vector serializePacket(const NetworkPacket& packet) { // NOTE: For compatibility reasons for the master server we need to add sizeof(Header.Id) to the size. // Previously the Id field was not part of the header rather part of the body. @@ -114,7 +116,7 @@ static std::vector serializePacket(const NetworkPacket& packet) header.Size = Convert::HostToNetwork(header.Size); header.Id = ByteSwapBE(header.Id); - std::vector buffer; + sfl::small_vector buffer; buffer.reserve(sizeof(header) + packet.Data.size()); buffer.insert(buffer.end(), reinterpret_cast(&header), reinterpret_cast(&header) + sizeof(header)); diff --git a/src/openrct2/network/NetworkPacket.h b/src/openrct2/network/NetworkPacket.h index 20f4a7cd87..a262fc1761 100644 --- a/src/openrct2/network/NetworkPacket.h +++ b/src/openrct2/network/NetworkPacket.h @@ -13,6 +13,7 @@ #include "NetworkTypes.h" #include +#include #include #pragma pack(push, 1) @@ -76,7 +77,7 @@ struct NetworkPacket final public: PacketHeader Header{}; - std::vector Data; + sfl::small_vector Data; size_t BytesTransferred = 0; size_t BytesRead = 0; };