Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exceptions

Reply
Thread Tools

Exceptions

 
 
Jon Harrop
Guest
Posts: n/a
 
      01-20-2007

Why are exceptions in C++ ~6x slower than exceptions in OCaml?

--
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-20-2007
* Jon Harrop:
> Why are exceptions in C++ ~6x slower than exceptions in OCaml?


Languages are not fast or slow; implementations are.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-20-2007

Jon Harrop wrote:
> Why are exceptions in C++ ~6x slower than exceptions in OCaml?


Remember that C++ exceptions are typically implemented in such a way
that they can transparently travel through frames that don't know
anything about exceptions, including frames set up by code that wasn't
necessarily even written in C++. It could be written in C and compiled
with a C compiler, or even written by hand in assembly language---as
long at follow the instruction set architecture's conventions with
regard to the stack. C++ code that does have any destructors to run,
and doesn't catch any exceptions can be also compiled as though
exceptions didn't exist, if things are implemented this way. But this
means that the exception handling search has to parse its way through
the machine's stack layout.

Would it be okay to slow down code that doesn't care about exceptions
by, say, 15% in order to speed up exceptions six fold? A lot of the
users of C++ would not want that trade off. Exceptions tend to be
regarded as rare events that occur in, well, exceptional circumstances.

Here is a consideration: Do OCaml exceptions have an ABI that non-OCaml
components running in the same image can use for interoperability?

Lastly, what compilers have you compared, anyway? How many compilers
are there for OCaml, and what kinds of things, in the area of
exceptions, did their writers consider important?

 
Reply With Quote
 
Jon Harrop
Guest
Posts: n/a
 
      01-20-2007
Kaz Kylheku wrote:
> Would it be okay to slow down code that doesn't care about exceptions
> by, say, 15% in order to speed up exceptions six fold? A lot of the
> users of C++ would not want that trade off. Exceptions tend to be
> regarded as rare events that occur in, well, exceptional circumstances.


Only if they are slow. In OCaml, exceptions are so fast that they are used
extensively.

> Here is a consideration: Do OCaml exceptions have an ABI that non-OCaml
> components running in the same image can use for interoperability?


OCaml can interface to C using a variety of macros from OCaml's library to
handle values and exceptions. I don't know how the exceptions are
implemented though.

Presumably garbage collection makes exceptions much faster, as C++ will have
to call all destructors in every scope that it unwinds past. In fact, C++
probably has to store such information in every stack frame, which might
explain why it has such big stack frames...

> Lastly, what compilers have you compared, anyway? How many compilers
> are there for OCaml, and what kinds of things, in the area of
> exceptions, did their writers consider important?


Just one OCaml compiler and g++ 4. My main test was a balanced binary tree
set implementation. I tried using exceptions to bail when inserting an
already-present element, to return the original tree unaltered. In OCaml,
this was significantly more efficient and clearer than unwinding the stack
by hand. In C++, it is much slower.

--
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-20-2007
Jon Harrop wrote:
>
> Just one OCaml compiler and g++ 4. My main test was a balanced binary tree
> set implementation. I tried using exceptions to bail when inserting an
> already-present element, to return the original tree unaltered. In OCaml,
> this was significantly more efficient and clearer than unwinding the stack
> by hand. In C++, it is much slower.
>

I'm sure you don't have to be reminded that in this universe you don't
get something for nothing. At some point in anything other than a
trivial application you garbage collector is going to kick in and do its
stuff. At that point, you will pay the cost of destroying the objects
created on the stack.

--
Ian Collins.
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-20-2007

Jon Harrop wrote:

> Kaz Kylheku wrote:
> > Would it be okay to slow down code that doesn't care about exceptions
> > by, say, 15% in order to speed up exceptions six fold? A lot of the
> > users of C++ would not want that trade off. Exceptions tend to be
> > regarded as rare events that occur in, well, exceptional circumstances.

>
> Only if they are slow. In OCaml, exceptions are so fast that they are used
> extensively.


Are you really write program as expecting exceptions? It is wrong
paradigm for C++ to use exception as return value or as representation
of concrete correct state. For example, error during remote transfer
over bad quality connection in most cases must not be treated as
exception.

The goal of exception using is safe termination (mostly) or restarting
separated part of program, if someting unexpected happends in it. In
most most cases does not matter time of exception execution if your
memory no longer exist.

In any case, what the trouble has C++ that results in slow exception
handling?

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      01-20-2007

Jon Harrop wrote:
> I tried using exceptions to bail when inserting an
> already-present element, to return the original tree unaltered.


