I'm studying .NET structs - some books advise creating structs if they have a开发者_如何学Pythonn instance size less than 16 bytes.
Why is that?
It's because 16 bytes is the threshold where the compiler starts copying structs as block of memory instead of using one or two simple move instructions.
The compiler optimises the copying of structures when they are small. A struct that is for example eight bytes can be copied as a single 64 bit value. A struct that is 16 bytes can be copies as one or two singular values (depending on the processor architecture). When the structure is larger than 16 bytes, the compiler doesn't try to optimise the move any more, and the fallback is to call a method that copies a block of memory.
(Note: the threshold of 16 bytes may differ depending on the version of compiler, it seems as it actually tries to optimise beyond that point in newer versions, but the optimised code will still be a lot of move instructions, compared to copying a reference to an object which still is a single move operation.)
Edit:
Here is the result of a test that I did on my 64 bit system copying structs half a billion times:
struct 4 : 272 ms.
struct 8 : 235 ms.
struct 16 : 317 ms.
struct 32 : 625 ms.
struct 64 : 1280 ms.
struct 128 : 4659 ms.
struct 256 : 8020 ms.
As you see, below 16 bytes the time is not linear, although 16 bytes is four times as much as 4 bytes, it doesn't take four times longer. Above 16 bytes the time is linear, so doubling the size double the time. That's where it would start using multiple moves. Above 64 bytes there is a jump, where the time suddenly quadruples when the size doubles. That's where the fallback would start to be used.
It's not exactly 16 bytes, but because structs are passed by value (copied whenever you give them to a method, assign them, etc.) they must not be too large. If they are, passing stuff by reference is cheaper.
4 years later: I answered this knowing very little about .NET (still don't know much about it). Guffa's answer is obviously more correct when dealing with "more or less than 16 bytes?", since at that size a bit of copying shouldn't matter much. My answer may be something to keep in mind when building huge structs.
精彩评论