C++编译器可以在同一个指针上优化重复的虚函数调用吗?

jap*_*iss 15 c++ polymorphism performance vtable

假设我有以下代码

void f(PolymorphicType *p)
{
    for (int i = 0; i < 1000; ++i)
    {
        p->virtualMethod(something);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器生成的代码解除引用pvtable条目是virtualMethod1次还是1000次?我正在使用Microsoft的编译器.

编辑

这是我正在查看的真实世界案例的生成程序集. line->addPoint()是虚拟的关注方法.我没有装配经验,所以我慢慢地过去了...

; 369  :        for (int i = 0; i < numPts; ++i)

    test    ebx, ebx
    je  SHORT $LN1@RDS_SCANNE
    lea edi, DWORD PTR [ecx+32]
    npad    2
$LL3@RDS_SCANNE:

; 370  :        {
; 371  :            double *pts = pPoints[i].SystemXYZ;
; 372  :            line->addPoint(pts[0], pts[1], pts[2]);

    fld QWORD PTR [edi+8]
    mov eax, DWORD PTR [esi]
    mov edx, DWORD PTR [eax+16]
    sub esp, 24                 ; 00000018H
    fstp    QWORD PTR [esp+16]
    mov ecx, esi
    fld QWORD PTR [edi]
    fstp    QWORD PTR [esp+8]
    fld QWORD PTR [edi-8]
    fstp    QWORD PTR [esp]
    call    edx
    add edi, 96                 ; 00000060H
    dec ebx
    jne SHORT $LL3@RDS_SCANNE
$LN314@RDS_SCANNE:

; 365  :        }
Run Code Online (Sandbox Code Playgroud)

n. *_* m. 6

一般来说,不,这是不可能的.该函数可以破坏*this和放置 - 从该空间中的相同基础派生的新的一些其他对象.

编辑:更简单,功能可以改变p.编译器不可能知道谁具有地址p,除非它对于所讨论的优化单元是本地的.