Suppose I have the following code
void f(PolymorphicType *p)
{
for (int i = 0; i < 1000; ++i)
{
p->virtualMethod(something);
}
}
Will the compiler's generated code dereference p
's vtable
entry for virtualMethod
1 or 1000 times? I am using Microsoft's compiler.
edit
here is the generated assembly for the real-world case I'm looking at. line->addPoint()
is the virtual method of concern. I have no assembly experience, so I'm going over it slowly...
; 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 : }