Array metadata issue (cache lines)

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());

It takes ~ 3 seconds on my core 2 duets. If I change the index in t1 to tmpInt [4], it takes ~ 5.5 seconds.

In any case, the first cache line ends with index 4. Since the cache line has 64 bytes and 5 int32 only 20 bytes, this means that there must be 44 bytes of metadata and / or filling before the actual array.

Another set of values ​​I tested is where 5 and 21. 5 and 21 takes ~ 3 seconds, but 5 and 20 takes ~ 5.5 seconds, but this is because index 20 shares the same cache line as index 5 when they are spaced within the same 64 bytes.

So my question is how much data does .Net back up before the array and does it change the amount between 32-bit and 64-bit systems?

: -)

+5
2

, , , ..NET , .

, 44 - , .

edit: http://msdn.microsoft.com/en-us/magazine/cc163791.aspx , , 16 . 4 - , 4 handle, - .

, , . , , -. , , , .

+4

: fooobar.com/questions/13467/...

, , 32- [64-]:

  • : 4B [8B]
  • : 4B [8B]
  • : 4B [4B]
  • : 4B [8B] ( )

4 :

12 bytes (32-bit value array)
16 bytes (32-bit reference array)
20 bytes (64-bit value array)
28 bytes (64-bit reference array)

-, : 64- " 32-" ( ), 32-!

0

All Articles