J
Jon Harrop
Why are exceptions in C++ ~6x slower than exceptions in OCaml?
Why are exceptions in C++ ~6x slower than exceptions in OCaml?
Jon said:Why are exceptions in C++ ~6x slower than exceptions in OCaml?
Kaz said: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?
I'm sure you don't have to be reminded that in this universe you don'tJon said: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.
Jon said:Only if they are slow. In OCaml, exceptions are so fast that they are used
extensively.
Jon said:I tried using exceptions to bail when inserting an
already-present element, to return the original tree unaltered.
Why are exceptions in C++ ~6x slower than exceptions in OCaml?
Only if they are slow. In OCaml, exceptions are so fast that they are used
extensively.
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...
I have no personal experience with g++ 4, but believe it should beJust 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.
Jon said:Only if they are slow. In OCaml, exceptions are so fast that they are used
extensively.
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...
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.
kwikius said:And what does the exception handler do?
Ian said: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.
peter said:Jon Harrop skrev:
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.
Yes.
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.
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.
Jon said:Returns the original tree.
kwikius said:Does it provide any indication that an error has occurred?
Jon said:Well, C++ always seems to have big stack frames, it just uses the
stack for many more things than other languages.
IR said:Thus "other" languages use the heap for many more things than C++.
Which is slower in most cases.
QED?
Pete said:No, no, no. You're missing the point. C++ is bad. So criticisms
should always be vague and scary, never objective.
IR said:Thus "other" languages use the heap for many more things than C++.
Which is slower in most cases.
QED?
Jon said:Heap is slower, but not 6x slower!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.