mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
perf(gameplay): optimize protobuf message allocation with Arena and limit thread pool size
Switch to Arena allocation for protobuf messages to reduce heap allocations. Pass ChunkDataRsp by value to enable move semantics. Cap thread pool size to hardware concurrency with max 4 threads.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
using namespace google::protobuf;
|
||||
namespace Cubed {
|
||||
NetworkClient::NetworkClient(ClientWorld& world)
|
||||
: m_socket(m_io), m_strand(asio::make_strand(m_io)), m_world(world) {}
|
||||
@@ -62,13 +63,14 @@ asio::awaitable<void> NetworkClient::read_loop() {
|
||||
}
|
||||
|
||||
constexpr auto& to_num = std::to_underlying<PacketEnum>;
|
||||
Arena arena;
|
||||
switch (header.cmd) {
|
||||
case to_num(PacketEnum::LOGIN_RSP): {
|
||||
LoginRsp rsp;
|
||||
auto* rsp = Arena::Create<LoginRsp>(&arena);
|
||||
Logger::info("Client: Receive Login rsp");
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
if (rsp.success()) {
|
||||
m_world.start_client_thread(rsp.uuid());
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
if (rsp->success()) {
|
||||
m_world.start_client_thread(rsp->uuid());
|
||||
} else {
|
||||
Logger::error("Connected Server Fail");
|
||||
}
|
||||
@@ -79,32 +81,32 @@ asio::awaitable<void> NetworkClient::read_loop() {
|
||||
Logger::info("Client: Receive Chunk Data rsp, size {}mb",
|
||||
body_data.size() / 1024.0f / 1024);
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
m_world.receive_chunk(rsp);
|
||||
m_world.receive_chunk(std::move(rsp));
|
||||
}
|
||||
} break;
|
||||
case to_num(PacketEnum::BLOCK_CHANGE_RSP): {
|
||||
BlockChangeRsp rsp;
|
||||
auto* rsp = Arena::Create<BlockChangeRsp>(&arena);
|
||||
Logger::info("Client: Receive Block Change rsp");
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
m_world.receive_block_change(rsp);
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
m_world.receive_block_change(*rsp);
|
||||
}
|
||||
} break;
|
||||
case to_num(PacketEnum::UPDATE_TIME): {
|
||||
UpdateTime rsp;
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
m_world.receive_time(rsp);
|
||||
auto* rsp = Arena::Create<UpdateTime>(&arena);
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
m_world.receive_time(*rsp);
|
||||
}
|
||||
} break;
|
||||
case to_num(PacketEnum::PLAYER_INFO_RSP): {
|
||||
PlayerInfoRsp rsp;
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
m_world.receive_other_player(rsp);
|
||||
auto* rsp = Arena::Create<PlayerInfoRsp>(&arena);
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
m_world.receive_other_player(*rsp);
|
||||
}
|
||||
} break;
|
||||
case to_num(PacketEnum::LOGOUT_RSP): {
|
||||
LogoutRsp rsp;
|
||||
if (decode_packet(rsp, body_data, header)) {
|
||||
m_world.receive_player_logout(rsp);
|
||||
auto* rsp = Arena::Create<LogoutRsp>(&arena);
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
m_world.receive_player_logout(*rsp);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user