Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1187

Appendix C: Notes on C


.text
.p2align 4,,15
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $1196, 12(%esp)
movl $52, 8(%esp)
movl $10, 4(%esp)
movl $.LC0, (%esp)
call printf
movl %ebp, %esp
popl %ebp
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"

The computation is no longer made at runtime because the results are already known. However, faster
program execution^6 is not the only benefit produced by this optimization step. The resulting code now
uses only one variable (z) because the two temporary variables (xandy) are superfluous. This not only
shortens execution time, but also saves storage space — a major consideration in large programs with a
large number of variables.

Loop Optimization


Code in loops may be executed repeatedly and therefore merits thorough optimization because speed
gains are particularly noticeable. If a loop is iterated 1,000 times and the runtime of the loop body is
shortened by one thousandth of a second as a result ofoptimization, total program runtime is one second
shorter. One second may not seem to be much. However, the benefits are best assessed by considering
very time-critical kernel actions such as task switching or long-running programs such as physical simu-
lation. While execution time in the latter case may differ by hours or even days, savings of fractions of a
second are a desirable goal in the former case — afterall, task switches are performed at short intervals
(which are beyond human perception) to give the illusion of parallel program execution.

This optimization feature is not difficult to understand and, in technical terms, appears to be relatively
simple. This feature can be illustrated via the following short sample program:

int count;

for (count = 0; count < 3; count++) {
printf("Pass: %d\n", count);
}

The loop is iterated three times in succession and outputs the number of the current pass (0, 1, or 2).
What can be optimized here? The loop is implemented in assembler code by incrementing thecount

(^6) Note that compilation naturally takes longer when results are computed at compilation time. This is a common aspect of all opti-
mization efforts — execution speed is boosted at the expense of compilation speed. However, this is acceptable because compilation
is performed once only and execution takes place regularly.

Free download pdf