Optimize calling Object.FindObject from C#

This commit is contained in:
Wojtek Figat
2021-08-19 13:05:59 +02:00
parent 39a4dab81f
commit a1bf4819cb
2 changed files with 36 additions and 5 deletions

View File

@@ -7,6 +7,8 @@
#include "Engine/Level/Actor.h"
#include "Engine/Core/Log.h"
#include "Engine/Utilities/StringConverter.h"
#include "Engine/Content/Asset.h"
#include "Engine/Content/Content.h"
#include "ManagedCLR/MAssembly.h"
#include "ManagedCLR/MClass.h"
#include "ManagedCLR/MUtils.h"
@@ -556,10 +558,35 @@ public:
static MonoObject* FindObject(Guid* id, MonoReflectionType* type)
{
ScriptingObject* obj = Scripting::FindObject(*id);
if (obj && !obj->Is(MUtils::GetClass(type)))
obj = nullptr;
return obj ? obj->GetOrCreateManagedInstance() : nullptr;
if (!id->IsValid())
return nullptr;
#if PLATFORM_LINUX
// Cannot enter GC unsafe region if the thread is not attached
MCore::AttachThread();
#endif
auto klass = MUtils::GetClass(type);
ScriptingObject* obj = Scripting::TryFindObject(*id);
if (!obj)
{
if (!klass || klass == ScriptingObject::GetStaticClass()->GetNative() || mono_class_is_subclass_of(klass, Asset::GetStaticClass()->GetNative(), false) != 0)
{
obj = Content::LoadAsync<Asset>(*id);
}
}
if (obj)
{
if (klass && !obj->Is(klass))
{
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}.", *id, String(obj->GetType().Fullname), String(MUtils::GetClassFullname(klass)));
return nullptr;
}
return obj->GetOrCreateManagedInstance();
}
if (klass)
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}.", *id, String(MUtils::GetClassFullname(klass)));
else
LOG(Warning, "Unable to find scripting object with ID={0}", *id);
return nullptr;
}
static MonoObject* TryFindObject(Guid* id, MonoReflectionType* type)