Search Unity

Is there a utility method for faster copying of NativeArray?

Discussion in 'Entity Component System' started by davenirline, Aug 5, 2018.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    Is there a better method other looping through arrays and copying each element?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Where you copying too?
    What you try to do?
     
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    I'm trying to expand an existing array. I'm copying to an array with bigger length.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    I have stumbled upon same problem at some point.
    Didn't have to comback to it, as I change approach.
    But questions still stays.
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    We have nativeArray.CopyTo(nativeArray) but I am not sure of the behaviour where the target is larger/smaller than the current one. One thing for sure it use UnsafeUtility.MemCpy which should be faster than manual looping.

    But you can also try NativeList instead. Each add might be slower than directly assigning to NativeArray but maybe the expansion cost is cheaper than making a new bigger NativeArray depending on your situation. The expansion rule is the initial capacity will *2 when you go over it.
     
  6. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    Can you share the code of NativeArray.CopyTo()?
     
  7. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Isn't it just like this :

    Code (CSharp):
    1.  
    2.         NativeArray<int> one = new NativeArray<int>(5, Allocator.Temp);
    3.         NativeArray<int> two = new NativeArray<int>(10, Allocator.Temp);
    4.         one[0] = 10;
    5.         one[1] = 10;
    6.         one.CopyTo(two);
    7.         Assert.That(two[0], Is.EqualTo(10));
    8.  
     
  8. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    It doesn't work. They need to have the same length.
     
  9. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    You can use NativeSlice

    Code (CSharp):
    1.             var array1 = new NativeArray<int>(100, Allocator.Temp);
    2.             var array2 = new NativeArray<int>(50, Allocator.Temp);
    3.        
    4.             var slice1 = new NativeSlice<int>(array1, 25, 50);
    5.             var slice2 = new NativeSlice<int>(array2);
    6.        
    7.             slice1.CopyFrom(slice2);
    8.        
    9.             // array1 index from 25 -> 75 is now equal to array 2
    It's very fast.
    To create a slice it just grabs the pointer from the NativeArray and offsets it.
    CopyFrom/CopyTo uses MemCopy.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    18.3 has CopyTo implemented via memcpy for all codepaths including copy to .NET arrays.
     
  11. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    This works. Thanks!
     
  12. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    Hey Joachim. Is this available in the beta? I need a fast copy from NativeArray to .NET array.
     
  13. diegoossal

    diegoossal

    Unity Technologies

    Joined:
    May 10, 2018
    Posts:
    4
    If you want to copy a range from a managed array to a NativeArray you can use something like this:

    Code (CSharp):
    1. public unsafe NativeArray<T> CopyData<T>(T[] array, int offset, int count, Allocator allocator) where T : unmanaged
    2. {
    3.     var dst = new NativeArray<T>(count, allocator);
    4.     fixed (T * srcPtr = array)
    5.     {
    6.         var dstPtr = dst.GetUnsafePtr();
    7.         UnsafeUtility.MemCpy(dstPtr,srcPtr + offset, sizeof(T) * count);
    8.     }
    9.     return dst;
    10. }
     
    Josh707, MaxEden, TigerHix and 7 others like this.