Skip to content

Commit

Permalink
SDK: Add caching to property lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 9, 2023
1 parent 9bf6804 commit cf198be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions shared/sdk/UClass.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <shared_mutex>
#include <unordered_map>

#include <windows.h>
#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -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<const sdk::UStruct*, std::unordered_map<std::wstring, sdk::FProperty*>> 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;
}
}
Expand All @@ -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<const sdk::UStruct*, std::unordered_map<std::wstring, sdk::UProperty*>> 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;
}
}
Expand Down
1 change: 1 addition & 0 deletions shared/sdk/UEngine.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "UObject.hpp"
#include "UWorld.hpp"

namespace sdk {
class UWorld;
Expand Down
11 changes: 11 additions & 0 deletions shared/sdk/UWorld.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "UObject.hpp"

namespace sdk {
class UWorld : public UObject {
public:

private:
};
}

0 comments on commit cf198be

Please sign in to comment.