perf(gameplay): batch entity updates into single packet

Aggregate per-entity update messages into S2CEntityUpdateBatch and
broadcast once per tick. Reuse serialized packets in several broadcast
loops to avoid repeated make_packet calls.
This commit is contained in:
2026-08-06 16:48:18 +08:00
parent 3534acd90f
commit c6a75e709e
8 changed files with 100 additions and 64 deletions

View File

@@ -19,8 +19,8 @@ public:
void receive_entity_create(S2CEntityCreate& msg);
void receive_entity_destory(EntityID id);
void receive_entity_update(S2CEntityUpdate& msg);
void receive_entity_update(const S2CEntityUpdate& msg);
void receive_entity_update(S2CEntityUpdateBatch& msg);
void destory(EntityID id);
void create(std::string_view name, const glm::vec3& pos);

View File

@@ -68,6 +68,7 @@ enum class PacketEnum : uint16_t {
C2S_ENTITY_CREATE_REQUEST = 3009,
C2S_ENTITY_DESTORY_REQUEST = 3010,
S2C_ENTITY_UPDATE = 3011,
S2C_ENTITY_UPDATE_BATCH = 3012,
CHAT_MSG = 4001,
VOICE_MSG = 4002,
@@ -153,6 +154,9 @@ template <> constexpr uint16_t get_packet_id<VoiceMsg>() {
template <> constexpr uint16_t get_packet_id<S2CEntityUpdate>() {
return std::to_underlying(PacketEnum::S2C_ENTITY_UPDATE);
}
template <> constexpr uint16_t get_packet_id<S2CEntityUpdateBatch>() {
return std::to_underlying(PacketEnum::S2C_ENTITY_UPDATE_BATCH);
}
template <typename T>
requires std::derived_from<T, google::protobuf::Message>

View File

@@ -1,5 +1,6 @@
#pragma once
#include "Cubed/gameplay/ecs/entity.hpp"
#include "Cubed/gameplay/gait.hpp"
#include "glm/ext/vector_float3.hpp"
#include <entt/entt.hpp>
@@ -26,6 +27,14 @@ private:
std::string name;
glm::vec3 pos;
};
struct EntitySendData {
EntityID id;
glm::vec3 pos;
glm::vec3 dir;
Gait gait;
};
using EntityMap = tbb::concurrent_hash_map<EntityID, entt::entity>;
using acc = EntityMap::accessor;
using cacc = EntityMap::const_accessor;
@@ -47,9 +56,8 @@ private:
void send_all_entities(std::shared_ptr<Session>& session);
void update_ai(entt::entity e);
void update_move(entt::entity e);
void
update_send(entt::entity e,
tbb::concurrent_vector<std::shared_ptr<Session>>& sessions);
void update_send(entt::entity e,
tbb::concurrent_vector<EntitySendData>& sessions);
template <typename... Args>
EntityID create_entity_in_factory(Args&&... args) {
auto entity = m_registry.create();