Got some simple code
Int32[] tmpInt = new Int32[32];
long lStart = DateTime.Now.Ticks;
Thread t1 = new Thread(new ThreadStart(delegate()
{
for (Int32 i = 0; i < 100000000; i++)
Interlocked.Increment(ref tmpInt[5]);
}));
Thread t2 = new Thread(new ThreadStart(delegate()
{
for (Int32 i = 0; i < 100000000; i++)
Interlocked.Increment(ref tmpInt[20]);
}));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(((DateTime.Now.Ticks - lStart)/10000).ToString());
This takes ~3 seconds on my core 2 duo. If I change the index in t1 to tmpInt[4], it takes ~5.5 seconds.
Anyway, the first cache line ends at index 4. Being that a cache line is 64bytes and 5 int32s are only 20 bytes, that means there are 44 bytes of metadata and/or padding before the actual array.
Another set of values that I tested where 5 and 21. 5 and 21 take ~3 seconds, but 5 and 20 takes ~5.5 seconds, but that's because index 20 shares the same cache line as index 5 as they're spaced within the same 64 bytes.
So my question is, how much data does .Net reserve before an array and does this amount change between 32bit and 64bit systems?
Thanks :-)