Subj : Re: OO compilers and efficiency To : comp.programming From : Gerry Quinn Date : Wed Jul 20 2005 12:47 am In article , brian@rohan.sdsu.edu says... > Is there a good approachable book that covers OO compilers? Something > like a Red Dragon for the ages? > > OO seems like it must be very wasteful. Extending a class usually > means taking the super class' existing method and tacking on some > new secret sauce stuff. If the original method works, nothing needs to be added. If it doesn't, you'd have to change it anyway. > So is this wrapping one stack frame on top of another? First the > subclasses method gets called then super's? Are compilers generally > smart enough to inline the two calls into one? They might or they might not. > Getter and setter functions are another simple example of what seems > to be waste. The method call works like this. The existing register > states are pushed onto the stack. The call is made. The getter > method returns a simple value on the stack. The call is returned. > The registers are popped. That must be about 20 instructions for > what might only take one, a simple memory read, or better yet, a > register read. That is a more likely candidate for auto-inlining, I would guess. > Anyway, those are some of the things that kind of nag at me. > I think it's a safe statement to say C can beat any OO compiled > program pound for pound given the same programmer skill and > adherence to language goals. > > Does everyone drop into C for critical code? You can use getter and setter functions in C too if you want. Or not use them in C++. I don't see their use as a specific indicator of OO. If your derived class is doing heavy calculation with certain members, they are normally its own. So you can write it like C if you want. > Or am I the only one who worries about this stuff? Is it a > concern or should I kind of push it back into the recesses > of my mind and say the wastefulness is part of the tradeoff > for programming OO code? There's a certain cost in terms of extra aliasing etc. going from C to C++. If it looks likely to hurt, you can write pretty much in C style where it counts. But it's unlikely that more than about 1% of the code - even of an action game - is so time critical that it makes a difference. - Gerry Quinn .