Home / Software / Tips and Tricks / Improove structurer performance by using StructLayout attribute

Improove structurer performance by using StructLayout attribute

Updated:  10/14/2010 13:10 PM
Author:  Shiju Mathews

Status:    Resolved.

When you declare a type, the CLR in runtime automatically rearrange the order of your type members for performance, so the access to these members are faster, and also trying to use minimum memory, stuff like making the variables reserves even memory locations for faster access and all these stuff, but you still have control on the CLR to force to follow the sequence you defined. This helps especially in structure to improove performance.

If you are declaring a struct type, the default Layout is Sequential, so you don't need to add the StructLayout.

However, developers add the attribute to struct when they want to mention that this struct is defined for interop with unmanaged stuff.

  • Auto (which is the default behavior)
  • Sequential
  • Explicit

    My tests are showing that, this will save up to 10% of memory utilization