Tuesday, January 18, 2011

Optimization Wars: Problems with CS Education

You may or may not know that I am currently a Computer Science student at Michigan State University. While it may come as no surprise that much of my knowledge of Graphics and Game Dev are largely a consequence of my own initiatives because no large public University is going to tailor its CS program to game dev. Which is totally fine, I expect that.

However, I've become increasingly disturbed with a trend that I believe to be a common denominator across most CS programs that work in any sort of prep for an engineering-type coding position. The trend is a general disregard for optimization. This post (rant maybe?) is essentially inspired from a #AltDevBlogADay by Chris Kosanovich but is fueled from my personal experiences in the University environment.

Chris's post is about the mentality that many software engineers get into where they only want to think in high level terms and don't want to consider optimization. Here's the quote from a conversation that he posts that really does it for me:

"’optimizing’ is what compilers are for.”
Frankly I wasn’t sure if this was a joke so I replied
“compilers will never do as good a job at optimizing as people”
after which all I got for reply was “yeah right”.

This pretty much summarizes the problem that I've found with the University system: we're not really taught optimization in the main curriculum. At least, only by the best professors do serious discussions arise. But perhaps the most enlightening experience that I've had about the overall attitude was in a software engineering class where the "Gang of Four" book is a required text. Several lectures into the class I asked what performance implications a certain design pattern had, to which the professor replied:

"In a professional setting, readability and design are much more important, optimization is not relevant to this course."

What, seriously? For the "Software Engineering" course? I started bringing my laptop to class to work on my games project during that lecture time after we had that particular conversation. I still got a 4.0 for the class, but the class became one of the least relevant classes in a program I've become so proud of here at MSU. I now realize I should have expected the professors response but still... this class is touted and one of the highlights of our program and it's just really misleading in terms of what a code writer should be striving for- even if they aren't going into games.

Chris ends his post with a dream of a mythical college class about hand optimizing code. We can dream... But the honest answer that it's up to students to realize that their programs have been twisted by a field full of people who too often backslide into thinking that their goal is to make their own job "easier" with design patterns and the like, even if it makes their product worse. I've heard people claim over and over again that a portfolio and game programming experience are critical to landing a job in games. I wish they would also mention the importance of optimization... I'm pretty sure it's important to all disciplines ranging from rendering to tools, so why aren't we making a bigger deal about it?

Sometimes I get worried that some of the other coders in the Video Game Specialization with me wouldn't understand why I would bit shift instead of doing integer division by 2. It gives me shivers.


  1. While I agree with the general theme, it is imperative to understand the transformations your compiler is capable of, and the optimization-related restrictions (why the compiler can't optimize the code in some way) and why they arise.

    For example, bit shifting instead of integer division by 2 is an example of the optimization that any (C/C++) compiler does.

    Low-level optimization is quite a complex topic, since it requires knowledge of certain CPU internals (caches, branch prediction, instruction reordering, pipelining, various conflicts & stalls dependent on the target architecture, latency, etc.) and compiler specifics (aliasing, side effects, fp math consistency, etc.). Moreover, while some things are more or less universal (i.e. cache-friendly data - albeit it's made more complicated when additional layers are introduced, such as NUMA), other things are specific to target architectures. So creating a course that teaches low-level optimization is complicated - it's much easier to teach design patterns ;)

  2. "In a professional setting, readability and design are much more important, optimization is not relevant to this course."

    Yes, really, that's what Engineering means. I don't know why it is every CS major is obsessed with hand-optimizing things, but it is almost never the professional thing to do. Almost no code needs speed optimization, proper engineering practice is to understand performance well enough to not do anything stupidly O(n^m) while writing code other humans will be able to understand and improve.