[Bug c++/33358] New: Slow calls through simple member function pointers

2007-09-08 Thread dleska at gmail dot com
Hi,

By simple MFPs I mean those that point to a class with single or no inheritance
and the class is fully defined at the time of the call. For example:

class SomeClass {};

typedef void (SomeClass::*MemberFunc)();

void callMemberFunc(SomeClass *object, MemberFunc func)
{
   (object->*func)();
} 

g++ seems to generate the same code whether SomeClass is a complete type or
not. In the above example, VS 2005 can significantly optimise the call when
SomeClass is defined:

00424D40  mov ecx,dword ptr [esp+4]
00424D44  jmp dword ptr [esp+8] 

g++ 4.2.1 generates the following:

.file   "test.cpp"
.text
.align 2
.p2align 4,,15
.globl __Z12callDelegateRN12fastdelegate12FastDelegateIFvvEEE
.def__Z12callDelegateRN12fastdelegate12FastDelegateIFvvEEE; .scl   
2;  .type   32; .endef
__Z12callDelegateRN12fastdelegate12FastDelegateIFvvEEE:
pushl   %ebp
movl%esp, %ebp
pushl   %ebx
movl8(%ebp), %eax
movl4(%eax), %ecx
movl(%eax), %ebx
movl8(%eax), %edx
testb   $1, %cl
je  L7
leal(%ebx,%edx), %edx
movl(%edx), %eax
movl-1(%eax,%ecx), %ecx
movl%edx, 8(%ebp)
popl%ebx
popl%ebp
jmp *%ecx
.p2align 4,,7
L7:
leal(%ebx,%edx), %edx
movl%edx, 8(%ebp)
popl%ebx
popl%ebp
jmp *%ecx

I've reproduced this on 4.2.1 and 3.4.2 on x86 (MinGW) and 4.1.1, 4.1.0, 4.0.1
and 3.4.4 on arm/thumb.

Would it be possible for gcc to optimise such calls?


-- 
   Summary: Slow calls through simple member function pointers
   Product: gcc
   Version: 4.2.1
Status: UNCONFIRMED
  Severity: enhancement
  Priority: P3
 Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
    ReportedBy: dleska at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33358



[Bug c++/33358] Slow calls through simple member function pointers

2007-09-08 Thread dleska at gmail dot com


--- Comment #2 from dleska at gmail dot com  2007-09-09 06:47 ---
(In reply to comment #1)
> VS seems to dispatch to some other function.

Yep - if the function is virtual, VS generates a stub that does the vtable
lookup.  However for non virtual function the call is direct - which is much
faster than what gcc does.

Note that if I replace the first line of my example with 'class SomeClass;' -
gcc and VS generate comparable code. However when the class is defined at the
point of the call and does not use multiple/virtual inheritance, VS generates
significantly faster calls (at least for non-virtual functions).

Is this optimisation not possible in gcc?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33358