From 74224c94a738acdcf326c563dde9f5912f24379e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Dec 2020 18:19:08 -0500 Subject: [PATCH 1/3] SysConf: Make use of std::string_view We can allow strings to be used with the SysConf interface in potentially non-allocating manners. --- Source/Core/Core/SysConf.cpp | 10 +++++----- Source/Core/Core/SysConf.h | 13 +++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Source/Core/Core/SysConf.cpp b/Source/Core/Core/SysConf.cpp index babbe1cc67..d9b3bb7907 100644 --- a/Source/Core/Core/SysConf.cpp +++ b/Source/Core/Core/SysConf.cpp @@ -227,29 +227,29 @@ void SysConf::AddEntry(Entry&& entry) m_entries.emplace_back(std::move(entry)); } -SysConf::Entry* SysConf::GetEntry(const std::string& key) +SysConf::Entry* SysConf::GetEntry(std::string_view key) { const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), [&key](const auto& entry) { return entry.name == key; }); return iterator != m_entries.end() ? &*iterator : nullptr; } -const SysConf::Entry* SysConf::GetEntry(const std::string& key) const +const SysConf::Entry* SysConf::GetEntry(std::string_view key) const { const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), [&key](const auto& entry) { return entry.name == key; }); return iterator != m_entries.end() ? &*iterator : nullptr; } -SysConf::Entry* SysConf::GetOrAddEntry(const std::string& key, Entry::Type type) +SysConf::Entry* SysConf::GetOrAddEntry(std::string_view key, Entry::Type type) { if (Entry* entry = GetEntry(key)) return entry; - AddEntry({type, key}); + AddEntry({type, std::string(key)}); return GetEntry(key); } -void SysConf::RemoveEntry(const std::string& key) +void SysConf::RemoveEntry(std::string_view key) { m_entries.erase(std::remove_if(m_entries.begin(), m_entries.end(), [&key](const auto& entry) { return entry.name == key; }), diff --git a/Source/Core/Core/SysConf.h b/Source/Core/Core/SysConf.h index 79de23e5c3..592046a085 100644 --- a/Source/Core/Core/SysConf.h +++ b/Source/Core/Core/SysConf.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "Common/Assert.h" @@ -75,20 +76,20 @@ public: }; void AddEntry(Entry&& entry); - Entry* GetEntry(const std::string& key); - const Entry* GetEntry(const std::string& key) const; - Entry* GetOrAddEntry(const std::string& key, Entry::Type type); - void RemoveEntry(const std::string& key); + Entry* GetEntry(std::string_view key); + const Entry* GetEntry(std::string_view key) const; + Entry* GetOrAddEntry(std::string_view key, Entry::Type type); + void RemoveEntry(std::string_view key); // Intended for use with the non array types. template - T GetData(const std::string& key, T default_value) const + T GetData(std::string_view key, T default_value) const { const Entry* entry = GetEntry(key); return entry ? entry->GetData(default_value) : default_value; } template - void SetData(const std::string& key, Entry::Type type, T value) + void SetData(std::string_view key, Entry::Type type, T value) { GetOrAddEntry(key, type)->SetData(value); } From 05094ab51f43032206270c0f5aba3b3c9cd316ca Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Dec 2020 18:24:21 -0500 Subject: [PATCH 2/3] SysConf: Return emplaced reference from AddEntry() Allows GetOrAddEntry() to be implemented in a manner that doesn't result in a redundant lookup in the event an addition needs to be made. --- Source/Core/Core/SysConf.cpp | 8 ++++---- Source/Core/Core/SysConf.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/SysConf.cpp b/Source/Core/Core/SysConf.cpp index d9b3bb7907..b4e0b2834a 100644 --- a/Source/Core/Core/SysConf.cpp +++ b/Source/Core/Core/SysConf.cpp @@ -222,9 +222,9 @@ SysConf::Entry::Entry(Type type_, const std::string& name_, std::vector byte { } -void SysConf::AddEntry(Entry&& entry) +SysConf::Entry& SysConf::AddEntry(Entry&& entry) { - m_entries.emplace_back(std::move(entry)); + return m_entries.emplace_back(std::move(entry)); } SysConf::Entry* SysConf::GetEntry(std::string_view key) @@ -245,8 +245,8 @@ SysConf::Entry* SysConf::GetOrAddEntry(std::string_view key, Entry::Type type) { if (Entry* entry = GetEntry(key)) return entry; - AddEntry({type, std::string(key)}); - return GetEntry(key); + + return &AddEntry({type, std::string(key)}); } void SysConf::RemoveEntry(std::string_view key) diff --git a/Source/Core/Core/SysConf.h b/Source/Core/Core/SysConf.h index 592046a085..5eeeddb121 100644 --- a/Source/Core/Core/SysConf.h +++ b/Source/Core/Core/SysConf.h @@ -75,7 +75,7 @@ public: std::vector bytes; }; - void AddEntry(Entry&& entry); + Entry& AddEntry(Entry&& entry); Entry* GetEntry(std::string_view key); const Entry* GetEntry(std::string_view key) const; Entry* GetOrAddEntry(std::string_view key, Entry::Type type); From 1bbfde62d12f8b1557c6a9082bc4d4f35b7973c9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Dec 2020 19:09:54 -0500 Subject: [PATCH 3/3] SysConf: std::move name in Entry constructor Allows code to move into the constructor, avoiding copies entirely. --- Source/Core/Core/SysConf.cpp | 6 +++--- Source/Core/Core/SysConf.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/SysConf.cpp b/Source/Core/Core/SysConf.cpp index b4e0b2834a..1efd4a2244 100644 --- a/Source/Core/Core/SysConf.cpp +++ b/Source/Core/Core/SysConf.cpp @@ -211,14 +211,14 @@ bool SysConf::Save() const return result == IOS::HLE::FS::ResultCode::Success; } -SysConf::Entry::Entry(Type type_, const std::string& name_) : type(type_), name(name_) +SysConf::Entry::Entry(Type type_, std::string name_) : type(type_), name(std::move(name_)) { if (type != Type::SmallArray && type != Type::BigArray) bytes.resize(GetNonArrayEntrySize(type)); } -SysConf::Entry::Entry(Type type_, const std::string& name_, std::vector bytes_) - : type(type_), name(name_), bytes(std::move(bytes_)) +SysConf::Entry::Entry(Type type_, std::string name_, std::vector bytes_) + : type(type_), name(std::move(name_)), bytes(std::move(bytes_)) { } diff --git a/Source/Core/Core/SysConf.h b/Source/Core/Core/SysConf.h index 5eeeddb121..cf86a3e9c5 100644 --- a/Source/Core/Core/SysConf.h +++ b/Source/Core/Core/SysConf.h @@ -47,8 +47,8 @@ public: ByteBool = 7, }; - Entry(Type type_, const std::string& name_); - Entry(Type type_, const std::string& name_, std::vector bytes_); + Entry(Type type_, std::string name_); + Entry(Type type_, std::string name_, std::vector bytes_); // Intended for use with the non array types. template