C++ index loop vs range-based for

以OBS Studio的code為範例
VS2017 15.7.1 release with pdb build

這邊我把兩種方式都寫進去,是為了方便比較asm,實際上只會選一種來用
(2)會轉成iterator的方式 http://en.cppreference.com/w/cpp/language/range-for
(2)產生的asm會比(1)快

OBS是用(1),我在修其他bug時看到,就順手改成(2)丟給官方了
稍微想一下就知道index會比較慢了,每個loop都要算 volumes + i*n (array element大小)
(2) 每個loop只要 vol += n

這邊我沒想到Jim會連這個也要求要照coding style,就直接寫auto& i了
想說是trivial的東西,因為每個範例都這樣寫
被要求改成 auto &vol

void OBSBasic::ClearVolumeControls()
{
    //(1)
    VolControl *control;

    for (size_t i = 0; i < volumes.size(); i++) {
        control = volumes[i];
        delete control;
    }
    //(2)
    for (auto &vol : volumes)
        delete vol;

    volumes.clear();
}


asm code https://hastebin.com/jasikeqada.pl

void OBSBasic::ClearVolumeControls()
{
00007FF61D43C0B0  mov         qword ptr [rsp+8],rbx  
00007FF61D43C0B5  mov         qword ptr [rsp+10h],rbp  
00007FF61D43C0BA  mov         qword ptr [rsp+18h],rsi  
00007FF61D43C0BF  push        rdi  
00007FF61D43C0C0  sub         rsp,20h  
00007FF61D43C0C4  mov         rbp,rcx  
 VolControl *control;

 for (size_t i = 0; i < volumes.size(); i++) {
00007FF61D43C0C7  xor         edi,edi  
00007FF61D43C0C9  mov         rcx,qword ptr [rcx+40h]  
00007FF61D43C0CD  mov         esi,edi  
00007FF61D43C0CF  mov         rax,rcx  
00007FF61D43C0D2  mov         rbx,qword ptr [rbp+38h]  
00007FF61D43C0D6  sub         rax,rbx  
00007FF61D43C0D9  sar         rax,3  
00007FF61D43C0DD  test        rax,rax  
00007FF61D43C0E0  je          OBSBasic::ClearVolumeControls+60h (07FF61D43C110h)  
  control = volumes[i];
  delete control;
00007FF61D43C0E2  mov         rcx,qword ptr [rbx+rsi*8]  
00007FF61D43C0E6  test        rcx,rcx  
00007FF61D43C0E9  je          OBSBasic::ClearVolumeControls+46h (07FF61D43C0F6h)  
00007FF61D43C0EB  mov         rax,qword ptr [rcx]  
00007FF61D43C0EE  mov         edx,1  
00007FF61D43C0F3  call        qword ptr [rax+18h]  
 VolControl *control;

 for (size_t i = 0; i < volumes.size(); i++) {
00007FF61D43C0F6  mov         rcx,qword ptr [rbp+40h]  
00007FF61D43C0FA  inc         rsi  
00007FF61D43C0FD  mov         rbx,qword ptr [rbp+38h]  
00007FF61D43C101  mov         rax,rcx  
00007FF61D43C104  sub         rax,rbx  
00007FF61D43C107  sar         rax,3  
00007FF61D43C10B  cmp         rsi,rax  
00007FF61D43C10E  jb          OBSBasic::ClearVolumeControls+32h (07FF61D43C0E2h)  
 }
 //
 for (auto& i : volumes)
00007FF61D43C110  mov         rsi,rcx  
00007FF61D43C113  sub         rsi,rbx  
00007FF61D43C116  add         rsi,7  
00007FF61D43C11A  shr         rsi,3  
00007FF61D43C11E  cmp         rbx,rcx  
00007FF61D43C121  cmova       rsi,rdi  
00007FF61D43C125  test        rsi,rsi  
00007FF61D43C128  je          OBSBasic::ClearVolumeControls+9Fh (07FF61D43C14Fh)  
00007FF61D43C12A  nop         word ptr [rax+rax]  
  delete i;
00007FF61D43C130  mov         rcx,qword ptr [rbx]  
00007FF61D43C133  test        rcx,rcx  
00007FF61D43C136  je          OBSBasic::ClearVolumeControls+93h (07FF61D43C143h)  
00007FF61D43C138  mov         rax,qword ptr [rcx]  
00007FF61D43C13B  mov         edx,1  
00007FF61D43C140  call        qword ptr [rax+18h]  
 }
 //
 for (auto& i : volumes)
00007FF61D43C143  add         rbx,8  
00007FF61D43C147  inc         rdi  
00007FF61D43C14A  cmp         rdi,rsi  
00007FF61D43C14D  jne         OBSBasic::ClearVolumeControls+80h (07FF61D43C130h)  

 volumes.clear();
00007FF61D43C14F  mov         rax,qword ptr [rbp+38h]  
}
00007FF61D43C153  mov         rbx,qword ptr [rsp+30h]  
00007FF61D43C158  mov         rsi,qword ptr [rsp+40h]  

 volumes.clear();
00007FF61D43C15D  mov         qword ptr [rbp+40h],rax  
}
00007FF61D43C161  mov         rbp,qword ptr [rsp+38h]  
00007FF61D43C166  add         rsp,20h  
00007FF61D43C16A  pop         rdi  
00007FF61D43C16B  ret  

留言

這個網誌中的熱門文章

OBS 抓不到畫面, 視窗, 遊戲擷取, 黑或白畫面, 當掉 ,卡住, black/white screen, window game capture, crash, freeze

優化實況(一) OBS設定 streaming settings

OBS plugin obs-text 顯示播放中音樂, display playing music