Avoid pre-allocating custom attributes for managed types

This commit is contained in:
2022-12-11 13:54:12 +02:00
parent 84f8e3a4b4
commit b9f11298e8
4 changed files with 57 additions and 4 deletions

View File

@@ -1365,6 +1365,18 @@ namespace FlaxEngine
*classAttributesCount = attributeTypes.Length;
}
[UnmanagedCallersOnly]
internal static IntPtr GetCustomAttribute(IntPtr typeHandle, IntPtr attribHandle)
{
Type type = (Type)GCHandle.FromIntPtr(typeHandle).Target;
Type attribType = (Type)GCHandle.FromIntPtr(attribHandle).Target;
object attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x.GetType() == attribType);
if (attrib != null)
return GCHandle.ToIntPtr(GCHandle.Alloc(attrib, GCHandleType.Weak));
return IntPtr.Zero;
}
[UnmanagedCallersOnly]
internal static void GetClassInterfaces(IntPtr typeHandle, IntPtr* classInterfaces, int* classInterfacesCount)
{

View File

@@ -4,6 +4,7 @@
#include <ThirdParty/mono-2.0/mono/metadata/blob.h>
#include "Engine/Core/Types/String.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Scripting/Types.h"
#if defined(_WIN32)
@@ -46,4 +47,9 @@ public:
static gchandle NewGCHandleWeakref(void* obj, bool track_resurrection);
static void* GetGCHandleTarget(const gchandle& gchandle);
static void FreeGCHandle(const gchandle& gchandle);
static bool HasCustomAttribute(void* klass, void* attribClass);
static bool HasCustomAttribute(void* klass);
static void* GetCustomAttribute(void* klass, void* attribClass);
static Array<void*> GetCustomAttributes(void* klass);
};

View File

@@ -587,6 +587,30 @@ const char* CoreCLR::GetClassFullname(void* klass)
return ((CoreCLRClass*)klass)->GetFullname().Get();
}
bool CoreCLR::HasCustomAttribute(void* klass, void* attribClass)
{
return CoreCLR::GetCustomAttribute(klass, attribClass) != nullptr;
}
bool CoreCLR::HasCustomAttribute(void* klass)
{
return CoreCLR::GetCustomAttribute(klass, nullptr) != nullptr;
}
void* CoreCLR::GetCustomAttribute(void* klass, void* attribClass)
{
return CoreCLR::CallStaticMethodInternal<void*, void*, void*>(TEXT("GetCustomAttribute"), ((CoreCLRClass*)klass)->GetTypeHandle(), ((CoreCLRClass*)attribClass)->GetTypeHandle());
}
Array<void*> CoreCLR::GetCustomAttributes(void* klass)
{
Array<CoreCLRCustomAttribute*> attrib = ((CoreCLRClass*)klass)->GetCustomAttributes();
Array<void*> attributes;
attributes.Resize(attrib.Count(), false);
for (int i = 0; i < attrib.Count(); i++)
attributes.Add(attrib[i]->GetHandle());
return attributes;
}
/*
* loader.h
*/

View File

@@ -14,6 +14,9 @@
#include <ThirdParty/mono-2.0/mono/metadata/attrdefs.h>
#define GET_CUSTOM_ATTR() (MonoCustomAttrInfo*)(_attrInfo ? _attrInfo : _attrInfo = mono_custom_attrs_from_class(_monoClass))
#endif
#if USE_NETCORE
#include "Engine/Scripting/DotNet/CoreCLR.h"
#endif
#if USE_MONO
MClass::MClass(const MAssembly* parentAssembly, MonoClass* monoClass, const MString& fullname)
@@ -375,7 +378,9 @@ MObject* MClass::CreateInstance(void** params, uint32 numParams)
bool MClass::HasAttribute(const MClass* monoClass) const
{
#if USE_MONO
#if USE_NETCORE
return CoreCLR::HasCustomAttribute(_monoClass, monoClass->GetNative());
#elif USE_MONO
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
return attrInfo != nullptr && mono_custom_attrs_has_attr(attrInfo, monoClass->GetNative()) != 0;
#else
@@ -385,7 +390,9 @@ bool MClass::HasAttribute(const MClass* monoClass) const
bool MClass::HasAttribute() const
{
#if USE_MONO
#if USE_NETCORE
return CoreCLR::HasCustomAttribute(_monoClass);
#elif USE_MONO
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
return attrInfo && attrInfo->num_attrs > 0;
#else
@@ -395,7 +402,9 @@ bool MClass::HasAttribute() const
MObject* MClass::GetAttribute(const MClass* monoClass) const
{
#if USE_MONO
#if USE_NETCORE
return (MObject*)CoreCLR::GetCustomAttribute(_monoClass, monoClass->GetNative());
#elif USE_MONO
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
return attrInfo ? mono_custom_attrs_get_attr(attrInfo, monoClass->GetNative()) : nullptr;
#else
@@ -409,7 +418,9 @@ const Array<MObject*>& MClass::GetAttributes()
return _attributes;
_hasCachedAttributes = true;
#if USE_MONO
#if USE_NETCORE
_attributes = *(Array<MObject*>*)(&CoreCLR::GetCustomAttributes(_monoClass));
#elif USE_MONO
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
if (attrInfo == nullptr)
return _attributes;