Hacker News new | past | comments | ask | show | jobs | submit login

C++ exceptions have a runtime overhead, because they have to set up a handler (insert a record into a linked list) upon entering the try block and remove it when leaving the try block. Now of course checking return values introduces a lot of branches, so go figure out which one of them is worse...

still it is very advisable to have likely pragmas around if conditions that check return values.

The big problem with exceptions is that it introduces a lot of implicit code paths, and lots of possibilities for resource leaks (if raw pointers are involved).




That's one way they can be implemented, but now they tend to be zero-cost implementation where only when an exception occurs does it figure-out where it was and what to do.


the article speaks about 'embedded' systems - these are not x84-64 systems (intel cpus need too much power) so they don't have a 'zero cost' exception table (that is also not exactly zero cost)

Can you show me an embedded system with zero cost exceptions?


  $ CXX=g++ppc CXXFLAGS=-g make -f /dev/null foo.o
  g++ppc -g   -c -o foo.o foo.cc
  $ objdumpppc -drS foo.o                         
  
  foo.o:     file format elf32-powerpc-vxworks
  
  
  Disassembly of section .text:
  
  00000000 <_Z3fooi>:
  int
  foo(int i)
     0:   94 21 ff d0     stwu    r1,-48(r1)
     4:   7c 08 02 a6     mflr    r0
     8:   90 01 00 34     stw     r0,52(r1)
     c:   93 e1 00 2c     stw     r31,44(r1)
    10:   7c 3f 0b 78     mr      r31,r1
    14:   90 7f 00 18     stw     r3,24(r31)
  {
          try { if (i < 0) throw (i); }
    18:   80 1f 00 18     lwz     r0,24(r31)
    1c:   2f 80 00 00     cmpwi   cr7,r0,0
    20:   40 9c 00 74     bge-    cr7,94 <_Z3fooi+0x94>
    24:   38 60 00 04     li      r3,4
    28:   48 00 00 01     bl      28 <_Z3fooi+0x28>
                          28: R_PPC_REL24 __cxa_allocate_exception
    2c:   7c 60 1b 78     mr      r0,r3
    30:   7c 0b 03 78     mr      r11,r0
    34:   7d 69 5b 78     mr      r9,r11
    38:   80 1f 00 18     lwz     r0,24(r31)
    3c:   90 09 00 00     stw     r0,0(r9)
    40:   7d 63 5b 78     mr      r3,r11
    44:   3d 20 00 00     lis     r9,0
                          46: R_PPC_ADDR16_HA     _ZTIi
    48:   38 89 00 00     addi    r4,r9,0
                          4a: R_PPC_ADDR16_LO     _ZTIi
    4c:   38 a0 00 00     li      r5,0
    50:   48 00 00 01     bl      50 <_Z3fooi+0x50>
                          50: R_PPC_REL24 __cxa_throw
    54:   90 7f 00 1c     stw     r3,28(r31)
    58:   7c 80 23 78     mr      r0,r4
    5c:   2f 80 00 01     cmpwi   cr7,r0,1
    60:   41 9e 00 0c     beq-    cr7,6c <_Z3fooi+0x6c>
    64:   80 7f 00 1c     lwz     r3,28(r31)
    68:   48 00 00 01     bl      68 <_Z3fooi+0x68>
                          68: R_PPC_REL24 _Unwind_Resume
  
          catch (int e) { i = -e; }
    6c:   80 7f 00 1c     lwz     r3,28(r31)
    70:   48 00 00 01     bl      70 <_Z3fooi+0x70>
                          70: R_PPC_REL24 __cxa_begin_catch
    74:   7c 60 1b 78     mr      r0,r3
    78:   7c 09 03 78     mr      r9,r0
    7c:   80 09 00 00     lwz     r0,0(r9)
    80:   90 1f 00 08     stw     r0,8(r31)
    84:   80 1f 00 08     lwz     r0,8(r31)
    88:   7c 00 00 d0     neg     r0,r0
    8c:   90 1f 00 18     stw     r0,24(r31)
    90:   48 00 00 01     bl      90 <_Z3fooi+0x90>
                          90: R_PPC_REL24 __cxa_end_catch
  
          return (i);
    94:   80 1f 00 18     lwz     r0,24(r31)
  }
    98:   7c 03 03 78     mr      r3,r0
    9c:   81 61 00 00     lwz     r11,0(r1)
    a0:   80 0b 00 04     lwz     r0,4(r11)
    a4:   7c 08 03 a6     mtlr    r0
    a8:   83 eb ff fc     lwz     r31,-4(r11)
    ac:   7d 61 5b 78     mr      r1,r11
    b0:   4e 80 00 20     blr
  $ g++ppc -v 2>&1 | tail -1                      
  gcc version 4.3.3 (Wind River VxWorks G++ 4.3-315) 
  $




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: