I have just read this article on instruction and memory CPU cache awareness:
http://www.research.scea.com/research/pdfs/GDC2003_Memory_Optimization_18Mar03.pdf
The article reasoning is around using C/C++.
I have three related questions:
Will C# generally follow same rules?
Do I need to order struct fields manually for the best performance? Will compiler take care of this or it's not possible due to higher level?
If yes, then how does this affect generic structures? E.g. struct Triple < T1, T2, T3 > { /.../ }
C# has the same issues as C and C++ in the sense that an unaligned structure can cause performance to suffer in certain (probably most) architectures.
Although it's probably allowed to, in practice the Microsoft compiler does not take care of this by default.
[StructLayout(LayoutKind.Auto)]
(which is the default) for structs is typically the same as[StructLayout(LayoutKind.Sequential)]
. In C, struct members are required to be sequential and in C# they followed this choice as well.With that out of the way, of course, compilers attempt to align struct members, because they optimize for performance but not for space. You may have figured out in your particular scenario that optimizing for space does improve performance, but this is too complicated for a compiler to deduce.
When you see a definition for a
Triple<T1, T2, T3>
, what you see in your code is what is called (in MSDN) a generic type definition. Every time you instantiate a new combination of the type parameters of this generic definition, a new runtime type is created by the environment (you may see it called something likeTriple'1
). That generated type isn't different from a non-generic type anymore and I see no reason why the JIT-compiler would treat it differently (but I could be wrong, of course).