Should I do these class or structure vectors in C #

I am creating a geometry library in C # and I will need the following immutable types:

  • Vector2f (2 float - 8 bytes)
  • Vector2d (2 double - 16 bytes)
  • Vector3f (3 float - 12 bytes)
  • Vector3d (3 double - 24 bytes)
  • Vector4f (4 float - 16 bytes)
  • Vector4d (4 double - 32 bytes)

I am trying to determine whether to create their structures or classes. MSDN suggests using only structure if the size is no more than 16 bytes. This link seems to refer to 2005. Is 16 bytes the maximum recommended size?

I'm sure using structs for float vectors would be more efficient than using a class, but what should I do with double vectors? Do I have to make them structured to be consistent, or do I have to make their classes?

Updated: Everyone seems to agree that they should be structures. Thanks for the great answers.

+7
performance c # struct class size
source share
8 answers

The Microsoft XNA Framework uses structures for Vector2 / 3/4 . They contain fields of type float . I see nothing wrong with doing the same; the use of double fields should not matter much.

+8
source share

I would usually make types like these structures. Structures are likely to be pushed onto the stack, and if you use arrays with them, you can get a much nicer estimate than, say, using List <> objects. As long as you stay away from the operations that will put your vector classes in the box, the structure will usually be in a more efficient way.

+4
source share

Inevitable structures are a good choice. The size recommendation on MSDN is the weakest, 32 bytes are not very large.

The main argument will be that Vectors are used as simple (numeric) types, and implementation as a value type is the most suitable.

A good parallel is the new Complex and BigInteger structures in dotNet 4

+4
source share

Structures, definitely. What for? Well, this is part of the feeling, I think the vector just feels and behaves like a value.

Rico Mariani here gives some reasons why value types were chosen for certain types for the API (I don't think he's XNA, he talks about).

Although efficiency is a factor here, I don’t think about garbage collection, but more about data density, for example, says Rico. Say you also have a Vertex type that contains two Vector3 s: a Vector3 for normal and a Vector3 for world coordinates. If you made these types of classes, then having an array with 100 Vertex elements will consist of:

  • 100 * 8 bytes (8 bytes, I think, the overhead of a class in memory, 4 bytes for a type header and 4 bytes for something else, a GC handle?)
  • 100 * 4 bytes (for pointers in an array for Vertex elements)
  • 200 * 4 bytes (for pointers from each Vertex to two Vector3 elements)
  • 200 * 8 bytes (for 8-bit premiums that you pay for creating a Vector3 class)
  • 200 * 12 bytes (for actual payload 3 float per Vector3 )

6000 bytes (on a 32-bit system).

As a value type, it is just 200 * 12 bytes = 2400 bytes. Such a much more efficient space, not to mention a lower level of indirection when reading an array.

But taking up large space does not necessarily slow down, using the type of value incorrectly can be slower than making it a class, as I discovered . You definitely want to pass them ref as much as possible, do not copy them, but this is not suitable for all operations, so measure it. I think I remember that calculating a point product was slower when passing through ref , perhaps because it somehow prevented alignment by making IL larger. But don't take my word for it, just measure.

+3
source share

In my opinion, I would go with structures for this, since they will be allocated on the stack in most cases, this will reduce the pressure on the GC, so that your application runs more smoothly.

One piece of advice: your methods that opperate on structures should probably take arguments like the ref and out arguments, this will reduce the number of copies of data that occur when passing and returning structures.

+2
source share

Recently, we switched some types of mathematics (vec3, vec4, matrices, etc.) to using structures instead of classes and, as well as being slightly faster in tests, this also reduced the GC pressure (we had hundreds of megabytes of Vec3 distribution in a relatively short period of time) detected through the CLR profiler).

It is interesting to note that the implementation of XNA vectors and matrices is performed using structures. You can always follow the link if necessary, if you are also concerned about performance.

+2
source share

I assume you are worried about performance?

If they are immutable, they must be behaviorally identical. Just go with one, and if it turns out there is a perfection problem, switch it later.

If you are targeting .NET CE at all, you should probably consider using structures, since GC is not as efficient as a full .NET implementation.

0
source share

Structures (usually) are stored on the stack, which can make them more efficient, but probably not enough to make a noticeable difference.

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91

-one
source share

All Articles