When using the CopyTo function to copy data from a NativeSlice or a NativeArray to a managed array there is internal validation to make sure the managed array has the same size.
Would it be possible to allow copying if the managed array is larger than the native array.
I have a use case where I have a pre allocated managed array that in some cases are larger than the nativearray produced by jobs and I do not want to allocate this again and produce GC
I do this now with a custom CopyTo function and MemCopy but I would like to get the unsafe code out of the project.
Yes. I use slices. But if I have a slice of 900 items and want to copy this to a pre allocated array of 1023 (for instanced render api) it will not do that since the size does not match. I would like the CopyTo to just write to the first 900 items in the array.
That would be much slower than a single memory copy.
What I would like is basically this. Where it allows for copying the slice to a array that is larger than the slice. The CopyTo needs the size to be the same.
This code works good but requires the unsafe code checkbox to be enabled. Would be nice if the built-in copyTo could do the same.
public static unsafe void CopyToFast<T>(
this NativeSlice<T> nativeSlice,
T[] array)
where T : struct
{
if (array == null)
{
throw new NullReferenceException(nameof(array) + " is null");
}
int nativeArrayLength = nativeSlice.Length;
if (array.Length < nativeArrayLength)
{
throw new IndexOutOfRangeException(
nameof(array) + " is shorter than " + nameof(nativeSlice));
}
int byteLength = nativeSlice.Length * UnsafeUtility.SizeOf<T>();
void* managedBuffer = UnsafeUtility.AddressOf(ref array[0]);
void* nativeBuffer = nativeSlice.GetUnsafePtr();
UnsafeUtility.MemCpy(managedBuffer, nativeBuffer, byteLength);
}
The current NativeArray.CopyTo(T[ ]) is not a single memory copy, it copies each element to the destination individually. Even if they adjusted the method to allow copying to an array of a larger size, your method would still be faster (in most cases) since it is a single memory copy. That said, it would definitely be nice to see something like this in the Unity library.
Just hit upon this problem. Another unity3d YAGNI.
I DEMAND functionality that let me copy NativeArray data to another NativeArray of different size.
It is just a common sense.
The reason GetSubArray is better is because NativeArray is faster than NativeSlice since the compiler can assume there is not a dynamic offset between elements. NativeSlice should only be used if it is actually necessary to have dynamic strides.