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] 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)