And what does the exception handler do?

regards
Andy Little

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-20-2007

Jon Harrop skrev:
> Why are exceptions in C++ ~6x slower than exceptions in OCaml?
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy


This answer can not be answered without you giving us information about
the compiler used. Different compilers use different ways to handle
exceptions, and each method has its trade-offs.
One thing to say about exceptions is that they are meant for
exceptional stuff: thus C++ users would prefer to have fast code when
exceptions are not thrown, rather than having a fast path when an
actual throw occurs. In another post you mention the usage of
exceptions for a fast return after a binary tree search. I would say
that for a generic tree, using throw in this situation is not a smart
design decision.

/Peter

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-20-2007

Jon Harrop skrev:
> Kaz Kylheku wrote:
> > Would it be okay to slow down code that doesn't care about exceptions
> > by, say, 15% in order to speed up exceptions six fold? A lot of the
> > users of C++ would not want that trade off. Exceptions tend to be
> > regarded as rare events that occur in, well, exceptional circumstances.

>
> Only if they are slow. In OCaml, exceptions are so fast that they are used
> extensively.


Right. So we have an example where you use a style from one programming
language in another programming language. It should be no surprise that
such an approach has less than optimal performance.

[snip]
> Presumably garbage collection makes exceptions much faster, as C++ will have
> to call all destructors in every scope that it unwinds past. In fact, C++
> probably has to store such information in every stack frame, which might
> explain why it has such big stack frames...


It is correct that for objects with a non-trivial destructor, the
exception will have to be "caught" in order to release the ressources.
In the cases where the only resource involved is memory, C++ will have
to do more stops in order to clean up. But It still puzzles me if most
of your functions have such heavy objects: I believe most functions
would not contain such objects. Are you passing by value instead of be
reference? This could also explain your "big" stack frames.

>
> > Lastly, what compilers have you compared, anyway? How many compilers
> > are there for OCaml, and what kinds of things, in the area of
> > exceptions, did their writers consider important?

>
> Just one OCaml compiler and g++ 4. My main test was a balanced binary tree
> set implementation. I tried using exceptions to bail when inserting an
> already-present element, to return the original tree unaltered. In OCaml,
> this was significantly more efficient and clearer than unwinding the stack
> by hand. In C++, it is much slower.

I have no personal experience with g++ 4, but believe it should be
quite good. So far as I know, g++ implements exception handling by
having an external table that links instruction pointer adress and
exception handling. This approach gives no overhead at all when you
follow the non-exceptional path, but a rather slow response whenever an
exception is thrown.

/Peter

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-20-2007
Jon Harrop wrote:
> Kaz Kylheku wrote:
>> Would it be okay to slow down code that doesn't care about exceptions
>> by, say, 15% in order to speed up exceptions six fold? A lot of the
>> users of C++ would not want that trade off. Exceptions tend to be
>> regarded as rare events that occur in, well, exceptional circumstances.

>
> Only if they are slow. In OCaml, exceptions are so fast that they are used
> extensively.
>


That's backwards. C++ exceptions were designed for use only in
exceptional circumstances, so programmers who understand the language
don't put a premium on speed there.

>> Here is a consideration: Do OCaml exceptions have an ABI that non-OCaml
>> components running in the same image can use for interoperability?

>
> OCaml can interface to C using a variety of macros from OCaml's library to
> handle values and exceptions. I don't know how the exceptions are
> implemented though.
>
> Presumably garbage collection makes exceptions much faster, as C++ will have
> to call all destructors in every scope that it unwinds past. In fact, C++
> probably has to store such information in every stack frame, which might
> explain why it has such big stack frames...
>


Wow, big stack frames! But this, too, is nonsense. There's nothing in
the C++ language definition that requires storing information about
destructors in stack frames, and I don't know of any compiler that does
that.

>> Lastly, what compilers have you compared, anyway? How many compilers
>> are there for OCaml, and what kinds of things, in the area of
>> exceptions, did their writers consider important?

>
> Just one OCaml compiler and g++ 4. My main test was a balanced binary tree
> set implementation. I tried using exceptions to bail when inserting an
> already-present element, to return the original tree unaltered. In OCaml,
> this was significantly more efficient and clearer than unwinding the stack
> by hand. In C++, it is much slower.
>


I don't doubt it. Bad designs tend to have poor performance. Using C++
exceptions as an alternate return mechanism is a bad design. That's not
what they're intended for.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
ImageList Errors - Exceptions and...need HELP Herr Lucifer ASP .Net 0 06-21-2004 10:21 AM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments