perf: eliminate ~70 unnecessary sqrt ops per frame, optimize caches and threading

Squared distance optimizations across 30 files:
- Convert glm::length() comparisons to glm::dot() (no sqrt)
- Use glm::inversesqrt() for check-then-normalize patterns (1 rsqrt vs 2 sqrt)
- Defer sqrt to after early-out checks in collision/movement code
- Hottest paths: camera_controller (21), weather particles, WMO collision,
  transport movement, creature interpolation, nameplate culling

Container and algorithm improvements:
- std::map<string> → std::unordered_map for asset/DBC/MPQ/warden caches
- std::mutex → std::shared_mutex for asset_manager and mpq_manager caches
- std::sort → std::partial_sort in lighting_manager (top-2 of N volumes)
- Double-lookup find()+operator[] → insert_or_assign in game_handler
- Add reserve() for per-frame vectors: weather, swim_effects, WMO/M2 collision

Threading and synchronization:
- Replace 1ms busy-wait polling with condition_variable in character_renderer
- Move timestamp capture before mutex in logger
- Use memory_order_acquire/release for normal map completion signaling

API additions:
- DBC getStringView()/getStringViewByOffset() for zero-copy string access
- Parse creature display IDs from SMSG_CREATURE_QUERY_SINGLE_RESPONSE
This commit is contained in:
Kelsi 2026-03-27 16:33:16 -07:00
parent cf0e2aa240
commit b0466e9029
29 changed files with 328 additions and 196 deletions

View file

@ -5,6 +5,7 @@
#include <string>
#include <memory>
#include <map>
#include <unordered_map>
#include <functional>
// Forward declare unicorn types (will include in .cpp)
@ -148,7 +149,7 @@ private:
uint32_t apiStubBase_; // API stub base address
// API hooks: DLL name -> Function name -> stub address
std::map<std::string, std::map<std::string, uint32_t>> apiAddresses_;
std::unordered_map<std::string, std::unordered_map<std::string, uint32_t>> apiAddresses_;
// API stub dispatch: stub address -> {argCount, handler}
struct ApiHookEntry {

View file

@ -1502,9 +1502,10 @@ struct CreatureQueryResponseData {
std::string subName;
std::string iconName;
uint32_t typeFlags = 0;
uint32_t creatureType = 0;
uint32_t creatureType = 0; // 1=Beast, 2=Dragonkin, 3=Demon, 4=Elemental, 5=Giant, 6=Undead, 7=Humanoid, ...
uint32_t family = 0;
uint32_t rank = 0; // 0=Normal, 1=Elite, 2=Rare Elite, 3=Boss, 4=Rare
uint32_t displayId[4] = {}; // Up to 4 random display models (0 = unused)
bool isValid() const { return entry != 0 && !name.empty(); }
};

View file

@ -8,7 +8,9 @@
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <mutex>
#include <shared_mutex>
namespace wowee {
namespace pipeline {
@ -164,15 +166,15 @@ private:
*/
std::string resolveFile(const std::string& normalizedPath) const;
mutable std::mutex cacheMutex;
std::map<std::string, std::shared_ptr<DBCFile>> dbcCache;
mutable std::shared_mutex cacheMutex;
std::unordered_map<std::string, std::shared_ptr<DBCFile>> dbcCache;
// File cache (LRU, dynamic budget based on system RAM)
struct CachedFile {
std::vector<uint8_t> data;
uint64_t lastAccessTime;
};
mutable std::map<std::string, CachedFile> fileCache;
mutable std::unordered_map<std::string, CachedFile> fileCache;
mutable size_t fileCacheTotalBytes = 0;
mutable uint64_t fileCacheAccessCounter = 0;
mutable size_t fileCacheHits = 0;

View file

@ -3,6 +3,7 @@
#include <vector>
#include <map>
#include <string>
#include <string_view>
#include <cstdint>
#include <memory>
@ -92,6 +93,11 @@ public:
*/
std::string getString(uint32_t recordIndex, uint32_t fieldIndex) const;
/**
* Get a string field as a view (no allocation; valid while this DBCFile lives)
*/
std::string_view getStringView(uint32_t recordIndex, uint32_t fieldIndex) const;
/**
* Get string by offset in string block
* @param offset Offset into string block
@ -99,6 +105,11 @@ public:
*/
std::string getStringByOffset(uint32_t offset) const;
/**
* Get string by offset as a view (no allocation; valid while this DBCFile lives)
*/
std::string_view getStringViewByOffset(uint32_t offset) const;
/**
* Find a record by ID (assumes first field is ID)
* @param id Record ID to find

View file

@ -8,6 +8,7 @@
#include <unordered_map>
#include <unordered_set>
#include <mutex>
#include <shared_mutex>
// Forward declare StormLib handle
typedef void* HANDLE;
@ -115,7 +116,7 @@ private:
//
// Important: caching misses can blow up memory if the game probes many unique non-existent filenames.
// Miss caching is disabled by default and must be explicitly enabled.
mutable std::mutex fileArchiveCacheMutex_;
mutable std::shared_mutex fileArchiveCacheMutex_;
mutable std::unordered_map<std::string, HANDLE> fileArchiveCache_;
size_t fileArchiveCacheMaxEntries_ = 500000;
bool fileArchiveCacheMisses_ = false;

View file

@ -13,6 +13,7 @@
#include <utility>
#include <future>
#include <deque>
#include <condition_variable>
#include <mutex>
#include <atomic>
@ -325,6 +326,7 @@ private:
};
// Completed results ready for GPU upload (populated by background threads)
std::mutex normalMapResultsMutex_;
std::condition_variable normalMapDoneCV_; // signaled when pendingNormalMapCount_ reaches 0
std::deque<NormalMapResult> completedNormalMaps_;
std::atomic<int> pendingNormalMapCount_{0}; // in-flight background tasks