From cf198be5175ce3e36d7a9c78fc9acccdd23abc4a Mon Sep 17 00:00:00 2001 From: praydog Date: Sun, 9 Jul 2023 14:20:00 -0700 Subject: [PATCH] SDK: Add caching to property lookups --- shared/sdk/UClass.cpp | 33 +++++++++++++++++++++++++++++++++ shared/sdk/UEngine.hpp | 1 + shared/sdk/UWorld.hpp | 11 +++++++++++ 3 files changed, 45 insertions(+) create mode 100644 shared/sdk/UWorld.hpp diff --git a/shared/sdk/UClass.cpp b/shared/sdk/UClass.cpp index 2066d2b6..04f005cf 100644 --- a/shared/sdk/UClass.cpp +++ b/shared/sdk/UClass.cpp @@ -1,3 +1,6 @@ +#include +#include + #include #include @@ -559,11 +562,26 @@ void UScriptStruct::update_offsets() { } FProperty* UStruct::find_property(std::wstring_view name) const { + static std::shared_mutex prop_mtx{}; + static std::unordered_map> prop_cache{}; + + { + std::shared_lock _{ prop_mtx }; + + if (auto it1 = prop_cache.find(this); it1 != prop_cache.end()) { + if (auto it2 = it1->second.find(name.data()); it2 != it1->second.end()) { + return it2->second; + } + } + } + for (auto super = this; super != nullptr; super = (UClass*)super->get_super_struct()) { for (auto child = super->get_child_properties(); child != nullptr; child = child->get_next()) { //SPDLOG_INFO("[UStruct] Checking child property {}", utility::narrow(child->get_field_name().to_string())); if (child->get_field_name().to_string() == name) { + std::unique_lock _{ prop_mtx }; + prop_cache[this][name.data()] = (sdk::FProperty*)child; return (FProperty*)child; } } @@ -573,11 +591,26 @@ FProperty* UStruct::find_property(std::wstring_view name) const { } UProperty* UStruct::find_uproperty(std::wstring_view name) const { + static std::shared_mutex prop_mtx{}; + static std::unordered_map> prop_cache{}; + + { + std::shared_lock _{ prop_mtx }; + + if (auto it1 = prop_cache.find(this); it1 != prop_cache.end()) { + if (auto it2 = it1->second.find(name.data()); it2 != it1->second.end()) { + return it2->second; + } + } + } + for (auto super = this; super != nullptr; super = (UClass*)super->get_super_struct()) { for (auto child = super->get_children(); child != nullptr; child = child->get_next()) { //SPDLOG_INFO("[UStruct] Checking child UProperty {}", utility::narrow(child->get_fname().to_string())); if (child->get_fname().to_string() == name) { + std::unique_lock _{ prop_mtx }; + prop_cache[this][name.data()] = (sdk::UProperty*)child; return (UProperty*)child; } } diff --git a/shared/sdk/UEngine.hpp b/shared/sdk/UEngine.hpp index e58ec5cf..dc252857 100644 --- a/shared/sdk/UEngine.hpp +++ b/shared/sdk/UEngine.hpp @@ -1,6 +1,7 @@ #pragma once #include "UObject.hpp" +#include "UWorld.hpp" namespace sdk { class UWorld; diff --git a/shared/sdk/UWorld.hpp b/shared/sdk/UWorld.hpp new file mode 100644 index 00000000..a527068c --- /dev/null +++ b/shared/sdk/UWorld.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "UObject.hpp" + +namespace sdk { +class UWorld : public UObject { +public: + +private: +}; +} \ No newline at end of file