Merge branch 'arraytype_cache' of https://github.com/GoaLitiuM/FlaxEngine into GoaLitiuM-arraytype_cache

This commit is contained in:
Wojtek Figat
2023-08-14 15:36:27 +02:00
3 changed files with 23 additions and 12 deletions

View File

@@ -91,18 +91,19 @@ namespace FlaxEngine.Interop
internal void Allocate<T>(int length) where T : unmanaged
{
_length = length;
_arrayType = typeof(T[]);
_elementType = typeof(T);
_elementSize = Unsafe.SizeOf<T>();
// Try to reuse existing allocated buffer
if (length * Unsafe.SizeOf<T>() > _unmanagedAllocationSize)
if (length * _elementSize > _unmanagedAllocationSize)
{
if (_unmanagedAllocationSize > 0)
NativeInterop.NativeFree(_unmanagedData.ToPointer());
_unmanagedData = (IntPtr)NativeInterop.NativeAlloc(length, Unsafe.SizeOf<T>());
_unmanagedAllocationSize = Unsafe.SizeOf<T>() * length;
_unmanagedData = (IntPtr)NativeInterop.NativeAlloc(length, _elementSize);
_unmanagedAllocationSize = _elementSize * length;
}
_length = length;
_arrayType = typeof(T).MakeArrayType();
_elementType = typeof(T);
_elementSize = Unsafe.SizeOf<T>();
}
private ManagedArray()
@@ -112,12 +113,12 @@ namespace FlaxEngine.Interop
private ManagedArray(IntPtr ptr, int length, Type arrayType, Type elementType)
{
Assert.IsTrue(arrayType.IsArray);
_elementType = elementType;
_elementSize = NativeInterop.GetTypeSize(elementType);
_unmanagedData = ptr;
_unmanagedAllocationSize = Marshal.SizeOf(elementType) * length;
_unmanagedAllocationSize = _elementSize * length;
_length = length;
_arrayType = arrayType;
_elementType = elementType;
_elementSize = NativeInterop.GetTypeSize(_elementType);
}
~ManagedArray()

View File

@@ -526,7 +526,7 @@ namespace FlaxEngine.Interop
{
Type elementType = Unsafe.As<Type>(typeHandle.Target);
Type marshalledType = ArrayFactory.GetMarshalledType(elementType);
Type arrayType = elementType.MakeArrayType();
Type arrayType = ArrayFactory.GetArrayType(elementType);
if (marshalledType.IsValueType)
{
ManagedArray managedArray = ManagedArray.AllocateNewArray((int)size, arrayType, marshalledType);
@@ -544,7 +544,7 @@ namespace FlaxEngine.Interop
internal static ManagedHandle GetArrayTypeFromElementType(ManagedHandle elementTypeHandle)
{
Type elementType = Unsafe.As<Type>(elementTypeHandle.Target);
Type classType = elementType.MakeArrayType();
Type classType = ArrayFactory.GetArrayType(elementType);
return GetTypeGCHandle(classType);
}

View File

@@ -968,6 +968,7 @@ namespace FlaxEngine.Interop
private delegate Array CreateArrayDelegate(long size);
private static ConcurrentDictionary<Type, Type> marshalledTypes = new ConcurrentDictionary<Type, Type>(1, 3);
private static ConcurrentDictionary<Type, Type> arrayTypes = new ConcurrentDictionary<Type, Type>(1, 3);
private static ConcurrentDictionary<Type, CreateArrayDelegate> createArrayDelegates = new ConcurrentDictionary<Type, CreateArrayDelegate>(1, 3);
internal static Type GetMarshalledType(Type elementType)
@@ -987,6 +988,15 @@ namespace FlaxEngine.Interop
return marshalledTypes.GetOrAdd(elementType, Factory);
}
internal static Type GetArrayType(Type elementType)
{
static Type Factory(Type type) => type.MakeArrayType();
if (arrayTypes.TryGetValue(elementType, out var arrayType))
return arrayType;
return arrayTypes.GetOrAdd(elementType, Factory);
}
internal static Array CreateArray(Type type, long size)
{
static CreateArrayDelegate Factory(Type type)