Reputation: 70317
What is the maximum recommended size of a value type? I think I read that Microsoft suggests that they are not being larger than 16 bytes, but I can't find the reference.
Upvotes: 5
Views: 464
Reputation: 81179
Copying a value type 17 bytes or larger will take significantly longer than copying one which is 16 bytes or smaller; for this reason, the authors of Microsoft's guidelines suggested that structures should be smaller than 16 bytes. In fact, however, there are many situations where larger structures may be entirely appropriate.
In deciding whether to use a struct or an immutable class, one should note that certain operations are faster on each. Immutable class types are very efficient in situations where one would want many storage locations (variables, array elements, or whatever) to hold exactly the same information. One simply creates one object which holds the information, and then stores a reference into every storage location which should hold that information; the cost of storing the reference is equal to the cost of storing a four or eight (for 32/64-bit systems) byte struct. Storing larger structures would be correspondingly slower.
Immutable types behave poorly, however, in cases where one will often, take a storage location which holds some data and change it to a storage location which holds slightly-different data. In this scenario, exposed-field struct types are often ideal. The cost of making a slight change to such a storage location is very low, and is dependent on the nature of the change, rather than the size of the struct. By contrast, making a small change to a storage location of an immutable class type generally requires creating a new class object whose state is mostly copied from the old one, but with whatever changes are required. The cost of this is proportional to the size of the class, plus a substantial amount of overhead.
If one structure's one's code to minimize the number of times that structs get copied, even large structs can be quite efficient. Even though 17-byte structs are slower than 16-byte structs, a mutable 100-byte struct can still be more efficient than an equivalent immutable class.
Upvotes: 1
Reputation: 60902
I found a discussion and a loose recommendation on MSDN, "Using Classes and Structures in Visual Basic .NET":
If your application makes a large number of copies of a variable, the memory required for that variable can be a factor that determines whether it should be a value type or a reference type. There is a trade-off between copying all the bytes of a value type as opposed to allocating a new reference type on the heap. The more copies of a variable your application makes, the more important this distinction becomes.
A theoretical observation might serve as an initial guideline. Suppose you write a test application that does the following:
[..]
Depending on the execution platform and the loading from other tasks, you are likely to observe the following:
- If the common data size is less than 16 bytes, the structure instance copy loop might be slightly faster than the class instance copy loop.
- If the data size is 16 bytes, the loops might be approximately equal in timing.
- If the data size is greater than 16 bytes, the class loop is likely to be faster.
And later, something more definitive at Choosing Between Classes and Structures:
Do not define a structure unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (integer, double, and so on).
- It has an instance size smaller than 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.
Upvotes: 2
Reputation: 4971
Yes, Microsoft did suggest for struct
not to be larger than 16 bytes.
Ref: Choosing Between Classes and Structures
Upvotes: 4