From a343c5ac5be5f4a7fc90f6eb5e25d1675342ac30 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Fri, 22 Jul 2016 12:52:12 +0200 Subject: [PATCH] Optimize localized string allocation --- src/Components/Modules/Localization.cpp | 50 ++++++++++------------- src/Components/Modules/Localization.hpp | 1 + src/Components/Modules/Menus.cpp | 2 +- src/Components/Modules/ModList.cpp | 29 +++++++------ src/Components/Modules/ModList.hpp | 46 +++++++++++---------- src/Components/Modules/StringTable.cpp | 2 +- src/Components/Modules/StructuredData.cpp | 2 +- src/Utils/Memory.hpp | 26 +++++++++++- 8 files changed, 90 insertions(+), 68 deletions(-) diff --git a/src/Components/Modules/Localization.cpp b/src/Components/Modules/Localization.cpp index 70505bdf..959e603d 100644 --- a/src/Components/Modules/Localization.cpp +++ b/src/Components/Modules/Localization.cpp @@ -3,6 +3,7 @@ namespace Components { Dvar::Var Localization::UseLocalization; + Utils::Memory::Allocator Localization::MemAllocator; std::map Localization::LocalizeMap; std::map Localization::TempLocalizeMap; @@ -12,28 +13,28 @@ namespace Components { Game::LocalizedEntry* entry = Localization::LocalizeMap[key]; - char* newStaticValue = Utils::Memory::DuplicateString(value); + char* newStaticValue = Localization::MemAllocator.DuplicateString(value); if (!newStaticValue) return; - if (entry->value) Utils::Memory::Free(entry->value); + if (entry->value) Localization::MemAllocator.Free(entry->value); entry->value = newStaticValue; return; } - Game::LocalizedEntry* entry = Utils::Memory::Allocate(); + Game::LocalizedEntry* entry = Localization::MemAllocator.Allocate(); if (!entry) return; - entry->name = Utils::Memory::DuplicateString(key); + entry->name = Localization::MemAllocator.DuplicateString(key); if (!entry->name) { - Utils::Memory::Free(entry); + Localization::MemAllocator.Free(entry); return; } - entry->value = Utils::Memory::DuplicateString(value); + entry->value = Localization::MemAllocator.DuplicateString(value); if (!entry->value) { - Utils::Memory::Free(entry->name); - Utils::Memory::Free(entry); + Localization::MemAllocator.Free(entry->name); + Localization::MemAllocator.Free(entry); return; } @@ -70,26 +71,26 @@ namespace Components if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end()) { Game::LocalizedEntry* entry = Localization::TempLocalizeMap[key]; - if(entry->value) Utils::Memory::Free(entry->value); - entry->value = Utils::Memory::DuplicateString(value); + if(entry->value) Localization::MemAllocator.Free(entry->value); + entry->value = Localization::MemAllocator.DuplicateString(value); } else { - Game::LocalizedEntry* entry = Utils::Memory::Allocate(); + Game::LocalizedEntry* entry = Localization::MemAllocator.Allocate(); if (!entry) return; - entry->name = Utils::Memory::DuplicateString(key); + entry->name = Localization::MemAllocator.DuplicateString(key); if (!entry->name) { - Utils::Memory::Free(entry); + Localization::MemAllocator.Free(entry); return; } - entry->value = Utils::Memory::DuplicateString(value); + entry->value = Localization::MemAllocator.DuplicateString(value); if (!entry->value) { - Utils::Memory::Free(entry->name); - Utils::Memory::Free(entry); + Localization::MemAllocator.Free(entry->name); + Localization::MemAllocator.Free(entry); return; } @@ -103,9 +104,9 @@ namespace Components { if (i->second) { - if (i->second->name) Utils::Memory::Free(i->second->name); - if (i->second->value) Utils::Memory::Free(i->second->value); - Utils::Memory::Free(i->second); + if (i->second->name) Localization::MemAllocator.Free(i->second->name); + if (i->second->value) Localization::MemAllocator.Free(i->second->value); + Localization::MemAllocator.Free(i->second); } } @@ -186,16 +187,7 @@ namespace Components { Localization::ClearTemp(); - for (auto i = Localization::LocalizeMap.begin(); i != Localization::LocalizeMap.end(); ++i) - { - if (i->second) - { - if (i->second->name) Utils::Memory::Free(i->second->name); - if (i->second->value) Utils::Memory::Free(i->second->value); - Utils::Memory::Free(i->second); - } - } - Localization::LocalizeMap.clear(); + Localization::MemAllocator.Clear(); } } diff --git a/src/Components/Modules/Localization.hpp b/src/Components/Modules/Localization.hpp index fb34f73d..26700970 100644 --- a/src/Components/Modules/Localization.hpp +++ b/src/Components/Modules/Localization.hpp @@ -14,6 +14,7 @@ namespace Components static void ClearTemp(); private: + static Utils::Memory::Allocator MemAllocator; static std::map LocalizeMap; static std::map TempLocalizeMap; static Dvar::Var UseLocalization; diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index e461dfd1..6519d2a7 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -566,7 +566,7 @@ namespace Components if (originalConnect == menu) // Check if we draw the original loadscreen { - if (Menus::MenuList.find("connect") != Menus::MenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one ontop + if (Menus::MenuList.find("connect") != Menus::MenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one on top { return false; } diff --git a/src/Components/Modules/ModList.cpp b/src/Components/Modules/ModList.cpp index 090727a4..8854ece8 100644 --- a/src/Components/Modules/ModList.cpp +++ b/src/Components/Modules/ModList.cpp @@ -52,18 +52,7 @@ namespace Components { if (ModList::CurrentMod < ModList::Mods.size()) { - auto fsGame = Dvar::Var("fs_game"); - fsGame.Set(fmt::sprintf("mods/%s", ModList::Mods[ModList::CurrentMod].data())); - fsGame.Get()->pad2[0] = 1; - - if (Dvar::Var("cl_modVidRestart").Get()) - { - Command::Execute("vid_restart", false); - } - else - { - Command::Execute("closemenu mods_menu", false); - } + ModList::RunMod(ModList::Mods[ModList::CurrentMod]); } } @@ -83,6 +72,22 @@ namespace Components } } + void ModList::RunMod(std::string mod) + { + auto fsGame = Dvar::Var("fs_game"); + fsGame.Set(fmt::sprintf("mods/%s", mod.data())); + fsGame.Get()->pad2[0] = 1; + + if (Dvar::Var("cl_modVidRestart").Get()) + { + Command::Execute("vid_restart", false); + } + else + { + Command::Execute("closemenu mods_menu", false); + } + } + ModList::ModList() { ModList::CurrentMod = 0; diff --git a/src/Components/Modules/ModList.hpp b/src/Components/Modules/ModList.hpp index 710ef748..c99b38cb 100644 --- a/src/Components/Modules/ModList.hpp +++ b/src/Components/Modules/ModList.hpp @@ -1,23 +1,25 @@ -namespace Components -{ - class ModList : public Component - { - public: - ModList(); - ~ModList(); - const char* GetName() { return "ModList"; }; - - private: - static std::vector Mods; - static unsigned int CurrentMod; - - static bool HasMod(std::string modName); - - static unsigned int GetItemCount(); - static const char* GetItemText(unsigned int index, int column); - static void Select(unsigned int index); - static void UIScript_LoadMods(); - static void UIScript_RunMod(); - static void UIScript_ClearMods(); - }; +namespace Components +{ + class ModList : public Component + { + public: + ModList(); + ~ModList(); + const char* GetName() { return "ModList"; }; + + static void RunMod(std::string mod); + + private: + static std::vector Mods; + static unsigned int CurrentMod; + + static bool HasMod(std::string modName); + + static unsigned int GetItemCount(); + static const char* GetItemText(unsigned int index, int column); + static void Select(unsigned int index); + static void UIScript_LoadMods(); + static void UIScript_RunMod(); + static void UIScript_ClearMods(); + }; } \ No newline at end of file diff --git a/src/Components/Modules/StringTable.cpp b/src/Components/Modules/StringTable.cpp index 30cfd183..6a5cbd99 100644 --- a/src/Components/Modules/StringTable.cpp +++ b/src/Components/Modules/StringTable.cpp @@ -87,6 +87,6 @@ namespace Components StringTable::~StringTable() { StringTable::StringTableMap.clear(); - StringTable::MemAllocator.Free(); + StringTable::MemAllocator.Clear(); } } diff --git a/src/Components/Modules/StructuredData.cpp b/src/Components/Modules/StructuredData.cpp index 0542720d..99866c85 100644 --- a/src/Components/Modules/StructuredData.cpp +++ b/src/Components/Modules/StructuredData.cpp @@ -208,6 +208,6 @@ namespace Components StructuredData::~StructuredData() { - StructuredData::MemAllocator.Free(); + StructuredData::MemAllocator.Clear(); } } diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index 110f8ddc..5164664a 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -15,10 +15,10 @@ namespace Utils } ~Allocator() { - this->Free(); + this->Clear(); } - void Free() + void Clear() { for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i) { @@ -38,6 +38,28 @@ namespace Utils this->Pool.clear(); } + void Free(void* data) + { + auto i = this->RefMemory.find(data); + if (i != this->RefMemory.end()) + { + i->second(i->first); + this->RefMemory.erase(i); + } + + auto j = std::find(this->Pool.begin(), this->Pool.end(), data); + if (j != this->Pool.end()) + { + Memory::Free(data); + this->Pool.erase(j); + } + } + + void Free(const void* data) + { + this->Free(const_cast(data)); + } + void Reference(void* memory, FreeCallback callback) { this->RefMemory[memory] = callback;