0

According to the docs, unities Vector3 are value types. Also according to the docs "By default, on assignment, passing an argument to a method, and returning a method result, variable values are copied. In the case of value-type variables, the corresponding type instances are copied".

Not an expert in performance, pointers and so on, but I think its fact that passing arguments by reference instead by value to avoid making copies of instances is a good practice regarding performance. Indeed that's why most of the types in c# are reference types.

Same for the RaycastHit struct.

From as far as I researched classes and structures have the following basic differences

  • classes are reference types and structs are value types
  • structures do not support inheritance
  • structures cannot have default constructor

For my none of this would justify giving up the performance of ref types benefit,so, mainly Vector3 where in any game there are going to be vectors passed around as arguments so copied all over the app,what is the reason of it being struct type?

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • You're missing the *costs* of reference types, in particular the increased work the GC to do in order to free that memory back up again, which matters with latency-sensitive things like games. Copying a few bytes around is practically free: the [guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/choosing-between-class-and-struct) put the limit at 16 bytes. For larger structs, there are mechanisms for passing around references to them: `ref` and `out` have existed forever, but `in` and `readonly ` structs were introduced for things like Unity – canton7 Dec 23 '21 at 10:04
  • @canton7 none of the Unity structs are readonly though ;) – derHugo Dec 23 '21 at 11:02
  • @derHugo They were still introduced for latency-sensitive applications such as Unity – canton7 Dec 23 '21 at 11:02
  • Another difference missing is [heap vs stack allocation](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – derHugo Dec 23 '21 at 11:05

1 Answers1

2

When comparing classes (reference types) vs. structs (value types) in regards of performance, we can't say one is more performant to the other. The answer is: "It depends".

Classes are allocated on the heap, and are garbage collected, which is an overhead compared to structs.

Structs, as you mentioned, are copied when being passed, which is also an overhead compared to classes (this is not much of overhead of the size of the struct is small, that's why the design guidelines mentions a size limit when choosing structs).

Some general rules per design guidelines are:

✔️ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

❌ AVOID defining a struct unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.).
  • It has an instance size under 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

Practically, you should benchmark for your specific scenario when you're in doubt.

Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • 1
    Also, struct cannot use inheritance so that's another reason to discard it when designing. – fafase Dec 23 '21 at 10:13
  • I dont think so, because you could use a [sealed class](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed) – rustyBucketBay Dec 23 '21 at 10:25
  • 3
    @rustyBucketBay What fafase is referring to is, you *can* design a non-sealed class for inheritance. This is not an option at all with structs. So it's something to consider. – Youssef13 Dec 23 '21 at 10:31
  • [heap vs stack allocation](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) is actually a lot more then just the GC argument – derHugo Dec 23 '21 at 11:07
  • My point just was that taking unity's `Vector3` class as an example, as it is a built-in the engine class, making it sealed would have the same effect as being a struct regarding inheritance, meaning that there would be no chance to inherit from it. However thanks for spotting that out @Youssef. – rustyBucketBay Dec 23 '21 at 11:32
  • very useful resorce and point Mr@derHugo – rustyBucketBay Dec 23 '21 at 11:33