Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm curious - and excuse me for my ignorance - but what is "unrolling code"?

http://fabiensanglard.net/duke3d/duke3d_code_review_unrolled...



Usually it simply means repeating the loop body N times so that the amount of loop iterations can be reduced to 1/N.

For example, if you know that the iteration count is divisible by 4, you could do something like:

  int unrolledN = n / 4;
  for (int unrolledI = 0; unrolledI < unrolledN; unrolledI++) {
    int i = unrolledI * 4;
    // loop body...
    i++;
    // loop body...
    i++;
    // loop body...
    i++;
    // loop body...
  }
This wouldn't really offer any advantage over the plain loop, though. Next you'd need to reorganize the loop body so that e.g. memory reads for all iterations would occur at the start of the unrolled loop. This kind of optimizations can offer significant performance increases because you get more control over what the CPU is doing within the loop, but they also depend greatly on the target platform. Even different x86 processors can be very different in this respect, so unrolling can become a disoptimization easily.


In this case he sort of replaces some function calls with bodies of these functions, so the actual program flow would be clearer to read.


Broadly speaking, it's removing flow control from the code in order to make it easier to read or faster to execute.

In this case, I think he's replaced function calls with the body of those functions. For example, "displayrooms" is immediately followed by { } surrounding the contents of what "displayrooms" actually does: interpolate, animate, and so on. This means you can read just the one source file and know what's being executed, instead of having to read the source of displayrooms.c and various other source files separately.

Another common form of "unrolling" is repeating the body of a loop some number of times. See, for example, http://en.wikipedia.org/wiki/Duff%27s_device . Basically, by reducing the number of branches (and therefore potential pipeline flushes) you can decrease computation time.


I think what he means here is that all the code that is abstracted into functions etc. is "unrolled" (at some sensible level) into one main function so you can see the complete flow of the program without searching around.


Not sure, but it could be something like Duff's Device. Look it up, it can be used for various things, like drawing and stuff.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: