From 9538705b215f688a847650a2841b1f1b31a9e163 Mon Sep 17 00:00:00 2001 From: Pocakking <42542447+Pocakking@users.noreply.github.com> Date: Mon, 29 Jul 2019 23:19:21 +0200 Subject: [PATCH] Cache native handlers --- BigBaseV2/src/invoker.cpp | 20 +++++++++----------- BigBaseV2/src/invoker.hpp | 3 ++- BigBaseV2/src/script_mgr.cpp | 2 ++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/BigBaseV2/src/invoker.cpp b/BigBaseV2/src/invoker.cpp index 7030ec78..02001a9e 100644 --- a/BigBaseV2/src/invoker.cpp +++ b/BigBaseV2/src/invoker.cpp @@ -12,18 +12,15 @@ namespace big m_args = &m_arg_stack[0]; } - bool native_invoker::map_native(rage::scrNativeHash *hash) + void native_invoker::cache_handlers() { - for (auto const &mapping : g_crossmap) + for (const rage::scrNativeMapping &mapping : g_crossmap) { - if (mapping.first == *hash) - { - *hash = mapping.second; - return true; - } - } + rage::scrNativeHandler handler = g_pointers->m_get_native_handler( + g_pointers->m_native_registration_table, mapping.second); - return false; + m_handler_cache.emplace(mapping.first, handler); + } } void native_invoker::begin_call() @@ -33,9 +30,10 @@ namespace big void native_invoker::end_call(rage::scrNativeHash hash) { - map_native(&hash); - if (auto handler = g_pointers->m_get_native_handler(g_pointers->m_native_registration_table, hash)) + if (auto it = m_handler_cache.find(hash); it != m_handler_cache.end()) { + rage::scrNativeHandler handler = it->second; + __try { handler(&m_call_context); diff --git a/BigBaseV2/src/invoker.hpp b/BigBaseV2/src/invoker.hpp index 819991ef..2a43576e 100644 --- a/BigBaseV2/src/invoker.hpp +++ b/BigBaseV2/src/invoker.hpp @@ -19,7 +19,7 @@ namespace big explicit native_invoker() = default; ~native_invoker() = default; - bool map_native(rage::scrNativeHash *hash); + void cache_handlers(); void begin_call(); void end_call(rage::scrNativeHash hash); @@ -37,6 +37,7 @@ namespace big } private: native_call_context m_call_context; + std::unordered_map m_handler_cache; }; inline native_invoker g_native_invoker; diff --git a/BigBaseV2/src/script_mgr.cpp b/BigBaseV2/src/script_mgr.cpp index 4a007331..bafc88ad 100644 --- a/BigBaseV2/src/script_mgr.cpp +++ b/BigBaseV2/src/script_mgr.cpp @@ -3,6 +3,7 @@ #include "gta/script_thread.hpp" #include "gta/tls_context.hpp" #include "gta_util.hpp" +#include "invoker.hpp" #include "pointers.hpp" #include "script_mgr.hpp" @@ -28,6 +29,7 @@ namespace big void script_mgr::tick_internal() { static bool ensure_main_fiber = (ConvertThreadToFiber(nullptr), true); + static bool ensure_native_handlers = (g_native_invoker.cache_handlers(), true); std::lock_guard lock(m_mutex); for (auto const &script : m_scripts)