From d459470feef32756f2e6aed9d1670d6cf2d38341 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 14 Jun 2018 08:58:57 -0400 Subject: [PATCH 1/4] HotkeyManager: Convert file-scope std::string array to constexpr const char* array Previously, a total of 114 std::string instances would need to construct (allocating on the heap for larger strings that can't be stored with small string optimizations). We can just use an array of const char* strings instead, which allows us to avoid this. --- Source/Core/Core/HotkeyManager.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/HotkeyManager.cpp b/Source/Core/Core/HotkeyManager.cpp index 67086b593c..7b3f04a5e6 100644 --- a/Source/Core/Core/HotkeyManager.cpp +++ b/Source/Core/Core/HotkeyManager.cpp @@ -20,7 +20,7 @@ #include "InputCommon/GCPadStatus.h" // clang-format off -const std::string hotkey_labels[] = { +constexpr std::array hotkey_labels{{ _trans("Open"), _trans("Change Disc"), _trans("Eject Disc"), @@ -154,10 +154,9 @@ const std::string hotkey_labels[] = { _trans("Undo Save State"), _trans("Save State"), _trans("Load State"), -}; +}}; // clang-format on -static_assert(NUM_HOTKEYS == sizeof(hotkey_labels) / sizeof(hotkey_labels[0]), - "Wrong count of hotkey_labels"); +static_assert(NUM_HOTKEYS == hotkey_labels.size(), "Wrong count of hotkey_labels"); namespace HotkeyManagerEmu { From aae06f1cf7033a50174285edc919ea2167bfdb1f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 14 Jun 2018 09:12:16 -0400 Subject: [PATCH 2/4] HotkeyManager: Move HotkeyGroupInfo struct into the cpp file This is only ever used internally. Also change the std::string name over to a const char*, so that we don't need to potentially allocate anything on the heap at immediate runtime. --- Source/Core/Core/HotkeyManager.cpp | 9 ++++++++- Source/Core/Core/HotkeyManager.h | 7 ------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/Core/Core/HotkeyManager.cpp b/Source/Core/Core/HotkeyManager.cpp index 7b3f04a5e6..8c897a32b3 100644 --- a/Source/Core/Core/HotkeyManager.cpp +++ b/Source/Core/Core/HotkeyManager.cpp @@ -239,7 +239,14 @@ void Shutdown() } } -const std::array groups_info = { +struct HotkeyGroupInfo +{ + const char* name; + Hotkey first; + Hotkey last; +}; + +constexpr std::array groups_info = { {{_trans("General"), HK_OPEN, HK_EXIT}, {_trans("Volume"), HK_VOLUME_DOWN, HK_VOLUME_TOGGLE_MUTE}, {_trans("Emulation Speed"), HK_DECREASE_EMULATION_SPEED, HK_TOGGLE_THROTTLE}, diff --git a/Source/Core/Core/HotkeyManager.h b/Source/Core/Core/HotkeyManager.h index f8cc2bdc66..29ece35861 100644 --- a/Source/Core/Core/HotkeyManager.h +++ b/Source/Core/Core/HotkeyManager.h @@ -180,13 +180,6 @@ enum HotkeyGroup : int NUM_HOTKEY_GROUPS, }; -struct HotkeyGroupInfo -{ - std::string name; - Hotkey first; - Hotkey last; -}; - struct HotkeyStatus { u32 button[NUM_HOTKEY_GROUPS]; From d22d32372e5686052f2fad34c0a3ca461916f145 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 14 Jun 2018 09:48:39 -0400 Subject: [PATCH 3/4] HotkeyManager: Use std::array where applicable --- Source/Core/Core/HotkeyManager.cpp | 9 ++++----- Source/Core/Core/HotkeyManager.h | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Source/Core/Core/HotkeyManager.cpp b/Source/Core/Core/HotkeyManager.cpp index 8c897a32b3..db60003036 100644 --- a/Source/Core/Core/HotkeyManager.cpp +++ b/Source/Core/Core/HotkeyManager.cpp @@ -160,7 +160,7 @@ static_assert(NUM_HOTKEYS == hotkey_labels.size(), "Wrong count of hotkey_labels namespace HotkeyManagerEmu { -static u32 s_hotkeyDown[NUM_HOTKEY_GROUPS]; +static std::array s_hotkeyDown; static HotkeyStatus s_hotkey; static bool s_enabled; @@ -217,8 +217,7 @@ void Initialize() // load the saved controller config s_config.LoadConfig(true); - for (u32& key : s_hotkeyDown) - key = 0; + s_hotkeyDown = {}; s_enabled = true; } @@ -269,7 +268,7 @@ constexpr std::array groups_info = { HotkeyManager::HotkeyManager() { - for (int group = 0; group < NUM_HOTKEY_GROUPS; group++) + for (std::size_t group = 0; group < m_hotkey_groups.size(); group++) { m_hotkey_groups[group] = (m_keys[group] = new ControllerEmu::Buttons("Keys", groups_info[group].name)); @@ -294,7 +293,7 @@ std::string HotkeyManager::GetName() const void HotkeyManager::GetInput(HotkeyStatus* const kb) { const auto lock = GetStateLock(); - for (int group = 0; group < NUM_HOTKEY_GROUPS; group++) + for (std::size_t group = 0; group < groups_info.size(); group++) { const int group_count = (groups_info[group].last - groups_info[group].first) + 1; std::vector bitmasks(group_count); diff --git a/Source/Core/Core/HotkeyManager.h b/Source/Core/Core/HotkeyManager.h index 29ece35861..8505412d16 100644 --- a/Source/Core/Core/HotkeyManager.h +++ b/Source/Core/Core/HotkeyManager.h @@ -182,7 +182,7 @@ enum HotkeyGroup : int struct HotkeyStatus { - u32 button[NUM_HOTKEY_GROUPS]; + std::array button; s8 err; }; @@ -200,7 +200,7 @@ public: void LoadDefaults(const ControllerInterface& ciface) override; private: - ControllerEmu::Buttons* m_keys[NUM_HOTKEY_GROUPS]; + std::array m_keys; std::array m_hotkey_groups; }; From 410792d922931182613939996b30eaed23c84801 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 14 Jun 2018 09:53:52 -0400 Subject: [PATCH 4/4] HotkeyManager: Add missing s_ prefixes to internally-linked file-scope variables Previously there was an intermixing of no prefixing and prefixing. This makes the notation consistent. --- Source/Core/Core/HotkeyManager.cpp | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Source/Core/Core/HotkeyManager.cpp b/Source/Core/Core/HotkeyManager.cpp index db60003036..80b293f940 100644 --- a/Source/Core/Core/HotkeyManager.cpp +++ b/Source/Core/Core/HotkeyManager.cpp @@ -20,7 +20,7 @@ #include "InputCommon/GCPadStatus.h" // clang-format off -constexpr std::array hotkey_labels{{ +constexpr std::array s_hotkey_labels{{ _trans("Open"), _trans("Change Disc"), _trans("Eject Disc"), @@ -156,11 +156,11 @@ constexpr std::array hotkey_labels{{ _trans("Load State"), }}; // clang-format on -static_assert(NUM_HOTKEYS == hotkey_labels.size(), "Wrong count of hotkey_labels"); +static_assert(NUM_HOTKEYS == s_hotkey_labels.size(), "Wrong count of hotkey_labels"); namespace HotkeyManagerEmu { -static std::array s_hotkeyDown; +static std::array s_hotkey_down; static HotkeyStatus s_hotkey; static bool s_enabled; @@ -194,14 +194,14 @@ bool IsPressed(int id, bool held) static_cast(s_config.GetController(0))->GetIndexForGroup(group, id); if (s_hotkey.button[group] & (1 << group_key)) { - bool pressed = !!(s_hotkeyDown[group] & (1 << group_key)); - s_hotkeyDown[group] |= (1 << group_key); + const bool pressed = !!(s_hotkey_down[group] & (1 << group_key)); + s_hotkey_down[group] |= (1 << group_key); if (!pressed || held) return true; } else { - s_hotkeyDown[group] &= ~(1 << group_key); + s_hotkey_down[group] &= ~(1 << group_key); } return false; @@ -217,7 +217,7 @@ void Initialize() // load the saved controller config s_config.LoadConfig(true); - s_hotkeyDown = {}; + s_hotkey_down = {}; s_enabled = true; } @@ -245,7 +245,7 @@ struct HotkeyGroupInfo Hotkey last; }; -constexpr std::array groups_info = { +constexpr std::array s_groups_info = { {{_trans("General"), HK_OPEN, HK_EXIT}, {_trans("Volume"), HK_VOLUME_DOWN, HK_VOLUME_TOGGLE_MUTE}, {_trans("Emulation Speed"), HK_DECREASE_EMULATION_SPEED, HK_TOGGLE_THROTTLE}, @@ -271,12 +271,12 @@ HotkeyManager::HotkeyManager() for (std::size_t group = 0; group < m_hotkey_groups.size(); group++) { m_hotkey_groups[group] = - (m_keys[group] = new ControllerEmu::Buttons("Keys", groups_info[group].name)); + (m_keys[group] = new ControllerEmu::Buttons("Keys", s_groups_info[group].name)); groups.emplace_back(m_hotkey_groups[group]); - for (int key = groups_info[group].first; key <= groups_info[group].last; key++) + for (int key = s_groups_info[group].first; key <= s_groups_info[group].last; key++) { m_keys[group]->controls.emplace_back( - new ControllerEmu::Input(ControllerEmu::Translate, hotkey_labels[key])); + new ControllerEmu::Input(ControllerEmu::Translate, s_hotkey_labels[key])); } } } @@ -293,9 +293,9 @@ std::string HotkeyManager::GetName() const void HotkeyManager::GetInput(HotkeyStatus* const kb) { const auto lock = GetStateLock(); - for (std::size_t group = 0; group < groups_info.size(); group++) + for (std::size_t group = 0; group < s_groups_info.size(); group++) { - const int group_count = (groups_info[group].last - groups_info[group].first) + 1; + const int group_count = (s_groups_info[group].last - s_groups_info[group].first) + 1; std::vector bitmasks(group_count); for (size_t key = 0; key < bitmasks.size(); key++) bitmasks[key] = static_cast(1 << key); @@ -312,15 +312,15 @@ ControllerEmu::ControlGroup* HotkeyManager::GetHotkeyGroup(HotkeyGroup group) co int HotkeyManager::FindGroupByID(int id) const { - const auto i = std::find_if(groups_info.begin(), groups_info.end(), + const auto i = std::find_if(s_groups_info.begin(), s_groups_info.end(), [id](const auto& entry) { return entry.last >= id; }); - return static_cast(std::distance(groups_info.begin(), i)); + return static_cast(std::distance(s_groups_info.begin(), i)); } int HotkeyManager::GetIndexForGroup(int group, int id) const { - return id - groups_info[group].first; + return id - s_groups_info[group].first; } void HotkeyManager::LoadDefaults(const ControllerInterface& ciface)