Struct or class for a 4x4 Matrix object

I am trying to implement a simple 3D software engine. So I need a 4x4 matrix to convert. Each cell type of my matrix is ​​in the System.Double format, which means that the whole matrix will use 8 bytes * 16 cells = 128 bytes. I currently implemented this as a class. Also, this matrix is ​​an immutable type. So, now when I try to multiply a vector by this matrix, I am doing something like this: matrix.m11 * vector.X (etc.). It seems to me that to access the field m11, the program first needs to get the "matrix" reference value, and then check it for zero, and then find the M11 value, right? If I changed the class to structure, could my multiplication be much faster? I know that 128 bytes of struct are big, but if I write a method,which only works using ref / out keyowrds? Will it be a good chooose?
As I see in the SharpDX library, a matrix is ​​a structure. Should I change the class to structure in order to improve performance?

+4
source share
1 answer

I would not recommend using the class to store the double4x4 matrix for the 3D engine for the following reasons:

  • GC. , , GC, , World, View Projection , 3 ( + ~ 8-12 ) + 3 : , GC . , , , ...
  • . , . 3 , , . , , CPU
  • . 3D- 3D- , , , ( , ). float4x4: struct float4x4 , float4x4. fixed #. interop ( float4x4 )
  • . 3D-, , , . , GPU , . SSE CPU, 4 float , 2 . double , (, , " " )
  • : . , , . - . , . .

, :

  • valuetype , , . , .
  • , ref, .
+14

Source: https://habr.com/ru/post/1544652/


All Articles