Optimiere deinen Code auf Wartbarkeit Optimiere deinen Code
Optimiere deinen Code – auf Wartbarkeit Optimiere deinen Code auf Wartbarkeit Stefan Mandel © 2018 andrena objects ag
2 Optimiere deinen Code – auf Wartbarkeit Was ist Optimierung? Was ist Refactoring Compiler Entwickler Source Code Analyse Intermediate Language Refactoring Optimierung Machine Code © 2018 andrena objects ag Refactored Code
3 Optimiere deinen Code – auf Wartbarkeit Was ist ähnlich? • Übersetzer Refactoring • A translator or programming language processor is a computer program that performs the translation of a program written in a given programming language into a functionally equivalent program in another computer language (the target language), without losing the functional or logical structure of the original code (the "essence" of each program). Wikipedia © 2018 andrena objects ag The process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. Martin Fowler e d n Ä e n i Ke o v g run n! e t l a h r n Ve
Optimiere deinen Code – auf Wartbarkeit Compiler-Optimierung mit SSA-Zwischensprache • • • SSA-Static Single Assignment • 3 -Address-Form (Ergebnis, Operand 1, Operand 2) • Variablen (Values) dürfen nur einmal zugewiesen werden • Es gibt Seiteneffektoperatoren • Record-Zugriff • Array-Zugriff • Methoden-Aufruf ist eine funktionale Sprache • auf Methodenebene • mit expliziten Seiteneffekte • leicht zu optimieren verwendet z. B. in Java Hotspot, LLVM © 2018 andrena objects ag 4
5 Optimiere deinen Code – auf Wartbarkeit SSA - Transformation int f(int b, int c) { int a = b + c; b = c + 1; int d = b + c; a = a + 1; int e = a + b; return e; } © 2018 andrena objects ag func f(b 1, a 1 : = b 1 b 2 : = c 1 d 1 : = b 2 a 2 : = a 1 e 1 : = a 2 ret e 1 } c 1 ) { + c 1 + 1 + b 2
6 Optimiere deinen Code – auf Wartbarkeit SSA - Transformation int f(int a) { if (a < 0) { a = -a; } return a; } © 2018 andrena objects ag func f(a 1) { if (a 1 < 0) { a 2 : = -a 1 } a 3 : = phi(a 1, a 2) ret a 3 }
Optimiere deinen Code – auf Wartbarkeit SSA - Optimierungen • • • Umordnung von Zuweisungen Propagieren von Konstanten • Wenn a 1 : = 1 • Ersetze alle folgenden a 1 durch 1 Propagieren von Ausdrücken • Wenn a 1 : = b 1 + c 1 • Ersetze alle folgenden b 1 + c 1 durch a 1 Eliminieren von totem Code Algebraische Vereinfachung … © 2018 andrena objects ag 7
8 Optimiere deinen Code – auf Wartbarkeit SSA – Optimierungen (Beispiel) Java int f() int a int b int c if (c a = } { = 1; = 100; = a * b; < 100) { c; return a; } © 2018 andrena objects ag SSA Constant Propagation func f() { a 1 : = 1 b 1 : = 100 c 1 : = a 1 * b 1 if (c 1 < 100) { a 2 : = c 1 } a 3 : = phi(a 1, a 2) ret a 3 } func a 1 b 1 c 1 if f() { : = 100 : = 1 * b 1 (c 1 < 100) { a 2 : = c 1 } } a 3 : = phi(1, a 2) ret a 3
9 Optimiere deinen Code – auf Wartbarkeit SSA – Optimierungen (Beispiel) Constant Propagation Constant Folding Constant Propagation func f() { } b 1 : = 100 c 1 : = 1 * 100 if (c 1 < 100) { a 2 : = c 1 } a 3 : = phi(1, a 2) ret a 3 © 2018 andrena objects ag } c 1 : = 100 if (c 1 < 100) { a 2 : = c 1 } a 3 : = phi(1, a 2) ret a 3 } c 1 : = 100 if (100 < 100) { a 2 : = 100 } a 3 : = phi(1, a 2) ret a 3
10 Optimiere deinen Code – auf Wartbarkeit SSA – Optimierungen (Beispiel) Constant Folding Branch Elimination Phi Elimination func f() { } if (false) { a 2 : = 100 } a 3 : = phi(1, a 2) ret a 3 © 2018 andrena objects ag } if (false) { a 2 : = 100 } a 3 : = phi(1, ) ret a 3 } a 3 : = 1 ret a 3
Optimiere deinen Code – auf Wartbarkeit SSA – Optimierungen (Beispiel) Constant Propagation func f() { a 3 : = 1 ret 1 } © 2018 andrena objects ag ret 1 } 11
12 Optimiere deinen Code – auf Wartbarkeit Refactoring wie Compiler-Optimierung Compiler Entwickler Source Code Analyse Normalisierung SSA Easy Refactoring Form Optimierung Refactoring Machine Code Refactored Code © 2018 andrena objects ag
Optimiere deinen Code – auf Wartbarkeit Easy-Refactoring-Form • • Quellsprache (z. B. Java) • in SSA-Form • mit möglichst wenigen Seiteneffekten Erleichtert • Umordnen und Neugruppieren von Anweisungen • Zugriff auf Zwischenergebnisse • Elementare Refactorings • Extraktion von Funktionen/Parametern/Objekten • Inlinen von Funktionen/Variablen • Einführung von Polymorphie © 2018 andrena objects ag 13
Optimiere deinen Code – auf Wartbarkeit Normalisierung (Beispiele) • https: //github. com/andrena/refactoring. Like. Compilers • Sequences • Simple. Conditionals • Complex. Conditionals • Simple. For. Each. Loops • Simple. While. Loops • Exceptional. Control. Flow • Html. Cleaner (almost real Legacy Code) © 2018 andrena objects ag 14
Optimiere deinen Code – auf Wartbarkeit Refactoring/Optimierung (Beispiele) • https: //github. com/andrena/refactoring. Like. Compilers • Html. Cleaner (almost real Legacy Code) © 2018 andrena objects ag 15
Optimiere deinen Code – auf Wartbarkeit Zusammenfassung • Refactoring: • Wenn ihr die Lösung nicht sofort seht, • Bringt den Code in Easy-Refactoring-Form • Und schaut an welche Möglichkeiten sich danach bieten © 2018 andrena objects ag 16
Optimiere deinen Code – auf Wartbarkeit Fragen? Feedback? Vielen Dank © 2018 andrena objects ag 17
- Slides: 17