4

I have seen that C# "is optimized" for accessing 1D array (does not call a function to access element), but for multidim-arrays function GetValue is called each time. Why not instead of calling this function each time "normal" access (pointers shifting + checking) is not implemented or at least this function is not flattened ?

I can not find the logical reason. (performance for multidim-arrays are not great)

2 Answers 2

1

I tried it out, it's about 14 instructions per element access, due to bounds check and multiplication by dimension size.

If perf is important, use 1D array of pinning to pointer.

Sign up to request clarification or add additional context in comments.

Comments

1

I didn't uncheck "Supress JIT optimization..." therefore function call GetValue wasn't inlined.

Optimized code:

b = a[3, 5];
00000026  mov         eax,3 
0000002b  lea         edx,[eax+2] 
0000002e  sub         eax,dword ptr [ecx+10h] 
00000031  cmp         eax,dword ptr [ecx+8] 
00000034  jae         0000010B 
0000003a  sub         edx,dword ptr [ecx+14h] 
0000003d  cmp         edx,dword ptr [ecx+0Ch] 
00000040  jae         0000010B 
00000046  imul        eax,dword ptr [ecx+0Ch] 
0000004a  add         eax,edx 
0000004c  mov         edi,dword ptr [ecx+eax*4+18h] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.