类 实例的内存对齐

Memory align of instances of classes

我最近 interesting article 阅读了有关性能的内容。

在那里,作者定义struct如下:

public struct DecisionTreeNode
{
  public short ShortValue;
  public float FloatValue;
  public byte ByteVal1;
  public byte ByteVal2;
}

这个 struct 的实例是 8 个字节。

一切都清楚了,但后来他建议为了获得性能,作者写道:

This [memory] alignment issue means it can be a fair bit more expensive to load the non aligned value. Luckily, the fix is really simple, you just swap the order of the fields in your class definition.

所以他这样做了:

public struct DecisionTreeNode
{
  public float FloatValue;
  public short ShortValue;
  public byte ByteVal1;
  public byte ByteVal2;
}

class 中字段的顺序如何影响实例在内存中的存储方式?它如何影响提到的对齐方式?

你的解释很好:

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?view=netframework-4.7

https://kalapos.net/Blog/ShowPost/DotNetConceptOfTheWeek13_DotNetMemoryLayout

一切都是关于Pack字段的(是其最大元素的大小或指定的packing size属性=> [StructLayout(LayoutKind.Sequential, Pack=X)] 取小者)。

根据 Pack 字段,结构将字段按顺序分组为 Pack 大小并添加空字节以适应 Pack 字段。

struct => 1byte + 8byte + 1byte => Pack field => 8 => 8 + 8 + 8 = 24bytes

struct => 1byte + 1byte + 8byte => Pack field => 8 => 8 (1+1+6 empty bytes) + 8 = 16 bytes。

如您所见,始终使用 Pack 字段大小对字段进行分组。