[prev] [thread] [next] [lurker] [Date index for 2007/01/29]
On Sun, 2007-01-28 at 13:44 +0200, Yossi Kreinin wrote: > How can a C++ function, no matter if it has try/catch statements or not, be > exception safe without saving a pointer to the bulk of code (destructor > invokations) to call upon stack unwinding? How can you save things at 0 cycles? Sure, this is how the original C++ compilers did things, maybe because it was easy, or because the people concerned didn't think of anything better. > By "exceptions don't slow things down", do you mean "a C++ program which never > mentions the words try/catch/throw runs as slowly as one that does mention them" > (I think this is true and I don't like it) or "a true C++ program - compiled > with exception support - is as fast as a C program or a program in a C++ dialect > lacking exceptions at the compiler level" (I wonder how that works)? > > Note: of course I might be wrong and I really want to know the answer to these > questions. Now lets see, this shouldn't be too hard. The key point to remember here is that a try block covers a fixed range of code which always resides within a given range of machine addresses for any given compilation (ignoring PIC here, but that's fixable). So what we need are some good old fashioned tables here. Say start address, end address, catch handler address, maybe stack offsets and other stuff. When the implementation's throw function is called it is trivial to unwind the stack and get the program counter in each method back up the stack, after all this has to be saved anyway for normal operation. So then it just finds each try block by using the program counter addresses to search its try-block tables and get the catch handlers. Given a sensible data structure for this data this can be very quick as well. Of course, try blocks can nest but that's all easy really. The kind of optimisations which prevent a level of pc being saved on the stack are easily resolved. eg. tail call optimisation is irrelevant since you couldn't do that in a try block, inlining doesn't break any of this, etc. Of course, there's an implicit try block around every single code block, which is why you get quite a lot of data. But still no more code in the straight through case. No rocket science here I'm afraid. Cheers, Martin.There's stuff above here
Generated at 23:01 on 06 Feb 2007 by mariachi 0.52