Professor Brailsford of Computerphile has an interesting short video on unrolling loops. He motivates the discussion by relating a story about the introduction of the Laserwriter. John Warnock, from Adobe, and Steve Jobs planned to show off the Laserwriter’s capabilities by having it print a U.S. tax form. The problem was that the form is visually complicated and took a minute and 20 seconds to print.
Jobs felt that that was too long for an effective demonstration so Warnock took the PostScript file and removed all the procedure calls and unrolled the loops. The new file printed in 20 seconds, only 25% of the initial version.
Brailsford goes on to explain how unrolling loops can dramatically increase the speed of your code. Of course, doing so violates all sorts of rules about properly structuring your code so it’s something you should do only for critical code paths. He also mentions that once you get away from trivial programs, manually unrolling loops is too burdensome so he introduces the notion of translation programs to do the unrolling.
He demonstrates all this by writing a short PostScript program and then unrolling the loop inside it. His notes for the video contain the PostScript program, the flattened version of it, and the PostScript program to automatically unroll loops.
It’s an excellent video and just short of 10 minutes so it’s easy to find time for it. Don’t worry about the program being in PostScript; Brailsford explains everything you need to know.