Why are compilers so stupid?

In my opinion, I don’t believe it’s the job of the compiler to fix what is, honestly, bad coding. You have, quite explicitly, told the compiler you want that first loop executed. It’s the same as:

x = 0
sleep 6 // Let's assume this is defined somewhere.
print x

I wouldn’t want the compiler removing my sleep statement just because it did nothing. You may argue that the sleep statement is an explicit request for a delay whereas your example is not. But then you will be allowing the compiler to make very high-level decisions about what your code should do, and I believe that to be a bad thing.

Code, and the compiler that processes it, are tools and you need to be a tool-smith if you want to use them effectively. How many 12″ chainsaws will refuse to try cut down a 30″ tree? How many drills will automatically switch to hammer mode if they detect a concrete wall?

None, I suspect, and this is because the cost of designing this into the product would be horrendous for a start. But, more importantly, you shouldn’t be using drills or chainsaws if you don’t know what you’re doing. For example: if you don’t know what kickback is (a very easy way for a newbie to take off their arm), stay away from chainsaws until you do.

I’m all for allowing compilers to suggest improvements but I’d rather maintain the control myself. It should not be up to the compiler to decide unilaterally that a loop is unnecessary.

For example, I’ve done timing loops in embedded systems where the clock speed of the CPU is known exactly but no reliable timing device is available. In that case, you can calculate precisely how long a given loop will take and use that to control how often things happen. That wouldn’t work if the compiler (or assembler in that case) decided my loop was useless and optimized it out of existence.

Having said that, let me leave you with an old story of a VAX FORTRAN compiler that was undergoing a benchmark for performance and it was found that it was many orders of magnitude faster than its nearest competitor.

It turns out the compiler noticed that the result of the benchmark loops weren’t being used anywhere else and optimized the loops into oblivion.

Leave a Comment