From cccfb89aa08cef786bb0803189fb6d0863706864 Mon Sep 17 00:00:00 2001 From: DH Date: Sun, 28 Nov 2021 09:30:41 +0200 Subject: [PATCH] [Config] Use std::less<> for std::map<...> Reduces amount of string copies [Utilities] fmt::replace_all: avoid creation of temporary strings --- Utilities/Config.cpp | 43 +++++++++------- Utilities/Config.h | 49 ++++++++++--------- Utilities/StrUtil.h | 23 ++++++--- rpcs3/Emu/RSX/GL/GLCompute.cpp | 2 +- .../RSX/Program/CgBinaryFragmentProgram.cpp | 2 +- .../Emu/RSX/Program/CgBinaryVertexProgram.cpp | 2 +- .../RSX/Program/FragmentProgramDecompiler.cpp | 4 +- rpcs3/Emu/RSX/Program/GLSLCommon.cpp | 2 +- .../RSX/Program/VertexProgramDecompiler.cpp | 2 +- rpcs3/Emu/RSX/VK/VKCompute.cpp | 4 +- rpcs3/Emu/RSX/VK/VKCompute.h | 2 +- rpcs3/Emu/RSX/VK/VKResolveHelper.h | 3 +- rpcs3/util/logs.cpp | 2 +- rpcs3/util/logs.hpp | 2 +- 14 files changed, 80 insertions(+), 62 deletions(-) diff --git a/Utilities/Config.cpp b/Utilities/Config.cpp index fbf15c9ccc..77a4189440 100644 --- a/Utilities/Config.cpp +++ b/Utilities/Config.cpp @@ -19,8 +19,8 @@ namespace cfg } } - _base::_base(type _type, node* owner, const std::string& name, bool dynamic) - : m_type(_type), m_dynamic(dynamic), m_name(name) + _base::_base(type _type, node* owner, std::string name, bool dynamic) + : m_type(_type), m_dynamic(dynamic), m_name(std::move(name)) { for (const auto& node : owner->m_nodes) { @@ -33,7 +33,7 @@ namespace cfg owner->m_nodes.emplace_back(this); } - bool _base::from_string(const std::string&, bool) + bool _base::from_string(std::string_view, bool) { cfg_log.fatal("cfg::_base::from_string() purecall"); return false; @@ -58,7 +58,7 @@ std::vector cfg::make_int_range(s64 min, s64 max) return {std::to_string(min), std::to_string(max)}; } -bool try_to_int64(s64* out, const std::string& value, s64 min, s64 max) +bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max) { s64 result; const char* start = &value.front(); @@ -104,7 +104,7 @@ std::vector cfg::make_uint_range(u64 min, u64 max) return {std::to_string(min), std::to_string(max)}; } -bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max) +bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max) { u64 result; const char* start = &value.front(); @@ -136,7 +136,7 @@ bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max) return true; } -bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string::format) func, const std::string& value) +bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string::format) func, std::string_view value) { u64 max = umax; @@ -320,7 +320,7 @@ void cfg::decode(const YAML::Node& data, cfg::_base& rhs, bool dynamic) return; } - std::map values; + map_of_type values; for (const auto& pair : data) { @@ -339,7 +339,7 @@ void cfg::decode(const YAML::Node& data, cfg::_base& rhs, bool dynamic) return; // ??? } - std::map values; + map_of_type values; for (const auto& pair : data) { @@ -377,9 +377,9 @@ std::string cfg::node::to_string() const return {out.c_str(), out.size()}; } -bool cfg::node::from_string(const std::string& value, bool dynamic) +bool cfg::node::from_string(std::string_view value, bool dynamic) { - auto [result, error] = yaml_load(value); + auto [result, error] = yaml_load(std::string(value)); if (error.empty()) { @@ -414,26 +414,31 @@ void cfg::set_entry::from_default() m_set = {}; } -std::string cfg::map_entry::get_value(const std::string& key) +std::string cfg::map_entry::get_value(std::string_view key) { - return m_map.contains(key) ? m_map.at(key) : ""; + if (auto it = m_map.find(key); it != m_map.end()) + { + return it->second; + } + + return {}; } -void cfg::map_entry::set_value(const std::string& key, const std::string& value) +void cfg::map_entry::set_value(std::string key, std::string value) { - m_map[key] = value; + m_map[std::move(key)] = std::move(value); } -void cfg::map_entry::set_map(std::map&& map) +void cfg::map_entry::set_map(map_of_type&& map) { m_map = std::move(map); } -void cfg::map_entry::erase(const std::string& key) +void cfg::map_entry::erase(std::string_view key) { - if (m_map.contains(key)) + if (auto it = m_map.find(key); it != m_map.end()) { - m_map.erase(key); + m_map.erase(it); } } @@ -442,7 +447,7 @@ void cfg::map_entry::from_default() set_map({}); } -void cfg::log_entry::set_map(std::map&& map) +void cfg::log_entry::set_map(map_of_type&& map) { m_map = std::move(map); } diff --git a/Utilities/Config.h b/Utilities/Config.h index c92793b4a0..1ca8bbf75d 100644 --- a/Utilities/Config.h +++ b/Utilities/Config.h @@ -21,7 +21,7 @@ namespace cfg std::vector make_uint_range(u64 min, u64 max); // Internal hack - bool try_to_enum_value(u64* out, decltype(&fmt_class_string::format) func, const std::string&); + bool try_to_enum_value(u64* out, decltype(&fmt_class_string::format) func, std::string_view); // Internal hack std::vector try_to_enum_list(decltype(&fmt_class_string::format) func); @@ -53,7 +53,7 @@ namespace cfg _base(type _type); // Owned entry constructor - _base(type _type, class node* owner, const std::string& name, bool dynamic); + _base(type _type, class node* owner, std::string name, bool dynamic); public: _base(const _base&) = delete; @@ -80,7 +80,7 @@ namespace cfg } // Try to convert from string (optional) - virtual bool from_string(const std::string&, bool /*dynamic*/ = false); + virtual bool from_string(std::string_view, bool /*dynamic*/ = false); // Get string list (optional) virtual std::vector to_list() const @@ -107,8 +107,8 @@ namespace cfg } // Registered node constructor - node(node* owner, const std::string& name, bool dynamic = true) - : _base(type::node, owner, name, dynamic) + node(node* owner, std::string name, bool dynamic = true) + : _base(type::node, owner, std::move(name), dynamic) { } @@ -122,7 +122,7 @@ namespace cfg std::string to_string() const override; // Deserialize node - bool from_string(const std::string& value, bool dynamic = false) override; + bool from_string(std::string_view value, bool dynamic = false) override; // Set default values void from_default() override; @@ -135,8 +135,8 @@ namespace cfg public: bool def; - _bool(node* owner, const std::string& name, bool def = false, bool dynamic = false) - : _base(type::_bool, owner, name, dynamic) + _bool(node* owner, std::string name, bool def = false, bool dynamic = false) + : _base(type::_bool, owner, std::move(name), dynamic) , m_value(def) , def(def) { @@ -159,7 +159,7 @@ namespace cfg return m_value ? "true" : "false"; } - bool from_string(const std::string& value, bool /*dynamic*/ = false) override + bool from_string(std::string_view value, bool /*dynamic*/ = false) override { if (value == "false") m_value = false; @@ -220,7 +220,7 @@ namespace cfg return result; // TODO: ??? } - bool from_string(const std::string& value, bool /*dynamic*/ = false) override + bool from_string(std::string_view value, bool /*dynamic*/ = false) override { u64 result; @@ -285,7 +285,7 @@ namespace cfg return std::to_string(m_value); } - bool from_string(const std::string& value, bool /*dynamic*/ = false) override + bool from_string(std::string_view value, bool /*dynamic*/ = false) override { s64 result; if (try_to_int64(&result, value, Min, Max)) @@ -359,7 +359,7 @@ namespace cfg return std::to_string(m_value); } - bool from_string(const std::string& value, bool /*dynamic*/ = false) override + bool from_string(std::string_view value, bool /*dynamic*/ = false) override { u64 result; if (try_to_uint64(&result, value, Min, Max)) @@ -430,9 +430,9 @@ namespace cfg return *m_value.load().get(); } - bool from_string(const std::string& value, bool /*dynamic*/ = false) override + bool from_string(std::string_view value, bool /*dynamic*/ = false) override { - m_value = value; + m_value = std::string(value); return true; } }; @@ -474,9 +474,12 @@ namespace cfg } }; + template + using map_of_type = std::map>; + class map_entry final : public _base { - std::map m_map{}; + map_of_type m_map{}; public: map_entry(node* owner, const std::string& name) @@ -484,24 +487,24 @@ namespace cfg { } - const std::map& get_map() const + const map_of_type& get_map() const { return m_map; } - std::string get_value(const std::string& key); + std::string get_value(std::string_view key); - void set_value(const std::string& key, const std::string& value); - void set_map(std::map&& map); + void set_value(std::string key, std::string value); + void set_map(map_of_type&& map); - void erase(const std::string& key); + void erase(std::string_view key); void from_default() override; }; class log_entry final : public _base { - std::map m_map{}; + map_of_type m_map{}; public: log_entry(node* owner, const std::string& name) @@ -509,12 +512,12 @@ namespace cfg { } - const std::map& get_map() const + const map_of_type& get_map() const { return m_map; } - void set_map(std::map&& map); + void set_map(map_of_type&& map); void from_default() override; }; diff --git a/Utilities/StrUtil.h b/Utilities/StrUtil.h index f305fecf1d..13d4e50ca2 100644 --- a/Utilities/StrUtil.h +++ b/Utilities/StrUtil.h @@ -6,6 +6,8 @@ #include #include +#include "util/types.hpp" + #ifdef _WIN32 std::string wchar_to_utf8(const wchar_t *src); std::string wchar_path_to_ansi_path(const std::wstring& src); @@ -22,17 +24,17 @@ inline void strcpy_trunc(D& dst, const T& src) } // Convert string to signed integer -bool try_to_int64(s64* out, const std::string& value, s64 min, s64 max); +bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max); // Convert string to unsigned integer -bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max); +bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max); namespace fmt { std::string replace_all(std::string_view src, std::string_view from, std::string_view to, usz count = -1); template - std::string replace_all(std::string src, const std::pair (&list)[list_size]) + std::string replace_all(std::string src, const std::pair (&list)[list_size]) { for (usz pos = 0; pos < src.length(); ++pos) { @@ -41,11 +43,14 @@ namespace fmt const usz comp_length = list[i].first.length(); if (src.length() - pos < comp_length) + { continue; + } if (src.substr(pos, comp_length) == list[i].first) { - src = (pos ? src.substr(0, pos) + list[i].second : list[i].second) + src.substr(pos + comp_length); + src.erase(pos, comp_length); + src.insert(pos, list[i].second.data(), list[i].second.length()); pos += list[i].second.length() - 1; break; } @@ -56,7 +61,7 @@ namespace fmt } template - std::string replace_all(std::string src, const std::pair> (&list)[list_size]) + std::string replace_all(std::string src, const std::pair> (&list)[list_size]) { for (usz pos = 0; pos < src.length(); ++pos) { @@ -65,12 +70,16 @@ namespace fmt const usz comp_length = list[i].first.length(); if (src.length() - pos < comp_length) + { continue; + } if (src.substr(pos, comp_length) == list[i].first) { - src = (pos ? src.substr(0, pos) + list[i].second() : list[i].second()) + src.substr(pos + comp_length); - pos += list[i].second().length() - 1; + src.erase(pos, comp_length); + auto replacement = list[i].second(); + src.insert(pos, replacement); + pos += replacement.length() - 1; break; } } diff --git a/rpcs3/Emu/RSX/GL/GLCompute.cpp b/rpcs3/Emu/RSX/GL/GLCompute.cpp index ff19922581..369c4bf119 100644 --- a/rpcs3/Emu/RSX/GL/GLCompute.cpp +++ b/rpcs3/Emu/RSX/GL/GLCompute.cpp @@ -135,7 +135,7 @@ namespace gl " %vars" "\n"; - const std::pair syntax_replace[] = + const std::pair syntax_replace[] = { { "%loc", std::to_string(GL_COMPUTE_BUFFER_SLOT(0)) }, { "%ws", std::to_string(optimal_group_size) }, diff --git a/rpcs3/Emu/RSX/Program/CgBinaryFragmentProgram.cpp b/rpcs3/Emu/RSX/Program/CgBinaryFragmentProgram.cpp index 8b8901aae3..c3b7bc7237 100644 --- a/rpcs3/Emu/RSX/Program/CgBinaryFragmentProgram.cpp +++ b/rpcs3/Emu/RSX/Program/CgBinaryFragmentProgram.cpp @@ -139,7 +139,7 @@ std::string CgBinaryDisasm::GetCondDisAsm() const std::string CgBinaryDisasm::FormatDisAsm(const std::string& code) { - const std::pair> repl_list[] = + const std::pair> repl_list[] = { { "$$", []() -> std::string { return "$"; } }, { "$0", [this]{ return GetSrcDisAsm(src0); } }, diff --git a/rpcs3/Emu/RSX/Program/CgBinaryVertexProgram.cpp b/rpcs3/Emu/RSX/Program/CgBinaryVertexProgram.cpp index e02066b754..304a1fd166 100644 --- a/rpcs3/Emu/RSX/Program/CgBinaryVertexProgram.cpp +++ b/rpcs3/Emu/RSX/Program/CgBinaryVertexProgram.cpp @@ -180,7 +180,7 @@ std::string CgBinaryDisasm::GetTexDisasm() std::string CgBinaryDisasm::FormatDisasm(const std::string& code) { - const std::pair> repl_list[] = + const std::pair> repl_list[] = { { "$$", []() -> std::string { return "$"; } }, { "$0", [this]{ return GetSRCDisasm(0); } }, diff --git a/rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.cpp b/rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.cpp index 551c1257d8..e56e837680 100644 --- a/rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.cpp +++ b/rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.cpp @@ -330,7 +330,7 @@ bool FragmentProgramDecompiler::DstExpectsSca() const std::string FragmentProgramDecompiler::Format(const std::string& code, bool ignore_redirects) { - const std::pair> repl_list[] = + const std::pair> repl_list[] = { { "$$", []() -> std::string { return "$"; } }, { "$0", [this]() -> std::string {return GetSRC(src0);} }, @@ -369,7 +369,7 @@ std::string FragmentProgramDecompiler::Format(const std::string& code, bool igno { //Redirect parameter 0 to the x2d temp register for TEXBEM //TODO: Organize this a little better - std::pair repl[] = { { "$0", "x2d" } }; + std::pair repl[] = { { "$0", "x2d" } }; std::string result = fmt::replace_all(code, repl); return fmt::replace_all(result, repl_list); diff --git a/rpcs3/Emu/RSX/Program/GLSLCommon.cpp b/rpcs3/Emu/RSX/Program/GLSLCommon.cpp index f1b920d047..75a037d577 100644 --- a/rpcs3/Emu/RSX/Program/GLSLCommon.cpp +++ b/rpcs3/Emu/RSX/Program/GLSLCommon.cpp @@ -122,7 +122,7 @@ namespace program_common " return result;\n" "}\n\n"; - std::pair replacements[] = + std::pair replacements[] = {std::make_pair("$T", wide_vector_type), std::make_pair("$I", input_coord)}; diff --git a/rpcs3/Emu/RSX/Program/VertexProgramDecompiler.cpp b/rpcs3/Emu/RSX/Program/VertexProgramDecompiler.cpp index 6dc3cb23e2..3a25240f70 100644 --- a/rpcs3/Emu/RSX/Program/VertexProgramDecompiler.cpp +++ b/rpcs3/Emu/RSX/Program/VertexProgramDecompiler.cpp @@ -222,7 +222,7 @@ std::string VertexProgramDecompiler::GetTex() std::string VertexProgramDecompiler::Format(const std::string& code) { - const std::pair> repl_list[] = + const std::pair> repl_list[] = { { "$$", []() -> std::string { return "$"; } }, { "$0", std::bind(std::mem_fn(&VertexProgramDecompiler::GetSRC), this, 0) }, diff --git a/rpcs3/Emu/RSX/VK/VKCompute.cpp b/rpcs3/Emu/RSX/VK/VKCompute.cpp index 0f8681e6c9..e3f3560cdd 100644 --- a/rpcs3/Emu/RSX/VK/VKCompute.cpp +++ b/rpcs3/Emu/RSX/VK/VKCompute.cpp @@ -251,7 +251,7 @@ namespace vk "\n"; const auto parameters_size = utils::align(push_constants_size, 16) / 16; - const std::pair syntax_replace[] = + const std::pair syntax_replace[] = { { "%ws", std::to_string(optimal_group_size) }, { "%ks", std::to_string(kernel_size) }, @@ -396,7 +396,7 @@ namespace vk " }\n" "}\n"; - const std::pair syntax_replace[] = + const std::pair syntax_replace[] = { { "%ws", std::to_string(optimal_group_size) }, }; diff --git a/rpcs3/Emu/RSX/VK/VKCompute.h b/rpcs3/Emu/RSX/VK/VKCompute.h index 30f15c5cf1..8c1ebbfffd 100644 --- a/rpcs3/Emu/RSX/VK/VKCompute.h +++ b/rpcs3/Emu/RSX/VK/VKCompute.h @@ -565,7 +565,7 @@ namespace vk } } - const std::pair syntax_replace[] = + const std::pair syntax_replace[] = { { "%ws", std::to_string(optimal_group_size) }, { "%_wordcount", std::to_string(sizeof(_BlockType) / 4) }, diff --git a/rpcs3/Emu/RSX/VK/VKResolveHelper.h b/rpcs3/Emu/RSX/VK/VKResolveHelper.h index 4a59127f11..30fd6534a2 100644 --- a/rpcs3/Emu/RSX/VK/VKResolveHelper.h +++ b/rpcs3/Emu/RSX/VK/VKResolveHelper.h @@ -21,6 +21,7 @@ namespace vk virtual ~cs_resolve_base() {} + // FIXME: move body to cpp void build(const std::string& kernel, const std::string& format_prefix, int direction) { create(); @@ -39,7 +40,7 @@ namespace vk break; } - const std::pair syntax_replace[] = + const std::pair syntax_replace[] = { { "%wx", std::to_string(cs_wave_x) }, { "%wy", std::to_string(cs_wave_y) }, diff --git a/rpcs3/util/logs.cpp b/rpcs3/util/logs.cpp index 4ed46e7a2b..b7da7a2398 100644 --- a/rpcs3/util/logs.cpp +++ b/rpcs3/util/logs.cpp @@ -227,7 +227,7 @@ namespace logs } } - void set_channel_levels(const std::map& map) + void set_channel_levels(const std::map>& map) { for (auto&& pair : map) { diff --git a/rpcs3/util/logs.hpp b/rpcs3/util/logs.hpp index daaded35fd..0bc6e31485 100644 --- a/rpcs3/util/logs.hpp +++ b/rpcs3/util/logs.hpp @@ -164,7 +164,7 @@ namespace logs level get_level(const std::string&); // Log level control: set specific channels to level::fatal - void set_channel_levels(const std::map& map); + void set_channel_levels(const std::map>& map); // Get all registered log channels std::vector get_channels();