1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Reduce heap allocations with small_vector

This commit is contained in:
ζeh Matt
2024-12-11 21:27:08 +02:00
parent a16b493ae4
commit bc8e8f0360
2 changed files with 6 additions and 3 deletions

View File

@@ -17,6 +17,8 @@
#include "Socket.h"
#include "network.h"
#include <sfl/small_vector.hpp>
using namespace OpenRCT2;
static constexpr size_t kNetworkDisconnectReasonBufSize = 256;
@@ -101,7 +103,7 @@ NetworkReadPacket NetworkConnection::ReadPacket()
return NetworkReadPacket::MoreData;
}
static std::vector<uint8_t> serializePacket(const NetworkPacket& packet)
static sfl::small_vector<uint8_t, 512> 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<uint8_t> serializePacket(const NetworkPacket& packet)
header.Size = Convert::HostToNetwork(header.Size);
header.Id = ByteSwapBE(header.Id);
std::vector<uint8_t> buffer;
sfl::small_vector<uint8_t, 512> buffer;
buffer.reserve(sizeof(header) + packet.Data.size());
buffer.insert(buffer.end(), reinterpret_cast<uint8_t*>(&header), reinterpret_cast<uint8_t*>(&header) + sizeof(header));

View File

@@ -13,6 +13,7 @@
#include "NetworkTypes.h"
#include <memory>
#include <sfl/small_vector.hpp>
#include <vector>
#pragma pack(push, 1)
@@ -76,7 +77,7 @@ struct NetworkPacket final
public:
PacketHeader Header{};
std::vector<uint8_t> Data;
sfl::small_vector<uint8_t, 512> Data;
size_t BytesTransferred = 0;
size_t BytesRead = 0;
};