Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster/cleaner way to copy structs to an array in C#?

Tags:

arrays

c#

memory

I have a float4x4 struct which simply containts 16 floats:

struct float4x4
{
        public float M11; public float M12; public float M13; public float M14;
        public float M21; public float M22; public float M23; public float M24;
        public float M31; public float M32; public float M33; public float M34;
        public float M41; public float M42; public float M43; public float M44;
}

I want to copy an array of these structs into a big array of floats. This is as far as I know a 1:1 copy of a chunk of memory

What I do know is rather ugly, and not that fast:

        int n = 0;
        for (int i = 0; i < Length; i++)
        {
            array[n++] = value[i].M11;
            array[n++] = value[i].M12;
            array[n++] = value[i].M13;
            array[n++] = value[i].M14;

            array[n++] = value[i].M21;
            array[n++] = value[i].M22;
            array[n++] = value[i].M23;
            array[n++] = value[i].M24;

            array[n++] = value[i].M31;
            array[n++] = value[i].M32;
            array[n++] = value[i].M33;
            array[n++] = value[i].M34;

            array[n++] = value[i].M41;
            array[n++] = value[i].M42;
            array[n++] = value[i].M43;
            array[n++] = value[i].M44;
        }

If I was using a lower level language, I would simply use memcpy, what can I use as an equivilant in C#?

like image 329
Hannesh Avatar asked Sep 17 '11 10:09

Hannesh


People also ask

Which is faster array or structure?

Array traversal and searching is easy and fast. Structure traversal and searching is complex and slow.

Can we copy struct in C?

In C/C++, we can assign a struct (or class in C++ only) variable to another variable of same type. When we assign a struct variable to another, all members of the variable are copied to the other struct variable.

Can struct be copied?

A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct.

Can you set one struct equal to another?

Yes, you can assign one instance of a struct to another using a simple assignment statement. In the case of non-pointer or non pointer containing struct members, assignment means copy. In the case of pointer struct members, assignment means pointer will point to the same address of the other pointer.


1 Answers

You can't use a memory copy, as you can't blindly assume anything about how the members are stored inside the structure. The JIT compiler could decide to store them with a few bytes of padding between them, if that would make it faster.

Your structure is way too large for the recommended size of a structure anyway, so you should make it a class. Also, structures should not be mutable, which also talks for a class.

If you store the properties in an array internally, you can use that for copying the values:

class float4x4 {

  public float[] Values { get; private set; } 

  public float4x4() {
    Values = new float[16];
  }

  public float M11 { get { return Values[0]; } set { Values[0] = value; } }
  public float M12 { get { return Values[0]; } set { Values[0] = value; } }
  ...
  public float M43 { get { return Values[14]; } set { Values[14] = value; } }
  public float M44 { get { return Values[15]; } set { Values[15] = value; } }

}

Now you can get the Values array from the object and copy to the array using the Array.CopyTo method:

int n = 0;
foreach (float4x4 v in values) {
  v.Values.CopyTo(array, n);
  n += 16;
}
like image 94
Guffa Avatar answered Sep 19 '22 19:09

Guffa