Optimize managed memory allocations in Editor profiler
This commit is contained in:
@@ -21,6 +21,20 @@ namespace FlaxEngine
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the assets (loaded or during load).
|
||||
/// </summary>
|
||||
/// <param name="buffer">Output buffer to fill with asset pointers. Can be provided by a user to avoid memory allocation. Buffer might be larger than actual list size. Use <paramref name="count"/> for actual item count.></param>
|
||||
/// <param name="count">Amount of valid items inside <paramref name="buffer"/>.</param>
|
||||
public static void GetAssets(ref Asset[] buffer, out int count)
|
||||
{
|
||||
count = 0;
|
||||
IntPtr ptr = Internal_GetAssetsInternal();
|
||||
ManagedArray array = Unsafe.As<ManagedArray>(ManagedHandle.FromIntPtr(ptr).Target);
|
||||
buffer = NativeInterop.GCHandleArrayToManagedArray<Asset>(array, buffer);
|
||||
count = buffer.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads asset to the Content Pool and holds it until it won't be referenced by any object. Returns null if asset is missing. Actual asset data loading is performed on a other thread in async.
|
||||
/// </summary>
|
||||
|
||||
@@ -195,17 +195,19 @@ namespace FlaxEngine.Interop
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Array element type.</typeparam>
|
||||
/// <param name="ptrArray">Input array.</param>
|
||||
/// <param name="buffer">Cached memory allocation buffer to use for the result (if size fits).</param>
|
||||
/// <returns>Output array.</returns>
|
||||
public static T[] GCHandleArrayToManagedArray<T>(ManagedArray ptrArray) where T : class
|
||||
public static T[] GCHandleArrayToManagedArray<T>(ManagedArray ptrArray, T[] buffer = null) where T : class
|
||||
{
|
||||
Span<IntPtr> span = ptrArray.ToSpan<IntPtr>();
|
||||
T[] managedArray = new T[ptrArray.Length];
|
||||
for (int i = 0; i < managedArray.Length; i++)
|
||||
if (buffer == null || buffer.Length < ptrArray.Length)
|
||||
buffer = new T[ptrArray.Length];
|
||||
for (int i = 0; i < ptrArray.Length; i++)
|
||||
{
|
||||
IntPtr ptr = span[i];
|
||||
managedArray[i] = ptr != IntPtr.Zero ? (T)ManagedHandle.FromIntPtr(ptr).Target : default;
|
||||
buffer[i] = ptr != IntPtr.Zero ? (T)ManagedHandle.FromIntPtr(ptr).Target : default;
|
||||
}
|
||||
return managedArray;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,6 +20,20 @@ namespace FlaxEngine
|
||||
return NativeInterop.GCHandleArrayToManagedArray<GPUResource>(array);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list with all active GPU resources.
|
||||
/// </summary>
|
||||
/// <param name="buffer">Output buffer to fill with resource pointers. Can be provided by a user to avoid memory allocation. Buffer might be larger than actual list size. Use <paramref name="count"/> for actual item count.></param>
|
||||
/// <param name="count">Amount of valid items inside <paramref name="buffer"/>.</param>
|
||||
public void GetResources(ref GPUResource[] buffer, out int count)
|
||||
{
|
||||
count = 0;
|
||||
IntPtr ptr = Internal_GetResourcesInternal(__unmanagedPtr);
|
||||
ManagedArray array = Unsafe.As<ManagedArray>(ManagedHandle.FromIntPtr(ptr).Target);
|
||||
buffer = NativeInterop.GCHandleArrayToManagedArray<GPUResource>(array, buffer);
|
||||
count = buffer.Length;
|
||||
}
|
||||
}
|
||||
|
||||
partial struct GPUBufferDescription : IEquatable<GPUBufferDescription>
|
||||
|
||||
Reference in New Issue
Block a user