Professional Documents
Culture Documents
Chapter 3: Code Optimization and Code Version Control
Chapter 3: Code Optimization and Code Version Control
Strength reduction
Replace expensive operations
E.g., x := x * 8 x := x << 3
Code Optimization Techniques
Copy propagation
Extension of constant propagation
After y is assigned to x, use y to replace x till x is assigned again
Example
x := y; s := y * f(y)
s := x * f(x)
Reduce the copying
If y is reassigned in between, then this action cannot be performed
Code Optimization Techniques
Common subexpression elimination
Example:
a := b + c a := b + c
c := b + c c := a
d := b + c d := b + c
Code Optimization Techniques
Unreacheable code elimination
Construct the control flow graph
Unreachable code block will not have an incoming edge
After constant propagation/folding, unreachable branches can be
eliminated
Dead code elimination
Ineffective statements
x := y + 1 (immediately redefined, eliminate!)
y := 5 y := 5
x := 2 * z x := 2 * z
A variable is dead if it is never used after last definition
Eliminate assignments to dead variables
Need to do data flow analysis to find dead variables
Code Optimization Techniques
Function inlining
Replace a function call with the body of the function
Save a lot of copying of the parameters, return address, etc.
Function cloning
Create specialized code for a function for different calling
parameters
Code Optimization Techniques
Loop optimization
Consumes 90% of the execution time
a larger payoff to optimize the code within a loop
Techniques
Loop invariant detection and code motion
Strength reduction in loops
Loop unrolling
Loop peeling
Code Optimization Techniques
Loop invariant detection and code motion
If the result of a statement or expression does not change within a
loop, and it has no external side-effect
Computation can be moved to outside of the loop
Example
for (i=0; i<n; i++) a[i] := a[i] + x/y;
Three address code
for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; }
c := x/y;
for (i=0; i<n; i++) a[i] := a[i] + c;
Code Optimization Techniques
Strength reduction in loops
Example
s := 0; for (i=0; i<n; i++) { v := 4 * i; s := s + v; )
s := 0; for (i=0; i<n; i++) { v := v + 4; s := s + v; )
Code Optimization Techniques
Loop unrolling
The goal of loop unwinding is to increase a program's speed by
reducing or eliminating instructions that control the loop, such as
pointer arithmetic and "end of loop" tests on each iteration; reducing
branch penalties; as well as hiding latencies including the delay in
reading data from memory.
Execute loop body multiple times at each iteration
Get rid of the conditional branches, if possible
Space time tradeoff: Increase in code size, reduce some instructions
Loop peeling
Loop peeling is a special case of loop splitting which splits any
problematic first (or last) few iterations from the loop and performs
them outside of the loop body.
Version Control
Why version control?
Scenario 1:
Your program is working
You change “just one thing”
Your program breaks
You change it back
Your program is still broken--why?
18
Version control for teams
Scenario:
You change one part of a program--it works
Your co-worker changes another part--it works
You put them together--it doesn’t work
Some change in one part must have broken something in the other
part
What were all the changes?
19
Teams (part 2)
Scenario:
You make a number of improvements to a class
Your co-worker makes a number of different improvements to the same
class
How can you merge these changes?
20
Version control systems
A version control system (often called a source code
control system) does these things:
Keeps multiple (older and newer) versions of everything (not just
source code)
Requests comments regarding every change
Allows “check in” and “check out” of files so you know which
files someone else is working on
Displays differences between versions
21
Control the process automatically
Manage these things using a version control system (VCS)
A version control system is a system which allows for the
management of a code base.
22
Details of the process
Files are kept in a repository
Repositories can be local or remote to the user
The user edits a copy called the working copy
Changes are committed to the repository when
the user is finished making changes
Other people can then access the repository to get the
new code
Can also be used to manage files when working across
multiple computers
23
24
Centralised Version Control
25
26
Distributed Version Control
Each client (essentially) holds a complete copy of the code
base.
Code is shared between clients by push/pulls
Advantages: Many operations cheaper. No single point of failure
Disadvantages: A bit more complicated!
27
28
What is Git
29
What is Git
https://www.youtube.com/watch?v=9GKpbI1siow
https://www.youtube.com/watch?v=DqtZUvmPmo4
30