Optimize performance

Introduces a generic, fixed-size `Bucket` type in place of the inner
vector. This does add size generics but I think it's a very good
tradeoff considering how performance-sensitive this codepath is.
This commit is contained in:
Gent Semaj 2024-10-28 12:56:20 -07:00
parent b988724937
commit feaca861d5
Signed by untrusted user: ycc
GPG Key ID: 2D76C57BF6BEADC4
3 changed files with 97 additions and 57 deletions

View File

@ -115,6 +115,7 @@ CXXHDR=\
src/settings.hpp\ src/settings.hpp\
src/Transport.hpp\ src/Transport.hpp\
src/TableData.hpp\ src/TableData.hpp\
src/Bucket.hpp\
src/Chunking.hpp\ src/Chunking.hpp\
src/Buddies.hpp\ src/Buddies.hpp\
src/Groups.hpp\ src/Groups.hpp\

39
src/Bucket.hpp Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <array>
#include <optional>
template<class T, size_t N>
class Bucket {
std::array<T, N> buf;
size_t sz;
public:
Bucket() {
sz = 0;
}
void add(const T& item) {
if (sz < N) {
buf[sz++] = item;
}
}
std::optional<T> get(size_t idx) const {
if (idx < sz) {
return buf[idx];
}
return std::nullopt;
}
size_t size() const {
return sz;
}
bool isFull() const {
return sz == N;
}
void clear() {
sz = 0;
}
};

View File

