From ba0540b9c5b13093fd7ca2443621dc59e5a582cb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Dec 2020 16:35:25 -0500 Subject: [PATCH 1/2] IOS: Allow for heterogenous name lookup Allows lookups to be done with std::string_view or any other string type. This allows for non-allocating strings to be used with the name lookup without needing to construct a std::string. --- Source/Core/Core/IOS/IOS.cpp | 4 ++-- Source/Core/Core/IOS/IOS.h | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/IOS/IOS.cpp b/Source/Core/Core/IOS/IOS.cpp index 342a630fe9..25c7dea018 100644 --- a/Source/Core/Core/IOS/IOS.cpp +++ b/Source/Core/Core/IOS/IOS.cpp @@ -505,14 +505,14 @@ s32 Kernel::GetFreeDeviceID() return -1; } -std::shared_ptr Kernel::GetDeviceByName(const std::string& device_name) +std::shared_ptr Kernel::GetDeviceByName(std::string_view device_name) { std::lock_guard lock(m_device_map_mutex); const auto iterator = m_device_map.find(device_name); return iterator != m_device_map.end() ? iterator->second : nullptr; } -std::shared_ptr EmulationKernel::GetDeviceByName(const std::string& device_name) +std::shared_ptr EmulationKernel::GetDeviceByName(std::string_view device_name) { return Kernel::GetDeviceByName(device_name); } diff --git a/Source/Core/Core/IOS/IOS.h b/Source/Core/Core/IOS/IOS.h index daf53585ab..ffb37c47f9 100644 --- a/Source/Core/Core/IOS/IOS.h +++ b/Source/Core/Core/IOS/IOS.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "Common/CommonTypes.h" @@ -110,14 +111,14 @@ protected: void AddDevice(std::unique_ptr device); void AddCoreDevices(); void AddStaticDevices(); - std::shared_ptr GetDeviceByName(const std::string& device_name); + std::shared_ptr GetDeviceByName(std::string_view device_name); s32 GetFreeDeviceID(); IPCCommandResult OpenDevice(OpenRequest& request); bool m_is_responsible_for_nand_root = false; u64 m_title_id = 0; static constexpr u8 IPC_MAX_FDS = 0x18; - std::map> m_device_map; + std::map, std::less<>> m_device_map; std::mutex m_device_map_mutex; // TODO: make this fdmap per process. std::array, IPC_MAX_FDS> m_fdmap; @@ -144,7 +145,7 @@ public: // Get a resource manager by name. // This only works for devices which are part of the device map. - std::shared_ptr GetDeviceByName(const std::string& device_name); + std::shared_ptr GetDeviceByName(std::string_view device_name); }; // Used for controlling and accessing an IOS instance that is tied to emulation. From 0e91470828769bd68b1f3ee59d5616bcf3f129df Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Dec 2020 16:37:24 -0500 Subject: [PATCH 2/2] IOS: Make use of insert_or_assign with AddDevice() operator[] performs a default construction if an object at the given key doesn't exist before overwriting it with the one we provide in operator= insert_or_assign performs optimal insertion by avoiding the default construction if an entry doesn't exist. Not a game changer, but it is essentially a "free" change. --- Source/Core/Core/IOS/IOS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/Core/IOS/IOS.cpp b/Source/Core/Core/IOS/IOS.cpp index 25c7dea018..3f715215dd 100644 --- a/Source/Core/Core/IOS/IOS.cpp +++ b/Source/Core/Core/IOS/IOS.cpp @@ -409,7 +409,7 @@ bool Kernel::BootIOS(const u64 ios_title_id, const std::string& boot_content_pat void Kernel::AddDevice(std::unique_ptr device) { ASSERT(device->GetDeviceType() == Device::Device::DeviceType::Static); - m_device_map[device->GetDeviceName()] = std::move(device); + m_device_map.insert_or_assign(device->GetDeviceName(), std::move(device)); } void Kernel::AddCoreDevices()