[ODE] Exception Handling

Jon Watte (ODE) hplus-ode at mindcontrol.org
Thu Feb 1 15:13:09 MST 2007


Benchmarking is a very hard science, and it's easy to make mistakes. 
That's why I often get frustrated when I suggest specific code paths 
based on experience and knowledge, and some happy amateur will whip up 
some irrelevant piece of code and "prove" that some other way is better.

In this case, aren't you even the slightest bit concerned that the call 
to exit() is nowhere to be found in your code?

The reason is that printf() can't throw, so the compiler eliminates the 
exception code. If you compile somthing that might throw, here is the 
code. Note the big difference in code size between TestFuncA and 
TestFuncB: TestFuncA is 17 times bigger (in bytes)!
This is MSVC 2005 with /Ox for full optimization


#include <exception>
#include <stdlib.h>

extern void func_may_throw() throw(std::exception);

void TestFuncA() {
  try {
    func_may_throw();
  }
  catch (...) {
    exit(-1);
  }
}

void TestFuncB() {
  func_may_throw();
}


Microsoft (R) COFF/PE Dumper Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file foo.obj

File Type: COFF OBJECT

?TestFuncA@@YAXXZ (void __cdecl TestFuncA(void)):
  00000000: 55                 push        ebp
  00000001: 8B EC              mov         ebp,esp
  00000003: 6A FF              push        0FFFFFFFFh
  00000005: 68 00 00 00 00     push        offset 
__ehhandler$?TestFuncA@@YAXXZ
  0000000A: 64 A1 00 00 00 00  mov         eax,dword ptr fs:[00000000h]
  00000010: 50                 push        eax
  00000011: 51                 push        ecx
  00000012: 53                 push        ebx
  00000013: 56                 push        esi
  00000014: 57                 push        edi
  00000015: A1 00 00 00 00     mov         eax,dword ptr 
[___security_cookie]
  0000001A: 33 C5              xor         eax,ebp
  0000001C: 50                 push        eax
  0000001D: 8D 45 F4           lea         eax,[ebp-0Ch]
  00000020: 64 A3 00 00 00 00  mov         dword ptr fs:[00000000h],eax
  00000026: 89 65 F0           mov         dword ptr [ebp-10h],esp
  00000029: C7 45 FC 00 00 00  mov         dword ptr [ebp-4],0
            00
  00000030: E8 00 00 00 00     call        ?func_may_throw@@YAXXZ
  00000035: C7 45 FC FF FF FF  mov         dword ptr [ebp-4],0FFFFFFFFh
            FF
  0000003C: 8B 4D F4           mov         ecx,dword ptr [ebp-0Ch]
  0000003F: 64 89 0D 00 00 00  mov         dword ptr fs:[0],ecx
            00
  00000046: 59                 pop         ecx
  00000047: 5F                 pop         edi
  00000048: 5E                 pop         esi
  00000049: 5B                 pop         ebx
  0000004A: 8B E5              mov         esp,ebp
  0000004C: 5D                 pop         ebp
  0000004D: C3                 ret
__catch$?TestFuncA@@YAXXZ$0:
  0000004E: 6A FF              push        0FFFFFFFFh
  00000050: E8 00 00 00 00     call        _exit
$LN9:
  00000055: CC                 int         3
  00000056: CC                 int         3
  00000057: CC                 int         3
  00000058: CC                 int         3
  00000059: CC                 int         3
  0000005A: CC                 int         3
  0000005B: CC                 int         3
  0000005C: CC                 int         3
  0000005D: CC                 int         3
  0000005E: CC                 int         3
  0000005F: CC                 int         3
?TestFuncB@@YAXXZ (void __cdecl TestFuncB(void)):
  00000060: E9 00 00 00 00     jmp         ?func_may_throw@@YAXXZ

__ehhandler$?TestFuncA@@YAXXZ:
  00000000: 8B 54 24 08        mov         edx,dword ptr [esp+8]
  00000004: 8D 42 0C           lea         eax,[edx+0Ch]
  00000007: 8B 4A EC           mov         ecx,dword ptr [edx-14h]
  0000000A: 33 C8              xor         ecx,eax
  0000000C: E8 00 00 00 00     call        @__security_check_cookie at 4
  00000011: B8 00 00 00 00     mov         eax,offset 
__ehfuncinfo$?TestFuncA@@YAXXZ
  00000016: E9 00 00 00 00     jmp         ___CxxFrameHandler3

  Summary

           4 .data
          10 .debug$F
          5D .debug$S
          45 .drectve
           F .rdata
           4 .sxdata
          65 .text
          1B .text$x
          58 .xdata$x



Simon Kolek wrote:
>> Well, not with GCC 3.4.5 dwarf2-eh. How bad is VC++ implementation?
>>
>>   
>>     
> Very very bad :). 
>
> VC++ is very good in C++ exceptions code optimization. Typically - 
> exception code is moved away from function body.
>
> Sample functions:
>
> void TestFuncA()
> {
>     try
>     {
>         printf( "aaaa" );
>     }
>     catch(...)
>     {
>         exit(-1);
>     }
> }
>
> void TestFuncB()
> {
>     printf( "aaaa" );
> }
>
> ----------------
>
> VC++2k5 asm output(  no differences):
>
> PUBLIC    ?TestFuncA@@YAXXZ                ; TestFuncA
> _TEXT    SEGMENT
> ?TestFuncA@@YAXXZ PROC                    ; TestFuncA
>     push    OFFSET ??_C at _04CLOGDNII@aaaa?$AA@
>     call    DWORD PTR __imp__printf
>     pop    ecx
>     ret    0
> ?TestFuncA@@YAXXZ ENDP                    ; TestFuncA
> _TEXT    ENDS
>
>
> PUBLIC    ?TestFuncB@@YAXXZ                ; TestFuncB
> _TEXT    SEGMENT
> ?TestFuncB@@YAXXZ PROC                    ; TestFuncB
>     push    OFFSET ??_C at _04CLOGDNII@aaaa?$AA@
>     call    DWORD PTR __imp__printf
>     pop    ecx
>     ret    0
> ?TestFuncB@@YAXXZ ENDP                    ; TestFuncB
> _TEXT    ENDS
>
>
>
> B.R.
>     Simon
> _______________________________________________
> ODE mailing list
> ODE at ode.org
> http://mooshika.org/mailman/listinfo/ode
>
>
>   


More information about the ODE mailing list