An array in C# has some space overhead: it stores its sync block, its array type, possibly its element type (for reference types), its length...
Some quick tests indicate that the array's elements actually start at a cache line boundary. For example, in a byte[]
it is faster for two separate threads to simultaneously update index 63 and 64, respectively (as they are on different cache lines), than for them to update index 62 and 63 (as they are on the same cache line).
I'm curious to know where the metadata is stored. Does it go at the end, possibly residing in the same cache line as the last few elements? Or is it stored elsewhere altogether?
This could be useful when trying to cache-optimize the usage of some array. If the metadata is stored in a separate cache line, then there is probably nothing we can do to avoid that extra cache line fill whenever the array is used. However, if it is stored in the last cache line, then the array size could be optimized to leave precisely enough room for the metadata, and the elements in that cache line could be probed first, as it is available anyway.