Classic Performance Myths

Wow, my head is spin­ning! It may very well be that sec­ond glass of wine, but I think it’s largely the fault of Joseph M. Newcomer’s out­stand­ing article/​blog post Mythol­ogy in C++: Excep­tions are Expen­sive.

New­comer starts out with some flat­ter­ing words about the PDP-​11 mini­com­puter where the C lan­guage matured into the lan­guage many of us learned in school (at least the K&R ver­sion of it). But after a cou­ple para­graphs of pleas­antries, the reader is caught in a whirl­wind of facts, fig­ures, and quite con­vinc­ing argu­ments that per­haps some tru­isms pro­gram­mers are taught don’t apply to mod­ern CPU architectures.

For exam­ple, New­comer points out the pop­u­lar for(;;) idiom for infi­nite loops in C is pre­ferred over while(true) because the orig­i­nal PDP-​11 C com­piler gen­er­ated inef­fi­cient code for the lat­ter. But mod­ern com­pil­ers, and even later com­pil­ers for the PDP-​11 knew how to to make the while ver­sion more effi­cient than the for one.

As the title implies, the bulk of the arti­cle deals with excep­tion han­dling in C++, the most famous object-​oriented off­shoot of the orig­i­nal C lan­guage. For years, we’ve heard about how expen­sive excep­tion han­dling is, while chip design­ers and oper­at­ing sys­tem design­ers worked to make sure excep­tions only had a tan­gi­ble effect when they actu­ally occur.

I’ll admit, tonight’s caber­net got in the way of me absorb­ing all the expla­na­tions. But I did walk away with the sense that New­comer has only peeled away one layer of the onion here, and we’re going to start see­ing more rev­e­la­tions like this, hope­fully backed by the same exten­sive research and testing.

On a per­sonal note, this reminds me of one C++ class I took in col­lege. The teacher had just fin­ished explain­ing object prop­er­ties, and I raised my hand to ask what the syn­tax was for spec­i­fy­ing the width of each prop­erty in bits (bit fields). You can do this with structs in C, and I loved using this fea­ture to cram two small val­ues in one 8-​bit byte, but my teacher was puz­zled as to why any­one would need to do that. I started research­ing why this was left out of C++ and came away with a deeper under­stand­ing of how hav­ing megabytes of RAM avail­able made com­put­ers per­form faster (they don’t have to deal with hacky bit field opti­miza­tions like that, among other things) and how mod­ern com­pil­ers opti­mize for align­ment.

There’s a good chance the com­piler was laugh­ing at my “opti­miza­tions” and ignor­ing them.