Why is 16 byte the recommended size for struct in C#?

后端 未结 7 475
心在旅途
心在旅途 2020-11-29 07:20

I read the Cwalina book (recommendations on development and design of .NET applications).

He says that a good designed struct has to be less than 16 bytes in size (f

相关标签:
7条回答
  • 2020-11-29 07:55

    One of the things I learnt while doing assembly language programming was that you align your structs (and other data) on 8 or 16 byte boundaries (there is actually a preprocessor command for it in MASM). The reason for this is considerably faster memory access when storing or moving things around in memory. Keeping your structs under 16 bytes in size would ensure that this memory alignment could be done successfully because nothing would cross the alignment boundaries.

    However I would expect this sort of thing to be largely hidden from you when using the .NET framework. If your structs were bigger than 16 bytes then I would expect the framework (or JIT compiler) to align your data on 32 byte boundaries, and so on. It would be interesting to see some validation of the comments in those books, and see what the difference in speed is.

    0 讨论(0)
提交回复
热议问题