From 25bcddf3a557250cb432c91deb302aaf76ec6d16 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Mon, 25 Dec 2023 05:14:34 -0800 Subject: [PATCH] FEXCore: Removes context wide and map lookup While locking a shared_lock and doing an empty table lookup is fairly fast, just remove them from the hot path entirely if no custom IR handlers are installed. This is only used for our IRLoader, which is losing its importance significantly and should probably be removed anyway. --- FEXCore/Source/Interface/Context/Context.h | 1 + FEXCore/Source/Interface/Core/Core.cpp | 26 +++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h index e552e99081..66c296fa10 100644 --- a/FEXCore/Source/Interface/Context/Context.h +++ b/FEXCore/Source/Interface/Context/Context.h @@ -389,6 +389,7 @@ namespace FEXCore::Context { FEX_CONFIG_OPT(AppFilename, APP_FILENAME); std::shared_mutex CustomIRMutex; + std::atomic HasCustomIRHandlers{}; fextl::unordered_map> CustomIRHandlers; FEXCore::CPU::DispatcherConfig DispatcherConfig; }; diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp index 83eec77879..e3bc6ddb48 100644 --- a/FEXCore/Source/Interface/Core/Core.cpp +++ b/FEXCore/Source/Interface/Core/Core.cpp @@ -513,17 +513,20 @@ namespace FEXCore::Context { uint64_t TotalInstructions {0}; uint64_t TotalInstructionsLength {0}; + bool HasCustomIR{}; + + if (HasCustomIRHandlers.load(std::memory_order_relaxed)) { + std::shared_lock lk(CustomIRMutex); + auto Handler = CustomIRHandlers.find(GuestRIP); + if (Handler != CustomIRHandlers.end()) { + TotalInstructions = 1; + TotalInstructionsLength = 1; + std::get<0>(Handler->second)(GuestRIP, Thread->OpDispatcher.get()); + HasCustomIR = true; + } + } - std::shared_lock lk(CustomIRMutex); - - auto Handler = CustomIRHandlers.find(GuestRIP); - if (Handler != CustomIRHandlers.end()) { - TotalInstructions = 1; - TotalInstructionsLength = 1; - std::get<0>(Handler->second)(GuestRIP, Thread->OpDispatcher.get()); - lk.unlock(); - } else { - lk.unlock(); + if (!HasCustomIR) { uint8_t const *GuestCode{}; GuestCode = reinterpret_cast(GuestRIP); @@ -984,6 +987,7 @@ namespace FEXCore::Context { std::unique_lock lk(CustomIRMutex); auto InsertedIterator = CustomIRHandlers.emplace(Entrypoint, std::tuple(Handler, Creator, Data)); + HasCustomIRHandlers = true; if (!InsertedIterator.second) { const auto &[fn, Creator, Data] = InsertedIterator.first->second; @@ -1002,6 +1006,8 @@ namespace FEXCore::Context { InvalidateGuestCodeRange(nullptr, Entrypoint, 1, [this](uint64_t Entrypoint, uint64_t) { CustomIRHandlers.erase(Entrypoint); }); + + HasCustomIRHandlers = !CustomIRHandlers.empty(); } uint64_t HandleSyscall(FEXCore::HLE::SyscallHandler *Handler, FEXCore::Core::CpuStateFrame *Frame, FEXCore::HLE::SyscallArguments *Args) {