@ -3,6 +3,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "MobAI.hpp" #include "MobAI.hpp"
#include "NPCManager.hpp" #include "NPCManager.hpp"
#include "Bucket.hpp"
#include <assert.h> #include <assert.h>
@ -12,11 +13,11 @@ using namespace Chunking;
* The initial chunkPos value before a player is placed into the world. * The initial chunkPos value before a player is placed into the world.
*/ */
const ChunkPos Chunking::INVALID_CHUNK = {}; const ChunkPos Chunking::INVALID_CHUNK = {};
constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sPCAppearanceData); constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sPCAppearanceData);
constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sNPCAppearanceData); constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sNPCAppearanceData);
constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sShinyAppearanceData); constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sShinyAppearanceData);
constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sTransportationAppearanceData); constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sTransportationAppearanceData);
constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(int32_t); constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(int32_t);
std::map<ChunkPos, Chunk*> Chunking::chunks; std::map<ChunkPos, Chunk*> Chunking::chunks;
@ -81,36 +82,32 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
deleteChunk(chunkPos); deleteChunk(chunkPos);
} }
template<class T> template<class T, size_t N>
static void sendAroundPacket(const EntityRef recipient, std::vector<std::vector<T>>& slices, size_t maxCnt, uint32_t packetId) { static void sendAroundPacket(const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
assert(recipient.kind == EntityKind::PLAYER); assert(recipient.kind == EntityKind::PLAYER);
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE]; uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
for (const auto& slice : slices) { for (const auto& bucket : buckets) {
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE); memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
int count = slice.size(); int count = bucket.size();
assert(count <= maxCnt);
*((int32_t*)pktBuf) = count; *((int32_t*)pktBuf) = count;
T* data = (T*)(pktBuf + sizeof(int32_t)); T* data = (T*)(pktBuf + sizeof(int32_t));
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
data[i] = slice[i]; data[i] = bucket.get(i).value();
} }
recipient.sock->sendPacket(pktBuf, packetId, sizeof(int32_t) + (count * sizeof(T))); recipient.sock->sendPacket(pktBuf, packetId, sizeof(int32_t) + (count * sizeof(T)));
} }
} }
static void sendAroundDelPacket(const EntityRef recipient, std::vector<std::vector<int32_t>>& slices, bool isTransportation, uint32_t packetId) { template<size_t N>
static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<int32_t, N>>& buckets, bool isTransportation, uint32_t packetId) {
assert(recipient.kind == EntityKind::PLAYER); assert(recipient.kind == EntityKind::PLAYER);
size_t maxCnt = MAX_IDS_PER_AROUND_DEL;
if (isTransportation)
maxCnt -= 1; // account for eTT. sad.
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE]; uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
for (const auto& slice : slices) { for (const auto& bucket : buckets) {
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE); memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
int count = slice.size(); int count = bucket.size();
assert(count <= maxCnt); assert(count <= N);
size_t baseSize; size_t baseSize;
if (isTransportation) { if (isTransportation) {
@ -125,39 +122,42 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<std::vect
int32_t* ids = (int32_t*)(pktBuf + baseSize); int32_t* ids = (int32_t*)(pktBuf + baseSize);
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
ids[i] = slice[i]; ids[i] = bucket.get(i).value();
} }
recipient.sock->sendPacket(pktBuf, packetId, baseSize + (count * sizeof(int32_t))); recipient.sock->sendPacket(pktBuf, packetId, baseSize + (count * sizeof(int32_t)));
} }
} }
template<class T> template<class T, size_t N>
static void bufferAppearanceData(std::vector<std::vector<T>>& slices, const T& data, size_t maxCnt) { static void bufferAppearanceData(std::vector<Bucket<T, N>>& buckets, const T& data) {
if (slices.empty()) if (buckets.empty())
slices.push_back(std::vector<T>()); buckets.push_back(Bucket<T, N>());
std::vector<T>& slice = slices[slices.size() - 1]; Bucket<T, N>& bucket = buckets[buckets.size() - 1];
slice.push_back(data); assert(!bucket.isFull());
if (slice.size() == maxCnt) bucket.add(data);
slices.push_back(std::vector<T>()); if (bucket.isFull())
buckets.push_back(Bucket<T, N>());
} }
static void bufferIdForDisappearance(std::vector<std::vector<int32_t>>& slices, int32_t id, size_t maxCnt) { template<size_t N>
if (slices.empty()) static void bufferIdForDisappearance(std::vector<Bucket<int32_t, N>>& buckets, int32_t id) {
slices.push_back(std::vector<int32_t>()); if (buckets.empty())
std::vector<int32_t>& slice = slices[slices.size() - 1]; buckets.push_back(Bucket<int32_t, N>());
slice.push_back(id); Bucket<int32_t, N>& bucket = buckets[buckets.size() - 1];
if (slice.size() == maxCnt) assert(!bucket.isFull());
slices.push_back(std::vector<int32_t>()); bucket.add(id);
if (bucket.isFull())
buckets.push_back(Bucket<int32_t, N>());
} }
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) { void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
Entity *ent = ref.getEntity(); Entity *ent = ref.getEntity();
bool alive = ent->isExtant(); bool alive = ent->isExtant();
std::vector<std::vector<sPCAppearanceData>> pcAppearances; std::vector<Bucket<sPCAppearanceData, MAX_PC_PER_AROUND>> pcAppearances;
std::vector<std::vector<sNPCAppearanceData>> npcAppearances; std::vector<Bucket<sNPCAppearanceData, MAX_NPC_PER_AROUND>> npcAppearances;
std::vector<std::vector<sShinyAppearanceData>> shinyAppearances; std::vector<Bucket<sShinyAppearanceData, MAX_SHINY_PER_AROUND>> shinyAppearances;
std::vector<std::vector<sTransportationAppearanceData>> transportationAppearances; std::vector<Bucket<sTransportationAppearanceData, MAX_TRANSPORTATION_PER_AROUND>> transportationAppearances;
for (Chunk *chunk : chnks) { for (Chunk *chunk : chnks) {
for (const EntityRef otherRef : chunk->entities) { for (const EntityRef otherRef : chunk->entities) {
// skip oneself // skip oneself
@ -181,27 +181,27 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
{ {
case EntityKind::PLAYER: case EntityKind::PLAYER:
pcData = dynamic_cast<Player*>(other)->getAppearanceData(); pcData = dynamic_cast<Player*>(other)->getAppearanceData();
bufferAppearanceData(pcAppearances, pcData, MAX_PC_PER_AROUND); bufferAppearanceData(pcAppearances, pcData);
break; break;
case EntityKind::SIMPLE_NPC: case EntityKind::SIMPLE_NPC:
npcData = dynamic_cast<BaseNPC*>(other)->getAppearanceData(); npcData = dynamic_cast<BaseNPC*>(other)->getAppearanceData();
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND); bufferAppearanceData(npcAppearances, npcData);
break; break;
case EntityKind::COMBAT_NPC: case EntityKind::COMBAT_NPC:
npcData = dynamic_cast<CombatNPC*>(other)->getAppearanceData(); npcData = dynamic_cast<CombatNPC*>(other)->getAppearanceData();
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND); bufferAppearanceData(npcAppearances, npcData);
break; break;
case EntityKind::MOB: case EntityKind::MOB:
npcData = dynamic_cast<Mob*>(other)->getAppearanceData(); npcData = dynamic_cast<Mob*>(other)->getAppearanceData();
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND); bufferAppearanceData(npcAppearances, npcData);
break; break;
case EntityKind::EGG: case EntityKind::EGG:
eggData = dynamic_cast<Egg*>(other)->getShinyAppearanceData(); eggData = dynamic_cast<Egg*>(other)->getShinyAppearanceData();
bufferAppearanceData(shinyAppearances, eggData, MAX_SHINY_PER_AROUND); bufferAppearanceData(shinyAppearances, eggData);
break; break;
case EntityKind::BUS: case EntityKind::BUS:
busData = dynamic_cast<Bus*>(other)->getTransportationAppearanceData(); busData = dynamic_cast<Bus*>(other)->getTransportationAppearanceData();
bufferAppearanceData(transportationAppearances, busData, MAX_TRANSPORTATION_PER_AROUND); bufferAppearanceData(transportationAppearances, busData);
break; break;
default: default:
break; break;
@ -220,23 +220,23 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
return; // nothing to send return; // nothing to send
if (!pcAppearances.empty()) if (!pcAppearances.empty())
sendAroundPacket(ref, pcAppearances, MAX_PC_PER_AROUND, P_FE2CL_PC_AROUND); sendAroundPacket(ref, pcAppearances, P_FE2CL_PC_AROUND);
if (!npcAppearances.empty()) if (!npcAppearances.empty())
sendAroundPacket(ref, npcAppearances, MAX_NPC_PER_AROUND, P_FE2CL_NPC_AROUND); sendAroundPacket(ref, npcAppearances, P_FE2CL_NPC_AROUND);
if (!shinyAppearances.empty()) if (!shinyAppearances.empty())
sendAroundPacket(ref, shinyAppearances, MAX_SHINY_PER_AROUND, P_FE2CL_SHINY_AROUND); sendAroundPacket(ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
if (!transportationAppearances.empty()) if (!transportationAppearances.empty())
sendAroundPacket(ref, transportationAppearances, MAX_TRANSPORTATION_PER_AROUND, P_FE2CL_TRANSPORTATION_AROUND); sendAroundPacket(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
} }
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) { void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) {
Entity *ent = ref.getEntity(); Entity *ent = ref.getEntity();
bool alive = ent->isExtant(); bool alive = ent->isExtant();
std::vector<std::vector<int32_t>> pcDisappearances; std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> pcDisappearances;
std::vector<std::vector<int32_t>> npcDisappearances; std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> npcDisappearances;
std::vector<std::vector<int32_t>> shinyDisappearances; std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> shinyDisappearances;
std::vector<std::vector<int32_t>> transportationDisappearances; std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL - 1>> transportationDisappearances;
for (Chunk *chunk : chnks) { for (Chunk *chunk : chnks) {
for (const EntityRef otherRef : chunk->entities) { for (const EntityRef otherRef : chunk->entities) {
// skip oneself // skip oneself
@ -257,21 +257,21 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
{ {
case EntityKind::PLAYER: case EntityKind::PLAYER:
id = dynamic_cast<Player*>(other)->iID; id = dynamic_cast<Player*>(other)->iID;
bufferIdForDisappearance(pcDisappearances, id, MAX_IDS_PER_AROUND_DEL); bufferIdForDisappearance(pcDisappearances, id);
break; break;
case EntityKind::SIMPLE_NPC: case EntityKind::SIMPLE_NPC:
case EntityKind::COMBAT_NPC: case EntityKind::COMBAT_NPC:
case EntityKind::MOB: case EntityKind::MOB:
id = dynamic_cast<BaseNPC*>(other)->id; id = dynamic_cast<BaseNPC*>(other)->id;
bufferIdForDisappearance(npcDisappearances, id, MAX_IDS_PER_AROUND_DEL); bufferIdForDisappearance(npcDisappearances, id);
break; break;
case EntityKind::EGG: case EntityKind::EGG:
id = dynamic_cast<Egg*>(other)->id; id = dynamic_cast<Egg*>(other)->id;
bufferIdForDisappearance(shinyDisappearances, id, MAX_IDS_PER_AROUND_DEL); bufferIdForDisappearance(shinyDisappearances, id);
break; break;
case EntityKind::BUS: case EntityKind::BUS:
id = dynamic_cast<Bus*>(other)->id; id = dynamic_cast<Bus*>(other)->id;
bufferIdForDisappearance(transportationDisappearances, id, MAX_IDS_PER_AROUND_DEL - 1); bufferIdForDisappearance(transportationDisappearances, id);
break; break;
default: default:
break; break;