www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why are exceptions so slow?

reply Arcane Jill <Arcane_member pathlink.com> writes:
Hi,

I don't understand why exceptions are so slow in D. They should be very fast,
because, (in my imagination, at least), they don't have to do much. But I just
wrote a piece of code yesterday which used pretty much used exceptions like a
fancy goto (thanks for that description, guys) and it ran unbelievebly slowly.
When I recoded it without exceptions, the speedup was phenomenal.

But in my imagination, all you have to do to throw an exception in D is load the
stack pointer with a previously stashed value, stash a pointer to the /next/
exception frame down (ready for next time), and perform a jump (through some
sort of lookup table based on the type of object thrown). I'm obviously missing
something though - it must be doing much more than that or it wouldn't take so
long.

What am I missing? What else does it have to do?

Arcane Jill
Jun 12 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Arcane Jill" <Arcane_member pathlink.com> wrote in message
news:cagtdm$1ug5$1 digitaldaemon.com...
 Hi,

 I don't understand why exceptions are so slow in D. They should be very
fast,
 because, (in my imagination, at least), they don't have to do much. But I
just
 wrote a piece of code yesterday which used pretty much used exceptions
like a
 fancy goto (thanks for that description, guys) and it ran unbelievebly
slowly.
 When I recoded it without exceptions, the speedup was phenomenal.

 But in my imagination, all you have to do to throw an exception in D is
load the
 stack pointer with a previously stashed value, stash a pointer to the
/next/
 exception frame down (ready for next time), and perform a jump (through
some
 sort of lookup table based on the type of object thrown). I'm obviously
missing
 something though - it must be doing much more than that or it wouldn't
take so
 long.

 What am I missing? What else does it have to do?
It has to be compatible with Microsoft's "Windows Structured Exception Handling" which is far too complicated to explain in a simple post. But it shouldn't matter if it is relatively slow. Exceptions should be for exceptional cases, not the ordinary program flow.
Jun 13 2004
next sibling parent "The Dr ... who?" <thedr who.com> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cagu40$1vnd$1 digitaldaemon.com...
 "Arcane Jill" <Arcane_member pathlink.com> wrote in message
 news:cagtdm$1ug5$1 digitaldaemon.com...
 Hi,

 I don't understand why exceptions are so slow in D. They should be very
fast,
 because, (in my imagination, at least), they don't have to do much. But I
just
 wrote a piece of code yesterday which used pretty much used exceptions
like a
 fancy goto (thanks for that description, guys) and it ran unbelievebly
slowly.
 When I recoded it without exceptions, the speedup was phenomenal.

 But in my imagination, all you have to do to throw an exception in D is
load the
 stack pointer with a previously stashed value, stash a pointer to the
/next/
 exception frame down (ready for next time), and perform a jump (through
some
 sort of lookup table based on the type of object thrown). I'm obviously
missing
 something though - it must be doing much more than that or it wouldn't
take so
 long.

 What am I missing? What else does it have to do?
It has to be compatible with Microsoft's "Windows Structured Exception Handling" which is far too complicated to explain in a simple post.
You're not wrong, about either the need for SEH compatibility, or its complexity.
 But it shouldn't matter if it is relatively slow. Exceptions should be for
 exceptional cases, not the ordinary program flow.
Exactamundo. Theoretically, I concur. Practically, D is not Java, does not have a byte-code interpreter, and therefore must interact with the machine for things like /0 and *NULL. Hence, it is slow. QED -- The Dr. da-da-da-dum, da-da-da-dum, da-da-da-dum, daaah da-da-da-dum, da-da-da-dum, da-da-da-dum, daaah woo-ooooo
Jun 13 2004
prev sibling parent reply Charlie <Charlie_member pathlink.com> writes:
It has to be compatible with Microsoft's "Windows Structured Exception
Handling" which is far too complicated to explain in a simple post.
Heres a good article http://www.codetools.com/cpp/exceptionhandler.asp
 pretty much used exceptions
like a
 fancy goto 
Perverse! Charlie In article <cagu40$1vnd$1 digitaldaemon.com>, Walter says...
"Arcane Jill" <Arcane_member pathlink.com> wrote in message
news:cagtdm$1ug5$1 digitaldaemon.com...
 Hi,

 I don't understand why exceptions are so slow in D. They should be very
fast,
 because, (in my imagination, at least), they don't have to do much. But I
just
 wrote a piece of code yesterday which used pretty much used exceptions
like a
 fancy goto (thanks for that description, guys) and it ran unbelievebly
slowly.
 When I recoded it without exceptions, the speedup was phenomenal.

 But in my imagination, all you have to do to throw an exception in D is
load the
 stack pointer with a previously stashed value, stash a pointer to the
/next/
 exception frame down (ready for next time), and perform a jump (through
some
 sort of lookup table based on the type of object thrown). I'm obviously
missing
 something though - it must be doing much more than that or it wouldn't
take so
 long.

 What am I missing? What else does it have to do?
It has to be compatible with Microsoft's "Windows Structured Exception Handling" which is far too complicated to explain in a simple post. But it shouldn't matter if it is relatively slow. Exceptions should be for exceptional cases, not the ordinary program flow.
Jun 13 2004
parent "Walter" <newshound digitalmars.com> writes:
"Charlie" <Charlie_member pathlink.com> wrote in message
news:cahflb$2onn$1 digitaldaemon.com...
It has to be compatible with Microsoft's "Windows Structured Exception
Handling" which is far too complicated to explain in a simple post.
Heres a good article http://www.codetools.com/cpp/exceptionhandler.asp
That's a good introduction to SEH, thanks.
Jun 13 2004