perf: entity/skill/DBC/warden maps to unordered_map; fix 3x contacts scan

Entity storage: std::map<uint64_t, shared_ptr<Entity>> → unordered_map for
O(1) entity lookups instead of O(log n). No code depends on GUID ordering.

Player skills: std::map<uint32_t, PlayerSkill> → unordered_map.
DBC ID cache: std::map<uint32_t, uint32_t> → unordered_map.
Warden: apiHandlers_ and allocations_ → unordered_map (freeBlocks_ kept
as std::map since its coalescing logic requires ordered iteration).

Contacts: handleFriendStatus() did 3 separate O(n) find_if scans per
packet. Consolidated to single find_if with iterator reuse. O(3n) → O(n).
This commit is contained in:
Kelsi 2026-03-27 18:28:36 -07:00
parent 2af3594ce8
commit e61b23626a
5 changed files with 20 additions and 21 deletions

View file

@ -3,6 +3,7 @@
#include <cstdint>
#include <string>
#include <map>
#include <unordered_map>
#include <memory>
namespace wowee {
@ -338,7 +339,7 @@ public:
bool hasEntity(uint64_t guid) const;
// Get all entities
const std::map<uint64_t, std::shared_ptr<Entity>>& getEntities() const {
const std::unordered_map<uint64_t, std::shared_ptr<Entity>>& getEntities() const {
return entities;
}
@ -353,7 +354,7 @@ public:
}
private:
std::map<uint64_t, std::shared_ptr<Entity>> entities;
std::unordered_map<uint64_t, std::shared_ptr<Entity>> entities;
};
} // namespace game

View file

@ -1106,7 +1106,7 @@ public:
uint32_t getOverrideLightTransMs() const { return overrideLightTransMs_; }
// Player skills
const std::map<uint32_t, PlayerSkill>& getPlayerSkills() const { return playerSkills_; }
const std::unordered_map<uint32_t, PlayerSkill>& getPlayerSkills() const { return playerSkills_; }
const std::string& getSkillName(uint32_t skillId) const;
uint32_t getSkillCategory(uint32_t skillId) const;
bool isProfessionSpell(uint32_t spellId) const;
@ -3502,7 +3502,7 @@ private:
uint32_t overrideLightTransMs_ = 0;
// ---- Player skills ----
std::map<uint32_t, PlayerSkill> playerSkills_;
std::unordered_map<uint32_t, PlayerSkill> playerSkills_;
std::unordered_map<uint32_t, std::string> skillLineNames_;
std::unordered_map<uint32_t, uint32_t> skillLineCategories_;
std::unordered_map<uint32_t, uint32_t> spellToSkillLine_; // spellID -> skillLineID

View file

@ -156,12 +156,12 @@ private:
int argCount;
std::function<uint32_t(WardenEmulator&, const std::vector<uint32_t>&)> handler;
};
std::map<uint32_t, ApiHookEntry> apiHandlers_;
std::unordered_map<uint32_t, ApiHookEntry> apiHandlers_;
uint32_t nextApiStubAddr_; // tracks next free stub slot (replaces static local)
bool apiCodeHookRegistered_; // true once UC_HOOK_CODE for stub range is added
// Memory allocation tracking
std::map<uint32_t, size_t> allocations_;
std::unordered_map<uint32_t, size_t> allocations_;
std::map<uint32_t, size_t> freeBlocks_; // free-list keyed by base address
uint32_t nextHeapAddr_;

View file

@ -1,7 +1,7 @@
#pragma once
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <string_view>
#include <cstdint>
@ -137,7 +137,7 @@ private:
std::vector<uint8_t> stringBlock; // String block
// Cache for record ID -> index lookup
mutable std::map<uint32_t, uint32_t> idToIndexCache;
mutable std::unordered_map<uint32_t, uint32_t> idToIndexCache;
mutable bool idCacheBuilt = false;
void buildIdCache() const;

View file

@ -23802,16 +23802,16 @@ void GameHandler::handleFriendStatus(network::Packet& packet) {
return;
}
// Single lookup — reuse iterator for name resolution and update/erase below
auto cit = std::find_if(contacts_.begin(), contacts_.end(),
[&](const ContactEntry& e){ return e.guid == data.guid; });
// Look up player name: contacts_ (populated by SMSG_FRIEND_LIST) > playerNameCache
std::string playerName;
{
auto cit2 = std::find_if(contacts_.begin(), contacts_.end(),
[&](const ContactEntry& e){ return e.guid == data.guid; });
if (cit2 != contacts_.end() && !cit2->name.empty()) {
playerName = cit2->name;
} else {
playerName = lookupName(data.guid);
}
if (cit != contacts_.end() && !cit->name.empty()) {
playerName = cit->name;
} else {
playerName = lookupName(data.guid);
}
// Update friends cache
@ -23823,11 +23823,9 @@ void GameHandler::handleFriendStatus(network::Packet& packet) {
// Mirror into contacts_: update existing entry or add/remove as needed
if (data.status == 0) { // Removed from friends list
contacts_.erase(std::remove_if(contacts_.begin(), contacts_.end(),
[&](const ContactEntry& e){ return e.guid == data.guid; }), contacts_.end());
if (cit != contacts_.end())
contacts_.erase(cit);
} else {
auto cit = std::find_if(contacts_.begin(), contacts_.end(),
[&](const ContactEntry& e){ return e.guid == data.guid; });
if (cit != contacts_.end()) {
if (!playerName.empty() && playerName != "Unknown") cit->name = playerName;
// status: 2=online→1, 3=offline→0, 1=added→1 (online on add)
@ -24028,7 +24026,7 @@ void GameHandler::extractSkillFields(const std::map<uint16_t, uint32_t>& fields)
const uint16_t PLAYER_SKILL_INFO_START = fieldIndex(UF::PLAYER_SKILL_INFO_START);
static constexpr int MAX_SKILL_SLOTS = 128;
std::map<uint32_t, PlayerSkill> newSkills;
std::unordered_map<uint32_t, PlayerSkill> newSkills;
for (int slot = 0; slot < MAX_SKILL_SLOTS; slot++) {
uint16_t baseField = PLAYER_SKILL_INFO_START + slot * 3;