From 809766a4391b7f7b30d65804ae22a0f3e0c8a1cc Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:26:08 -0700 Subject: [PATCH 1/5] Simplify `std::fill` with `std::array::fill` --- Source/Core/Common/Assembler/CaseInsensitiveDict.h | 2 +- Source/Core/InputCommon/GCAdapter.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/Assembler/CaseInsensitiveDict.h b/Source/Core/Common/Assembler/CaseInsensitiveDict.h index 8f6acccc8f..5dffbb6335 100644 --- a/Source/Core/Common/Assembler/CaseInsensitiveDict.h +++ b/Source/Core/Common/Assembler/CaseInsensitiveDict.h @@ -47,7 +47,7 @@ private: std::array _conns; std::optional _val; - TrieEntry() { std::fill(_conns.begin(), _conns.end(), INVALID_CONN); } + TrieEntry() { _conns.fill(INVALID_CONN); } }; constexpr size_t IndexOf(char c) const diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 01cfcd2f92..84b5022c99 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -915,7 +915,7 @@ static void ResetRumbleLockNeeded() return; } - std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0); + s_controller_rumble.fill(0); std::array rumble = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], From e6f93efac4c47c9281c98ad426e54e6fa5e4b0c7 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sun, 29 Sep 2024 10:44:23 -0700 Subject: [PATCH 2/5] Simplify `std::transform` with `std::ranges::transform_view` --- Source/Core/InputCommon/ControllerEmu/StickGate.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/InputCommon/ControllerEmu/StickGate.cpp b/Source/Core/InputCommon/ControllerEmu/StickGate.cpp index 4edc07fd02..2c82788b99 100644 --- a/Source/Core/InputCommon/ControllerEmu/StickGate.cpp +++ b/Source/Core/InputCommon/ControllerEmu/StickGate.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -284,11 +285,10 @@ void ReshapableInput::SaveConfig(Common::IniFile::Section* section, 50.0); } - std::vector save_data(m_calibration.size()); - std::transform( - m_calibration.begin(), m_calibration.end(), save_data.begin(), - [](ControlState val) { return fmt::format("{:.2f}", val * CALIBRATION_CONFIG_SCALE); }); - section->Set(group + CALIBRATION_CONFIG_NAME, fmt::to_string(fmt::join(save_data, " ")), ""); + const std::ranges::transform_view scaled_calibration( + m_calibration, [](ControlState val) { return val * CALIBRATION_CONFIG_SCALE; }); + section->Set(group + CALIBRATION_CONFIG_NAME, + fmt::format("{:.2f}", fmt::join(scaled_calibration, " ")), ""); // Save center value. static constexpr char center_format[] = "{:.2f} {:.2f}"; From 519da8297ce892f9edc215806bb641a6003d623d Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:17:12 -0700 Subject: [PATCH 3/5] Simplify `std::stable_sort` with `std::ranges::stable_partition` --- Source/Core/DolphinQt/Config/ARCodeWidget.cpp | 10 ++-------- Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp | 10 ++-------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/Source/Core/DolphinQt/Config/ARCodeWidget.cpp b/Source/Core/DolphinQt/Config/ARCodeWidget.cpp index 933c6575ef..e7d44b8fe6 100644 --- a/Source/Core/DolphinQt/Config/ARCodeWidget.cpp +++ b/Source/Core/DolphinQt/Config/ARCodeWidget.cpp @@ -140,20 +140,14 @@ void ARCodeWidget::SortAlphabetically() void ARCodeWidget::SortEnabledCodesFirst() { - std::stable_sort(m_ar_codes.begin(), m_ar_codes.end(), [](const auto& a, const auto& b) { - return a.enabled && a.enabled != b.enabled; - }); - + std::ranges::stable_partition(m_ar_codes, std::identity{}, &ActionReplay::ARCode::enabled); UpdateList(); SaveCodes(); } void ARCodeWidget::SortDisabledCodesFirst() { - std::stable_sort(m_ar_codes.begin(), m_ar_codes.end(), [](const auto& a, const auto& b) { - return !a.enabled && a.enabled != b.enabled; - }); - + std::ranges::stable_partition(m_ar_codes, std::logical_not{}, &ActionReplay::ARCode::enabled); UpdateList(); SaveCodes(); } diff --git a/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp b/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp index 0fc875401c..54ded43879 100644 --- a/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp +++ b/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp @@ -314,20 +314,14 @@ void GeckoCodeWidget::SortAlphabetically() void GeckoCodeWidget::SortEnabledCodesFirst() { - std::stable_sort(m_gecko_codes.begin(), m_gecko_codes.end(), [](const auto& a, const auto& b) { - return a.enabled && a.enabled != b.enabled; - }); - + std::ranges::stable_partition(m_gecko_codes, std::identity{}, &Gecko::GeckoCode::enabled); UpdateList(); SaveCodes(); } void GeckoCodeWidget::SortDisabledCodesFirst() { - std::stable_sort(m_gecko_codes.begin(), m_gecko_codes.end(), [](const auto& a, const auto& b) { - return !a.enabled && a.enabled != b.enabled; - }); - + std::ranges::stable_partition(m_gecko_codes, std::logical_not{}, &Gecko::GeckoCode::enabled); UpdateList(); SaveCodes(); } From 4c064de235e4e45cd8756a02960373231cb08d49 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:30:19 -0700 Subject: [PATCH 4/5] Simplify `std::copy` with `fmt::join` --- Source/Core/UICommon/GameFile.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Source/Core/UICommon/GameFile.cpp b/Source/Core/UICommon/GameFile.cpp index 7603463138..6068e1234e 100644 --- a/Source/Core/UICommon/GameFile.cpp +++ b/Source/Core/UICommon/GameFile.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -18,6 +17,7 @@ #include #include +#include #include #include "Common/BitUtils.h" @@ -643,10 +643,7 @@ std::string GameFile::GetNetPlayName(const Core::TitleDatabase& title_database) } if (info.empty()) return name; - std::ostringstream ss; - std::copy(info.begin(), info.end() - 1, std::ostream_iterator(ss, ", ")); - ss << info.back(); - return name + " (" + ss.str() + ")"; + return fmt::format("{} ({})", name, fmt::join(info, ", ")); } static Common::SHA1::Digest GetHash(u32 value) From ff6845288e376c4cb60645a96525d8ae63549c8e Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:24:24 -0700 Subject: [PATCH 5/5] Simplify `std::copy` with `fmt::format_to` Plus a few other memory allocation optimizations. --- Source/Core/Core/IOS/ES/Formats.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index 2826f7aed5..358387778e 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -580,7 +581,7 @@ SharedContentMap::GetFilenameFromSHA1(const std::array& sha1) const if (it == m_entries.end()) return {}; - const std::string id_string(it->id.begin(), it->id.end()); + const std::string_view id_string(reinterpret_cast(it->id.data()), it->id.size()); return fmt::format("/shared1/{}.app", id_string); } @@ -596,20 +597,22 @@ std::vector> SharedContentMap::GetHashes() const std::string SharedContentMap::AddSharedContent(const std::array& sha1) { - auto filename = GetFilenameFromSHA1(sha1); - if (filename) - return *filename; + if (auto filename = GetFilenameFromSHA1(sha1)) + return *std::move(filename); - const std::string id = fmt::format("{:08x}", m_last_id); - Entry entry; - std::copy(id.cbegin(), id.cend(), entry.id.begin()); + Entry& entry = m_entries.emplace_back(); + static_assert(sizeof(m_last_id) == 4, + "'m_last_id' must be represented by 8 characters when formatted in hexadecimal."); + static_assert(std::tuple_size_v == sizeof(m_last_id) * 2, + "'entry.id' must be a std::array capable of storing every nibble of 'm_last_id'."); + fmt::format_to(entry.id.data(), "{:08x}", m_last_id); entry.sha1 = sha1; - m_entries.push_back(entry); WriteEntries(); - filename = fmt::format("/shared1/{}.app", id); m_last_id++; - return *filename; + + const std::string_view id_string(reinterpret_cast(entry.id.data()), entry.id.size()); + return fmt::format("/shared1/{}.app", id_string); } bool SharedContentMap::DeleteSharedContent(const std::array& sha1)