One for the C++ coders
Has anybody else noticed that C++ code written to throw exceptions becomes needlessly large under gcc (2.95.3, 3 - 3.3, not yet tried in 4.x)?
Examining the assembly output, it's clear it inserts restore points where they simply aren't needed. For instance, any throw statement anywhere in a function saves a restore point (pretty expensive too, considering all possibly altered registers are pushed). This restore point is always called on function entry, even if the exception is meant to propagate out of the function - thus using a restore point further up the stack anyway.
Perhaps the following is well known and for some reason I missed it, but I've found that simply refactoring thow statements into their own functions reduces the overall code size immensely. For example, something like this
class NullPointer { .... };
class RangeError { .... };
//...
int someFunction(const char* foo, uint32 blah)
{
if (foo==0) {
throw NullPointer();
}
if (blah<5 || blah>20) {
throw RangeError();
}
//...
}
refactored into
extern void throwNull();
extern void throwRange();
int someFunction(const char* foo, uint32 blah)
{
if (foo==0) {
throwNull();
}
if (blah<5 || blah>20) {
throwRange();
}
//...
}
//...
void throwNull() { throw NullPointer(); }
void throwRange() { throw RangeError(); }
can produce significant reductions in code size. I had one C++ source file that implemented the methods for a class. There were about 9 throw statements. Refactoring it in the above manner caused the resultant .o file to be 1/3 the size it was originally :-o
Perhaps this is well known and I was somehow unaware, but heck, it's nice to learn something new :-D
-edit-
Do I know how to party, or what? :lol: