Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable class vs struct

Tags:

The following are the only ways classes are different from structs in C# (please correct me if I'm wrong):

  • Class variables are references, while struct variables are values, therefore the entire value of struct is copied in assignments and parameter passes
  • Class variables are pointers stored on stack that point to the memory on heap, while struct variables are on stored heap as values

Suppose I have an immutable struct, that is struct with fields that cannot be modified once initialized. Each time I pass this struct as a parameter or use in assignments, the value would be copied and stored on stack.

Then suppose I make this immutable struct to be an immutable class. The single instance of this class would be created once, and only the reference to the class would be copied in assignments and parameter passes.

If the object was mutable, the behavior in these two cases would be different: when one would change the object, in the first case the copy of the struct would be modified, while in the second case the original object would be changed. However, in both cases the object is immutable, therefore there is no difference whether this is actually a class or a struct for the user of this object.

Since copying reference is cheaper than copying struct, why would one use an immutable struct?

Also, since mutable structs are evil, it looks like there is no reason to use structs at all.

Where am I wrong?

like image 412
Georgii Oleinikov Avatar asked Jan 03 '13 21:01

Georgii Oleinikov


People also ask

Are structs immutable?

A struct type is not immutable. Yes, strings are. Making your own type immutable is easy, simply don't provide a default constructor, make all fields private and define no methods or properties that change a field value. Have a method that should mutate the object return a new object instead.

What does it mean for a struct to be immutable?

Struct / tuple values are only immutable if they are "let" vs "var". OTOH, enum values are more like true immutable values of functional languages - you can not change enum's associated value, you need a new value with a new associated value to do that.

Are structs immutable go?

After an object (or struct) is created, it can never be changed. It's immutable.

When should I use a struct instead of a class?

In classes, two variables can contain the reference of the same object and any operation on one variable can affect another variable. In this way, struct should be used only when you are sure that, It logically represents a single value, like primitive types (int, double, etc.). It is immutable.


2 Answers

Since copying reference is cheaper than copying struct, why would one use an immutable struct?

This isn't always true. Copying a reference is going to be 8 bytes on a 64bit OS, which is potentially larger than many structs.

Also note that creation of the class is likely more expensive. Creating a struct is often done completely on the stack (though there are many exceptions), which is very fast. Creating a class requires creating the object handle (for the garbage collector), creating the reference on the stack, and tracking the object's lifetime. This can add GC pressure, which also has a real cost.

That being said, creating a large immutable struct is likely not a good idea, which is part of why the Guidelines for choosing between Classes and Structures recommend always using a class if your struct will be more than 16 bytes, if it will be boxed, and other issues that make the difference smaller.

That being said, I often base my decision more on the intended usage and meaning of the type in question. Value types should be used to refer to a single value (again, refer to guidelines), and often have a semantic meaning and expected usage different than classes. This is often just as important as the performance characteristics when making the choice between class or struct.

like image 135
Reed Copsey Avatar answered Oct 30 '22 17:10

Reed Copsey


Reed's answer is quite good but just to add a few extra points:

please correct me if I'm wrong

You are basically on the right track here. You've made the common error of confusing variables with values. Variables are storage locations; values are stored in variables. And you are flirting with the commonly-stated myth that "value types go on the stack"; rather, variables go on either short-term or long-term storage, because variables are storage locations. Whether a variable goes on short or long term storage depends on its known lifetime, not its type.

But all of that is not particularly relevant to your question, which boils down to asking for a refutation of this syllogism:

  • Mutable structs are evil.
  • Reference copying is cheaper than struct copying, so immutable structs are always worse.
  • Therefore, there is never any use for structs.

We can refute the syllogism in several ways.

First, yes, mutable structs are evil. However, they are sometimes very useful because in some limited scenarios, you can get a performance advantage. I do not recommend this approach unless other reasonable avenues have been exhausted and there is a real performance problem.

Second, reference copying is not necessarily cheaper than struct copying. References are typically implemented as 4 or 8 byte managed pointers (though that is an implementation detail; they could be implemented as opaque handles). Copying a reference-sized struct is neither cheaper nor more expensive than copying a reference-sized reference.

Third, even if reference copying is cheaper than struct copying, references must be dereferenced in order to get at their fields. Dereferencing is not zero cost! Not only does it take machine cycles to dereference a reference, doing so might mess up the processor cache, and that can make future dereferences far more expensive!

Fourth, even if reference copying is cheaper than struct copying, who cares? If that is not the bottleneck that is producing an unacceptable performance cost then which one is faster is completely irrelevant.

Fifth, references are far, far more expensive in memory space than structs are.

Sixth, references add expense because the network of references must be periodically traced by the garbage collector; "blittable" structs may be ignored by the garbage collector entirely. Garbage collection is a large expense.

Seventh, immutable value types cannot be null, unlike reference types. You know that every value is a good value. And as Reed pointed out, in order to get a good value of a reference type you have to run both an allocator and a constructor. That's not cheap.

Eighth, value types represent values, and programs are often about the manipulation of values. It makes sense to "bake in" the metaphors of both "value" and "reference" in a language, regardless of which is "cheaper".

like image 34
Eric Lippert Avatar answered Oct 30 '22 18:10

Eric Lippert