The University of North Carolina at Chapel Hill
COMP 144 Programming Language Concepts
Spring 2002
Lecture 34:
Code Optimization
Felix Hernandez-Campos
April 17
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1
Optimization
• We will discuss code optimization from the point of
view of the compiler and the programmer
• Code improvement is an important phase in
production compilers
– Generate code that runs fast (CPU-oriented)
» In some case, optimize for memory requirements or network
performance
• Optimization is an important task when developing
resource-intensive application
– E.g. Games, Large-scale services
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2
Optimization
• Two lectures on optimization
• References
– Scott’s Chapter 13
– Doug Bell, Make Java fast: Optimize!
» http://www.javaworld.com/javaworld/jw-04-1997/jw-04-optimize
_p.html
– HyperProf- Java profile browser
» http://www.physics.orst.edu/~bulatov/HyperProf/
– Python Performance Tips
» http://manatee.mojam.com/~skip/python/fastpython.html
– Python Patterns - An Optimization Anecdote
» http://www.python.org/doc/essays/list2str.html
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
3
The Golden Rules of Optimization
Premature Optimization is Evil
• Donald Knuth, premature optimization is the root of
all evil
– Optimization can introduce new, subtle bugs
– Optimization usually makes code harder to understand and
maintain
• Get your code right first, then, if really needed,
optimize it
– Document optimizations carefully
– Keep the non-optimized version handy, or even as a
comment in your code
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
4
The Golden Rules of Optimization
The 80/20 Rule
• In general, 80% percent of a program’s execution
time is spent executing 20% of the code
• 90%/10% for performance-hungry programs
• Spend your time optimizing the important 10/20% of
your program
• Optimize the common case even at the cost of
making the uncommon case slower
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
5
The Golden Rules of Optimization
Good Algorithms Rule
• The best and most important way of optimizing a
program is using good algorithms
– E.g. O(n*log) rather than O(n2)
• However, we still need lower level optimization to
get more of our programs
• In addition, asymthotic complexity is not always an
appropriate metric of efficiency
– Hidden constant may be misleading
– E.g. a linear time algorithm than runs in 100*n+100 time
is slower than a cubic time algorithm than runs in n3+10
time if the problem size is small
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
6
Asymptotic Complexity
Hidden Constants
Hidden Contants
3000
2500
Execution Time
2000
100*n+100
1500
n*n*n+10
1000
500
0
0 5 10 15
Problem Size
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
7
General Optimization Techniques
• Strength reduction
– Use the fastest version of an operation
– E.g.
x >> 2 instead of x / 4
x << 1 instead of x * 2
• Common sub expression elimination
– Eliminate redundant calculations
– E.g.
double x = d * (lim / max) * sx;
double y = d * (lim / max) * sy;
double depth = d * (lim / max);
double x = depth * sx;
double y = depth * sy;
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
8
General Optimization Techniques
• Code motion
– Invariant expressions should be executed only once
– E.g.
for (int i = 0; i < x.length; i++)
x[i] *= Math.PI * Math.cos(y);
double picosy = Math.PI * Math.cos(y);
for (int i = 0; i < x.length; i++)
x[i] *= picosy;
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
9
General Optimization Techniques
• Loop unrolling
– The overhead of the loop control code can be reduced by
executing more than one iteration in the body of the loop
– E.g.
double picosy = Math.PI * Math.cos(y);
for (int i = 0; i < x.length; i++)
x[i] *= picosy;
double picosy = Math.PI * Math.cos(y);
for (int i = 0; i < x.length; i += 2) {
x[i] *= picosy;
x[i+1] *= picosy; A efficient “+1” in array
indexing is required
}
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
10
Compiler Optimizations
• Compilers try to generate good code
– I.e. Fast
• Code improvement is challenging
– Many problems are NP-hard
• Code improvement may slow down the compilation
process
– In some domains, such as just-in-time compilation,
compilation speed is critical
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
11
Phases of Compilation
• The first three
phases are
language-
dependent
• The last two
are machine-
dependent
• The middle
two
dependent on
neither the
language nor
COMP 144 Programming Language Concepts
the machine
Felix Hernandez-Campos
12
Phases
• Generating
highly
optimized is a
complicated
process
• We will
concentrate
on execution
speed
optimization
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
13
Phases
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
14
Example
Control Flow Graph
• Basic blocks are
maximal-length set
of sequential
operations
– Operations on a set
of virtual registers
» Unlimited
» A new one for each
computed value
• Arcs represent
interblock control
flow
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
15
Peephole Optimization
• Simple compiler do not perform machine-
independent code improvement
– They generates naïve code
• It is possible to take the target hole and optimize it
– Sub-optimal sequences of instructions that match an
optimization pattern are transformed into optimal
sequences of instructions
– This technique is known as peephole optimization
– Peephole optimization usually works by sliding a window
of several instructions (a peephole)
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
16
Peephole Optimization
Common Techniques
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
17
Peephole Optimization
Common Techniques
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
18
Peephole Optimization
Common Techniques
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
19
Peephole Optimization
Common Techniques
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
20
Peephole Optimization
• Peephole optimization is very fast
– Small overhead per instruction since they use a small,
fixed-size window
• It is often easier to generate naïve code and run
peephole optimization than generating good code!
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
21
Reading Assignment
• Read Scott
– Ch. 13 intro
– Sect. 13.1
– Sect. 13.2
• Doug Bell, Make Java fast: Optimize!
– http://www.javaworld.com/javaworld/jw-04-1997/jw
-04-optimize_p.html
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
22