Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 37 additions & 50 deletions textbook/07-optimization.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

\pagebreak

<!---
Expand Down Expand Up @@ -46,36 +45,6 @@ TOPICS:
Optimization is the process of modifying a software to make an aspect of it work more efficiently.
This means that the code will run more quickly and use up less resources than it already does.
7.2.1 Peephole
Peephole Optimization is a type of optimization that works on very small sets of instructions in generated code at a time.
It then refactors sets so that it can be replaced by faster, more efficient code segments.
7.2.1.2 Connects Adjacent Instructions to See If They Can Be Compressed
This optimization method will group together instructions to see if they can be compressed.
It will then look at how the memory is being handled and remove unnecessary lines of code.
7.2.1.1 Performed after Machine Code Has Been Generated
This is normally performed after the machine code has been generated.
It will optimize the execution of the instruction at the lowest level.
7.2.1.1.1 EXAMPLE
Take the instruction set:
a = b + c;
d = a + e;

What is actually implemented in machine code is:
MOV b, R0
ADD c, R0
MOV R0, a
MOV a, R0
ADD e, R0
MOV R0,d

This machine can be optimised.
It can be condensed to:
MOV b, R0
ADD c, R0
MOV R0, a
ADD e, R0
MOV R0, d

By taking the code at its base level and optimising it there, the code will run much faster and have to execute even less commands to achieve the same result.
7.2.2 Local
7.2.3 Loop
7.2.3.1 Act upon Loops
Expand Down Expand Up @@ -183,9 +152,6 @@ Examples:


### Complexity
<!--
7.1.2 Complexity
-->

#### Many Optimizations Are NP-Complete
<!--
Expand All @@ -205,9 +171,6 @@ Memory limitations exist for optimization, as optimization is a cpu-heavy and me
In addition, even the programmer's time to wait for a compiler to complete also places restrictions on optimization.

### Effectiveness
<!--
7.1.3 Effectiveness
-->

#### What is the Target Architecture?
<!--
Expand Down Expand Up @@ -262,19 +225,11 @@ A good example is like writing in assembly; you cannot write for an intel proces
It is good to know the hardware design as it leads to an effective program.

#### Host Architecture
<!--
7.1.3.2 Host Architecture
-->

##### The Machine Doing the Compilation
<!--
7.1.3.2.1 The Machine Doing the Compilation
-->


##### Factors
<!--
7.1.3.2.2 Factors
-->

###### CPU Speed
<!--
Expand Down Expand Up @@ -302,9 +257,6 @@ The read/write spead of the disk can directly effect the rate at which these ope
While disk read/write speeds do not have as large of an impact in compilation times as CPU and RAM performance, they still have an impact on the overall execution time.

##### Program Usage
<!--
7.1.3.2.3 Program Usage
-->

###### Release vs Debugging
<!--
Expand All @@ -314,4 +266,39 @@ The read/write spead of the disk can directly effect the rate at which these ope
In a debug build, the complete symbolic debug information is emitted for testing and debugging purposes.
Code optimization is not a priority in debug releases.
Release builds do not emit the symbolic debugging info, reducing the size of the final executable file.
The speed of execution may vary between debug and release builds depending on the compiler.
The speed of execution may vary between debug and release builds depending on the compiler.

###### Peephole Optimization
Peephole Optimization is a type of optimization that works on very small sets of instructions in generated code at a time.
It then refactors sets so that it can be replaced by faster, more efficient code segments.
<!--
7.2.1.2 Connects Adjacent Instructions to See If They Can Be Compressed
This optimization method will group together instructions to see if they can be compressed.
It will then look at how the memory is being handled and remove unnecessary lines of code.
7.2.1.1 Performed after Machine Code Has Been Generated
This is normally performed after the machine code has been generated.
It will optimize the execution of the instruction at the lowest level.
-->
Example:
Take the instruction set:
a = b + c;
d = a + e;

What is actually implemented in machine code is:
MOV b, R0
ADD c, R0
MOV R0, a
MOV a, R0
ADD e, R0
MOV R0,d

This machine can be optimised.

It can be condensed to:
MOV b, R0
ADD c, R0
MOV R0, a
ADD e, R0
MOV R0, d

By taking the code at its base level and optimising it there, the code will run much faster and have to execute even less commands to achieve the same result.