From f241bb72f5b6b0b213053e000051bf3b7e6b4bb0 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 28 Jan 2017 14:48:13 -0800 Subject: [PATCH] Pica/Regs: Use binary search to look up reg names This gets rid of the static unordered_map. Also changes the return type const char*, avoiding unnecessary allocations (the result was only used by calling .c_str() on it.) --- .../debugger/graphics/graphics_cmdlists.cpp | 2 +- src/video_core/regs.cpp | 21 +++++++------------ src/video_core/regs.h | 4 ++-- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp index 536548f361..c68fe753b2 100644 --- a/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp +++ b/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp @@ -72,7 +72,7 @@ QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const { if (role == Qt::DisplayRole) { switch (index.column()) { case 0: - return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str()); + return QString::fromLatin1(Pica::Regs::GetRegisterName(write.cmd_id)); case 1: return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0')); case 2: diff --git a/src/video_core/regs.cpp b/src/video_core/regs.cpp index f47e9e7634..2699e710aa 100644 --- a/src/video_core/regs.cpp +++ b/src/video_core/regs.cpp @@ -2,8 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include -#include #include #include "common/common_types.h" @@ -474,19 +474,14 @@ static const std::pair register_names[] = { {0x2DD, "GPUREG_VSH_OPDESCS_DATA7"}, }; -std::string Regs::GetCommandName(int index) { - static std::unordered_map map; - - if (map.empty()) { - map.insert(std::begin(register_names), std::end(register_names)); - } - - // Return empty string if no match is found - auto it = map.find(index); - if (it != map.end()) { - return it->second; +const char* Regs::GetRegisterName(u16 index) { + auto found = std::lower_bound(std::begin(register_names), std::end(register_names), index, + [](auto p, auto i) { return p.first < i; }); + if (found->first == index) { + return found->second; } else { - return std::string(); + // Return empty string if no match is found + return ""; } } diff --git a/src/video_core/regs.h b/src/video_core/regs.h index e38ab43336..86826088b0 100644 --- a/src/video_core/regs.h +++ b/src/video_core/regs.h @@ -64,8 +64,8 @@ struct Regs { std::array reg_array; }; - // Map register indices to names readable by humans - static std::string GetCommandName(int index); + /// Map register indices to names readable by humans + static const char* GetRegisterName(u16 index); }; static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Regs struct has wrong size");