SlideShare a Scribd company logo
1 of 75
Download to read offline
Improving Android
Performance
#perfmatters
Raimon Ràfols
Agenda
- Who am I?
- Java and VMs
- Examples - Do & dont's
- Tooling
Who am I?
- Mobile Software Engineering Manager at
Imagination Technologies (@imgtec)
- Twitter: @rrafols
- http://blog.rafols.org
Disclaimer
This presentation contains bytecode
Why do we need to talk about java?
*.java → [javac] → *.class
*.class → [dx] → dex file
Android → Java
Android → javac !
Javac vs other compilers
Compilers
Produces optimised code for
target platform
Javac
Doesn't optimise anything
Javac
Doesn't know on which
architecture will the code
be executed
Java bytecode is stack based
Easy to interpret
Not the most optimal solution
(regarding performance)
Dalvik VM and ART are register
based architectures
Stack based integer addition
iload_3
iload_2
iadd
istore_2
Register based integer addition
add-int v2, v3, #1
Java VM (JVM)
Only the JVM knows on which
architecture is running
Java VM (JVM)
All optimisations are left to be
done by the JVM
Maybe takes this concept a bit too
far...
GCC compiler
#include <stdio.h>
int main() {
int a = 10;
int b = 1 + 2 + 3 + 4 + 5 + 6 + a;
printf("%dn", b);
}
…
movl $31, %esi
call _printf
…
* Using gcc & -O2 compiler option
javac
public static void main(String args[]) {
int a = 10;
int b = 1 + 2 + 3 + 4 + 5 + 6 + a;
System.out.println(b);
}
0: bipush 10
2: istore_1
3: bipush 21
5: iload_1
6: iadd
7: istore_2
...
javac
public static void main(String args[]) {
int a = 10;
int b = 1 + 2 + 3 + 4 + 5 + a + 6;
System.out.println(b);
}
0: bipush 10
2: istore_1
3: bipush 15
5: iload_1
6: iadd
7: bipush 6
9: iadd
10: istore_2
...
javac
public static void main(String args[]) {
int a = 10;
int b = a + 1 + 2 + 3 + 4 + 5 + 6;
System.out.println(b);
}
javac
public static void main(String args[]) {
int a = 10;
int b = a + 1 + 2 + 3 + 4 + 5 + 6;
System.out.println(b);
}
0: bipush 10
2: istore_1
3: iload_1
4: iconst_1
5: iadd
6: iconst_2
7: iadd
8: iconst_3
9: iadd
10: iconst_4
11: iadd
12: iconst_5
13: iadd
14: bipush 6
16: iadd
17: istore_2
what?
Dalvik VM / ART
Examples
Do & Dont's
Autoboxing
Transparent to the developer but
compiler adds some 'extra' code
Autoboxing
long total = 0;
for(int i = 0; i < N; i++) {
total += i;
}
4: lconst_0
5: lstore_3
6: iconst_0
7: istore 5
9: iload 5
11: ldc #6;
13: if_icmpge 28
16: lload_3
17: iload 5
19: i2l
20: ladd
21: lstore_3
22: iinc 5,1
25: goto 9
Autoboxing
Long total = 0;
for(Integer i = 0; i < N; i++) {
total += i;
}
9: iconst_0
10: invokestatic #4; //Method java/lang/Integer.valueOf:
(I)Ljava/lang/Integer;
13: astore 4
15: aload 4
17: invokevirtual #5; //Method java/lang/Integer.intValue:()I
20: ldc #6; //int 10000000
22: if_icmpge 65
25: aload_3
26: invokevirtual #7; //Method java/lang/Long.longValue:()J
29: aload 4
31: invokevirtual #5; //Method java/lang/Integer.intValue:()I
34: i2l
35: ladd
36: invokestatic #3; //Method java/lang/Long.valueOf:
(J)Ljava/lang/Long;
39: astore_3
40: aload 4
42: astore 5
44: aload 4
46: invokevirtual #5; //Method java/lang/Integer.intValue:()I
49: iconst_1
50: iadd
51: invokestatic #4; //Method java/lang/Integer.valueOf:
(I)Ljava/lang/Integer;
54: dup
55: astore 4
57: astore 6
59: aload 5
61: pop
62: goto 15
Autoboxing
●
This is what that code is actually doing:
Long total = 0;
for(Integer i = Integer.valueOf(0);
i.intValue() < N;
i = Integer.valueOf(i.intValue() + 1)) {
total = Long.valueOf(total.longValue() + (long)i.intValue())
}
Sorting
The easy way
Sorting
Two easy ways to sort:
• Arrays.sort(array);
• Collections.sort(list);
Lets evaluate the performance as both data
type & sorting algorithms are different.
Sorting
• Arrays.sort(arrays) uses a modified version
of a 3-way quicksort.
• Collections.sort(list) uses a iterative
implementation of mergesort (stable)
Sorting
Arrays.sort(array) uses the same
implementation as
Collections.sort(list) for non-
primitive data types.
Loops
What's going on under the hood
Loops - List
ArrayList<Integer> list = new …
static long loopStandardList() {
long result = 0;
for(int i = 0; i < list.size(); i++) {
result += list.get(i);
}
return result;
}
Loops - List (Java bytecode)
7: lload_0
8: getstatic #26 // Field list:Ljava/util/ArrayList;
11: iload_2
12: invokevirtual #54 // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
15: checkcast #38 // class java/lang/Integer
18: invokevirtual #58 // Method java/lang/Integer.intValue:()I
21: i2l
22: ladd
23: lstore_0
24: iinc 2, 1
27: iload_2
28: getstatic #26 // Field list:Ljava/util/ArrayList;
31: invokevirtual #61 // Method java/util/ArrayList.size:()I
34: if_icmplt 7
Loops - List (Dalvik bytecode)
0003: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.list:Ljava/util/ArrayList;
0005: invoke-virtual {v3}, Ljava/util/ArrayList;.size:()I
0008: move-result v3
0009: if-lt v0, v3, 000c // +0003
000b: return-wide v1
000c: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.list:Ljava/util/ArrayList;
000e: invoke-virtual {v3, v0}, Ljava/util/ArrayList;.get:(I)Ljava/lang/Object;
0011: move-result-object v3
0012: check-cast v3, Ljava/lang/Integer;
0014: invoke-virtual {v3}, Ljava/lang/Integer;.intValue:()I
0017: move-result v3
0018: int-to-long v3, v3
0019: add-long/2addr v1, v3
001a: add-int/lit8 v0, v0, #int 1 // #01
001c: goto 0003 // -0019
Loops - foreach
ArrayList<Integer> list = new …
static long loopForeachList() {
long result = 0;
for(int v : list) {
result += v;
}
return result;
}
Loops - foreach (Java bytecode)
12: aload_3
13: invokeinterface #70, 1 // InterfaceMethod java/util/Iterator.next:()
18: checkcast #38 // class java/lang/Integer
21: invokevirtual #58 // Method java/lang/Integer.intValue:()I
24: istore_2
25: lload_0
26: iload_2
27: i2l
28: ladd
29: lstore_0
30: aload_3
31: invokeinterface #76, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z
36: ifne 12
Loops - foreach (Dalvik bytecode)
0008: invoke-interface {v4}, Ljava/util/Iterator;.hasNext:()Z
000b: move-result v3
000c: if-nez v3, 000f // +0003
000e: return-wide v0
000f: invoke-interface {v4}, Ljava/util/Iterator;.next:()Ljava/lang/Object;
0012: move-result-object v3
0013: check-cast v3, Ljava/lang/Integer;
0015: invoke-virtual {v3}, Ljava/lang/Integer;.intValue:()I
0018: move-result v2
0019: int-to-long v5, v2
001a: add-long/2addr v0, v5
001b: goto 0008 // -0013
Loops - Array
static int[] array = new ...
static long loopStandardArray() {
long result = 0;
for(int i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
Loops - Array (Java bytecode)
7: lload_0
8: getstatic #28 // Field array:[I
11: iload_2
12: iaload
13: i2l
14: ladd
15: lstore_0
16: iinc 2, 1
19: iload_2
20: getstatic #28 // Field array:[I
23: arraylength
24: if_icmplt 7
Loops - Array (Dalvik bytecode)
0003: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I
0005: array-length v3, v3
0006: if-lt v0, v3, 0009 // +0003
0008: return-wide v1
0009: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I
000b: aget v3, v3, v0
000d: int-to-long v3, v3
000e: add-long/2addr v1, v3
000f: add-int/lit8 v0, v0, #int 1 // #01
0011: goto 0003 // -000e
Loops - size cached
static int[] array = new ...
static long loopStandardArraySizeStored() {
long result = 0; int length = array.length;
for(int i = 0; i < length; i++) {
result += array[i];
}
return result;
}
Loops - size stored (Java bytecode)
12: lload_0
13: getstatic #28 // Field array:[I
16: iload_3
17: iaload
18: i2l
19: ladd
20: lstore_0
21: iinc 3, 1
24: iload_3
25: iload_2
26: if_icmplt 12
Loops - size stored(Dalvik bytecode)
0006: if-lt v0, v1, 0009 // +0003
0008: return-wide v2
0009: sget-object v4, Lcom/rrafols/bcndevcon/Loop;.array:[I
000b: aget v4, v4, v0
000d: int-to-long v4, v4
000e: add-long/2addr v2, v4
000f: add-int/lit8 v0, v0, #int 1 // #01
0011: goto 0006 // -000b
Loops - backwards
static int[] array = new ...
static long loopStandardArrayBackwards() {
long result = 0;
for(int i = array.length - 1; i >= 0; i--) {
result += array[i];
}
return result;
}
Loops - backwards (Java bytecode)
12: lload_0
13: getstatic #28 // Field array:[I
16: iload_2
17: iaload
18: i2l
19: ladd
20: lstore_0
21: iinc 2, -1
24: iload_2
25: ifge 12
Loops - backwards(Dalvik bytecode)
0007: if-gez v0, 000a // +0003
0009: return-wide v1
000a: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I
000c: aget v3, v3, v0
000e: int-to-long v3, v3
000f: add-long/2addr v1, v3
0010: add-int/lit8 v0, v0, #int -1 // #ff
0012: goto 0007 // -000b
String concatenation
The evil + sign
String concatenation
Java allows us to concatenate Strings using
the + operator to make our lifes easier. As
what happened before with autoboxing the
compiler will generate some additional code.
String concatenation
String str = "";
for(int i = 0; i < ITERATIONS; i++) {
str += ANY_OTHER_STRING;
}
String concatenation
8: new #26 // class java/lang/StringBuilder
11: dup
12: aload_1
13: invokestatic #28 // Method java/lang/String.valueOf:
(Ljava/lang/Object;)Ljava/lang/String;
16: invokespecial #34 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
19: ldc #11 // String ANY_OTHER_STRING
21: invokevirtual #37 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)
24: invokevirtual #41 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
27: astore_1
28: iinc 2, 1
31: iload_2
32: bipush ITERATIONS
34: if_icmplt 8
String concatenation
• It's creating a StringBuilder Object in every iteration of
the loop. It's the same as the following code:
String str = "";
for(int i = 0; i < ITERATIONS; i++) {
StringBuilder sb = new StringBuilder(String.valueOf(str));
sb.append(ANY_OTHER_STRING);
str = sb.toString();
}
String concatenation - alternatives
• Use String.concat()
• Concat cost is O(N) + O(M) - (N,M) length of each
String
• Concat returns a new String Object.
String str = "";
for(int i = 0; i < ITERATIONS; i++) {
str = str.concat(ANY_OTHER_STRING);
String concatenation - alternatives
• Use StringBuilder
• StringBuffer.append cost is O(M) amortized time (M
length of appended String)
• Avoids creation of new objects.
StringBuilder sb = new StringBuilder()
for(int i = 0; i < ITERATIONS; i++) {
sb.append(ANY_OTHER_STRING);
}
str = sb.toString();
String concatenation - alternatives
• StringBuilder is the not thread safe
implementation of StringBuffer.
• No need to add synchronization overhead if
it's not going to be used by multiple threads.
Tooling
Tooling - Disassembler
Java
• javap -c <classfile>
Android:
• Dexdump -d <dexfile>
• Smali - https://code.google.com/p/smali/
Tooling - Obfuscation
• Proguard & Dexguard
o Proguard & Dexguard are code obfuscators but they
also do a good job on optimizing at bytecode level,
reducing overhead, eliminating dead code, inlining
methods, …
o Proguard is free and comes with the android sdk,
needs to be enabled by just uncommenting one line
in project.properties
Tooling - Performance measurement
• Avoid doing manual performance tests with
System.currentTimeMillis() (although gives a
good estimate) and multiple tests in one run.
• JIT might be evil!
Performance measurements
Avoid doing multiple tests in one run
JIT might be evil!
Do not trust the compiler!
@rrafols
http://blog.rafols.org

More Related Content

What's hot

Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKGuardSquare
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS EngineChengHui Weng
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio
 
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...Asuka Nakajima
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
VLSI Lab manual PDF
VLSI Lab manual PDFVLSI Lab manual PDF
VLSI Lab manual PDFUR11EC098
 
第二回CTF勉強会資料
第二回CTF勉強会資料第二回CTF勉強会資料
第二回CTF勉強会資料Asuka Nakajima
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondGuardSquare
 
C++ programming with jni
C++ programming with jniC++ programming with jni
C++ programming with jniPeter Hagemeyer
 
EC6612 VLSI Design Lab Manual
EC6612 VLSI Design Lab ManualEC6612 VLSI Design Lab Manual
EC6612 VLSI Design Lab Manualtamil arasan
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performanceDuoyi Wu
 
Digital system design practical file
Digital system design practical fileDigital system design practical file
Digital system design practical fileArchita Misra
 
from java to c
from java to cfrom java to c
from java to cVõ Hòa
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jniYongqiang Li
 
Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。Mr. Vengineer
 
Vlsi lab manual exp:1
Vlsi lab manual exp:1Vlsi lab manual exp:1
Vlsi lab manual exp:1komala vani
 
IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011iamdvander
 

What's hot (20)

Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around Disney
 
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
VLSI Lab manual PDF
VLSI Lab manual PDFVLSI Lab manual PDF
VLSI Lab manual PDF
 
第二回CTF勉強会資料
第二回CTF勉強会資料第二回CTF勉強会資料
第二回CTF勉強会資料
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyond
 
C++ programming with jni
C++ programming with jniC++ programming with jni
C++ programming with jni
 
VLSI lab manual
VLSI lab manualVLSI lab manual
VLSI lab manual
 
EC6612 VLSI Design Lab Manual
EC6612 VLSI Design Lab ManualEC6612 VLSI Design Lab Manual
EC6612 VLSI Design Lab Manual
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performance
 
Digital system design practical file
Digital system design practical fileDigital system design practical file
Digital system design practical file
 
Programs of VHDL
Programs of VHDLPrograms of VHDL
Programs of VHDL
 
Learning from 6,000 projects mining specifications in the large
Learning from 6,000 projects   mining specifications in the largeLearning from 6,000 projects   mining specifications in the large
Learning from 6,000 projects mining specifications in the large
 
from java to c
from java to cfrom java to c
from java to c
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jni
 
Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。
 
Vlsi lab manual exp:1
Vlsi lab manual exp:1Vlsi lab manual exp:1
Vlsi lab manual exp:1
 
IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011
 

Viewers also liked

μακεδονικός αγώνας
μακεδονικός αγώναςμακεδονικός αγώνας
μακεδονικός αγώνας11dimalex
 
Duration of antibiotic treatment after appendicectomy for acute complicated a...
Duration of antibiotic treatment after appendicectomy for acute complicated a...Duration of antibiotic treatment after appendicectomy for acute complicated a...
Duration of antibiotic treatment after appendicectomy for acute complicated a...Arjun Rajagopalan
 
Cary Nadler, Phd.
Cary Nadler, Phd.Cary Nadler, Phd.
Cary Nadler, Phd.rearden638
 
Tα σαΐνια γράφουν Τεύχος 01
Tα σαΐνια γράφουν Τεύχος 01Tα σαΐνια γράφουν Τεύχος 01
Tα σαΐνια γράφουν Τεύχος 01Giannenakis Kostas
 
October, a Good Month
October, a Good MonthOctober, a Good Month
October, a Good MonthBanco Pine
 
Psychology the journal
Psychology the journal Psychology the journal
Psychology the journal kimberleyfay
 
Metshal fe 2albak (2)
Metshal fe 2albak (2)Metshal fe 2albak (2)
Metshal fe 2albak (2)At Minacenter
 
Taller1 catedra ecci
Taller1 catedra ecciTaller1 catedra ecci
Taller1 catedra eccijhonbena
 
Trial spm times_2013_maths_paper1_2_[a]
Trial spm times_2013_maths_paper1_2_[a]Trial spm times_2013_maths_paper1_2_[a]
Trial spm times_2013_maths_paper1_2_[a]Karthik Karunanithy
 
Ac shoulder-dislocation
Ac shoulder-dislocationAc shoulder-dislocation
Ac shoulder-dislocationSoulderPain
 
Onlinejobportalfinal 140315133018-phpapp02
Onlinejobportalfinal 140315133018-phpapp02Onlinejobportalfinal 140315133018-phpapp02
Onlinejobportalfinal 140315133018-phpapp02Hamid Nasir
 
Mba project on performance appraisal
Mba project on performance appraisalMba project on performance appraisal
Mba project on performance appraisalbushmiller440
 

Viewers also liked (18)

μακεδονικός αγώνας
μακεδονικός αγώναςμακεδονικός αγώνας
μακεδονικός αγώνας
 
Perry
PerryPerry
Perry
 
Duration of antibiotic treatment after appendicectomy for acute complicated a...
Duration of antibiotic treatment after appendicectomy for acute complicated a...Duration of antibiotic treatment after appendicectomy for acute complicated a...
Duration of antibiotic treatment after appendicectomy for acute complicated a...
 
Cary Nadler, Phd.
Cary Nadler, Phd.Cary Nadler, Phd.
Cary Nadler, Phd.
 
Tα σαΐνια γράφουν Τεύχος 01
Tα σαΐνια γράφουν Τεύχος 01Tα σαΐνια γράφουν Τεύχος 01
Tα σαΐνια γράφουν Τεύχος 01
 
Gesta
GestaGesta
Gesta
 
October, a Good Month
October, a Good MonthOctober, a Good Month
October, a Good Month
 
Psychology the journal
Psychology the journal Psychology the journal
Psychology the journal
 
Metshal fe 2albak (2)
Metshal fe 2albak (2)Metshal fe 2albak (2)
Metshal fe 2albak (2)
 
δαλάι λάμα
δαλάι λάμαδαλάι λάμα
δαλάι λάμα
 
Taller1 catedra ecci
Taller1 catedra ecciTaller1 catedra ecci
Taller1 catedra ecci
 
Trial spm times_2013_maths_paper1_2_[a]
Trial spm times_2013_maths_paper1_2_[a]Trial spm times_2013_maths_paper1_2_[a]
Trial spm times_2013_maths_paper1_2_[a]
 
Ac shoulder-dislocation
Ac shoulder-dislocationAc shoulder-dislocation
Ac shoulder-dislocation
 
What is technology
What is technologyWhat is technology
What is technology
 
Onlinejobportalfinal 140315133018-phpapp02
Onlinejobportalfinal 140315133018-phpapp02Onlinejobportalfinal 140315133018-phpapp02
Onlinejobportalfinal 140315133018-phpapp02
 
Distribución normal
Distribución normalDistribución normal
Distribución normal
 
Dps mock ups
Dps mock upsDps mock ups
Dps mock ups
 
Mba project on performance appraisal
Mba project on performance appraisalMba project on performance appraisal
Mba project on performance appraisal
 

Similar to Improving Android Performance at Mobiconf 2014

Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Raimon Ràfols
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
The bytecode gobbledygook
The bytecode gobbledygookThe bytecode gobbledygook
The bytecode gobbledygookRaimon Ràfols
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningCarol McDonald
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924yohanbeschi
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Gavin Guo
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Java performance
Java performanceJava performance
Java performanceSergey D
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdfamitbhachne
 
Jvmls 2019 feedback valhalla update
Jvmls 2019 feedback   valhalla updateJvmls 2019 feedback   valhalla update
Jvmls 2019 feedback valhalla updateLogico
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsAzul Systems, Inc.
 
Internet of Things Magic Show
Internet of Things Magic ShowInternet of Things Magic Show
Internet of Things Magic ShowStephen Chin
 
The bytecode hocus pocus - JavaOne 2016
The bytecode hocus pocus - JavaOne 2016The bytecode hocus pocus - JavaOne 2016
The bytecode hocus pocus - JavaOne 2016Raimon Ràfols
 
The bytecode mumbo-jumbo
The bytecode mumbo-jumboThe bytecode mumbo-jumbo
The bytecode mumbo-jumboRaimon Ràfols
 
Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Sylvain Wallez
 

Similar to Improving Android Performance at Mobiconf 2014 (20)

Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014
 
Java Generics
Java GenericsJava Generics
Java Generics
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
The bytecode gobbledygook
The bytecode gobbledygookThe bytecode gobbledygook
The bytecode gobbledygook
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java performance
Java performanceJava performance
Java performance
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Jvmls 2019 feedback valhalla update
Jvmls 2019 feedback   valhalla updateJvmls 2019 feedback   valhalla update
Jvmls 2019 feedback valhalla update
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
Internet of Things Magic Show
Internet of Things Magic ShowInternet of Things Magic Show
Internet of Things Magic Show
 
The bytecode hocus pocus - JavaOne 2016
The bytecode hocus pocus - JavaOne 2016The bytecode hocus pocus - JavaOne 2016
The bytecode hocus pocus - JavaOne 2016
 
The bytecode mumbo-jumbo
The bytecode mumbo-jumboThe bytecode mumbo-jumbo
The bytecode mumbo-jumbo
 
Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!
 

More from Raimon Ràfols

Rendering Art on the Web - A Performance compendium
Rendering Art on the Web - A Performance compendiumRendering Art on the Web - A Performance compendium
Rendering Art on the Web - A Performance compendiumRaimon Ràfols
 
The Digital Evolution of Dinosaurs - MWCS 2017
The Digital Evolution of Dinosaurs - MWCS 2017The Digital Evolution of Dinosaurs - MWCS 2017
The Digital Evolution of Dinosaurs - MWCS 2017Raimon Ràfols
 
Iterate & Learn 2017 (català)
Iterate & Learn 2017 (català)Iterate & Learn 2017 (català)
Iterate & Learn 2017 (català)Raimon Ràfols
 
Iterate + learn - February 2016
Iterate + learn - February 2016Iterate + learn - February 2016
Iterate + learn - February 2016Raimon Ràfols
 
Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Raimon Ràfols
 

More from Raimon Ràfols (7)

Rendering Art on the Web - A Performance compendium
Rendering Art on the Web - A Performance compendiumRendering Art on the Web - A Performance compendium
Rendering Art on the Web - A Performance compendium
 
The Digital Evolution of Dinosaurs - MWCS 2017
The Digital Evolution of Dinosaurs - MWCS 2017The Digital Evolution of Dinosaurs - MWCS 2017
The Digital Evolution of Dinosaurs - MWCS 2017
 
Android Custom Views
Android Custom ViewsAndroid Custom Views
Android Custom Views
 
Iterate & Learn 2017
Iterate & Learn 2017Iterate & Learn 2017
Iterate & Learn 2017
 
Iterate & Learn 2017 (català)
Iterate & Learn 2017 (català)Iterate & Learn 2017 (català)
Iterate & Learn 2017 (català)
 
Iterate + learn - February 2016
Iterate + learn - February 2016Iterate + learn - February 2016
Iterate + learn - February 2016
 
Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10Playing with camera preview buffers on BlackBerry 10
Playing with camera preview buffers on BlackBerry 10
 

Recently uploaded

FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRnishacall1
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfCWS Technology
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Servicenishacall1
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 

Recently uploaded (6)

FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 

Improving Android Performance at Mobiconf 2014

  • 2. Agenda - Who am I? - Java and VMs - Examples - Do & dont's - Tooling
  • 3. Who am I? - Mobile Software Engineering Manager at Imagination Technologies (@imgtec) - Twitter: @rrafols - http://blog.rafols.org
  • 5. Why do we need to talk about java?
  • 6. *.java → [javac] → *.class *.class → [dx] → dex file
  • 9. Javac vs other compilers
  • 10. Compilers Produces optimised code for target platform
  • 12. Javac Doesn't know on which architecture will the code be executed
  • 13. Java bytecode is stack based
  • 15. Not the most optimal solution (regarding performance)
  • 16. Dalvik VM and ART are register based architectures
  • 21. Java VM (JVM) Only the JVM knows on which architecture is running
  • 22. Java VM (JVM) All optimisations are left to be done by the JVM
  • 23. Maybe takes this concept a bit too far...
  • 24. GCC compiler #include <stdio.h> int main() { int a = 10; int b = 1 + 2 + 3 + 4 + 5 + 6 + a; printf("%dn", b); } … movl $31, %esi call _printf … * Using gcc & -O2 compiler option
  • 25. javac public static void main(String args[]) { int a = 10; int b = 1 + 2 + 3 + 4 + 5 + 6 + a; System.out.println(b); } 0: bipush 10 2: istore_1 3: bipush 21 5: iload_1 6: iadd 7: istore_2 ...
  • 26. javac public static void main(String args[]) { int a = 10; int b = 1 + 2 + 3 + 4 + 5 + a + 6; System.out.println(b); } 0: bipush 10 2: istore_1 3: bipush 15 5: iload_1 6: iadd 7: bipush 6 9: iadd 10: istore_2 ...
  • 27. javac public static void main(String args[]) { int a = 10; int b = a + 1 + 2 + 3 + 4 + 5 + 6; System.out.println(b); }
  • 28. javac public static void main(String args[]) { int a = 10; int b = a + 1 + 2 + 3 + 4 + 5 + 6; System.out.println(b); } 0: bipush 10 2: istore_1 3: iload_1 4: iconst_1 5: iadd 6: iconst_2 7: iadd 8: iconst_3 9: iadd 10: iconst_4 11: iadd 12: iconst_5 13: iadd 14: bipush 6 16: iadd 17: istore_2
  • 29. what?
  • 30. Dalvik VM / ART
  • 31.
  • 33. Autoboxing Transparent to the developer but compiler adds some 'extra' code
  • 34. Autoboxing long total = 0; for(int i = 0; i < N; i++) { total += i; } 4: lconst_0 5: lstore_3 6: iconst_0 7: istore 5 9: iload 5 11: ldc #6; 13: if_icmpge 28 16: lload_3 17: iload 5 19: i2l 20: ladd 21: lstore_3 22: iinc 5,1 25: goto 9
  • 35. Autoboxing Long total = 0; for(Integer i = 0; i < N; i++) { total += i; } 9: iconst_0 10: invokestatic #4; //Method java/lang/Integer.valueOf: (I)Ljava/lang/Integer; 13: astore 4 15: aload 4 17: invokevirtual #5; //Method java/lang/Integer.intValue:()I 20: ldc #6; //int 10000000 22: if_icmpge 65 25: aload_3 26: invokevirtual #7; //Method java/lang/Long.longValue:()J 29: aload 4 31: invokevirtual #5; //Method java/lang/Integer.intValue:()I 34: i2l 35: ladd 36: invokestatic #3; //Method java/lang/Long.valueOf: (J)Ljava/lang/Long; 39: astore_3 40: aload 4 42: astore 5 44: aload 4 46: invokevirtual #5; //Method java/lang/Integer.intValue:()I 49: iconst_1 50: iadd 51: invokestatic #4; //Method java/lang/Integer.valueOf: (I)Ljava/lang/Integer; 54: dup 55: astore 4 57: astore 6 59: aload 5 61: pop 62: goto 15
  • 36. Autoboxing ● This is what that code is actually doing: Long total = 0; for(Integer i = Integer.valueOf(0); i.intValue() < N; i = Integer.valueOf(i.intValue() + 1)) { total = Long.valueOf(total.longValue() + (long)i.intValue()) }
  • 38. Sorting Two easy ways to sort: • Arrays.sort(array); • Collections.sort(list); Lets evaluate the performance as both data type & sorting algorithms are different.
  • 39. Sorting • Arrays.sort(arrays) uses a modified version of a 3-way quicksort. • Collections.sort(list) uses a iterative implementation of mergesort (stable)
  • 40.
  • 41. Sorting Arrays.sort(array) uses the same implementation as Collections.sort(list) for non- primitive data types.
  • 42. Loops What's going on under the hood
  • 43. Loops - List ArrayList<Integer> list = new … static long loopStandardList() { long result = 0; for(int i = 0; i < list.size(); i++) { result += list.get(i); } return result; }
  • 44. Loops - List (Java bytecode) 7: lload_0 8: getstatic #26 // Field list:Ljava/util/ArrayList; 11: iload_2 12: invokevirtual #54 // Method java/util/ArrayList.get:(I)Ljava/lang/Object; 15: checkcast #38 // class java/lang/Integer 18: invokevirtual #58 // Method java/lang/Integer.intValue:()I 21: i2l 22: ladd 23: lstore_0 24: iinc 2, 1 27: iload_2 28: getstatic #26 // Field list:Ljava/util/ArrayList; 31: invokevirtual #61 // Method java/util/ArrayList.size:()I 34: if_icmplt 7
  • 45. Loops - List (Dalvik bytecode) 0003: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.list:Ljava/util/ArrayList; 0005: invoke-virtual {v3}, Ljava/util/ArrayList;.size:()I 0008: move-result v3 0009: if-lt v0, v3, 000c // +0003 000b: return-wide v1 000c: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.list:Ljava/util/ArrayList; 000e: invoke-virtual {v3, v0}, Ljava/util/ArrayList;.get:(I)Ljava/lang/Object; 0011: move-result-object v3 0012: check-cast v3, Ljava/lang/Integer; 0014: invoke-virtual {v3}, Ljava/lang/Integer;.intValue:()I 0017: move-result v3 0018: int-to-long v3, v3 0019: add-long/2addr v1, v3 001a: add-int/lit8 v0, v0, #int 1 // #01 001c: goto 0003 // -0019
  • 46. Loops - foreach ArrayList<Integer> list = new … static long loopForeachList() { long result = 0; for(int v : list) { result += v; } return result; }
  • 47. Loops - foreach (Java bytecode) 12: aload_3 13: invokeinterface #70, 1 // InterfaceMethod java/util/Iterator.next:() 18: checkcast #38 // class java/lang/Integer 21: invokevirtual #58 // Method java/lang/Integer.intValue:()I 24: istore_2 25: lload_0 26: iload_2 27: i2l 28: ladd 29: lstore_0 30: aload_3 31: invokeinterface #76, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 36: ifne 12
  • 48. Loops - foreach (Dalvik bytecode) 0008: invoke-interface {v4}, Ljava/util/Iterator;.hasNext:()Z 000b: move-result v3 000c: if-nez v3, 000f // +0003 000e: return-wide v0 000f: invoke-interface {v4}, Ljava/util/Iterator;.next:()Ljava/lang/Object; 0012: move-result-object v3 0013: check-cast v3, Ljava/lang/Integer; 0015: invoke-virtual {v3}, Ljava/lang/Integer;.intValue:()I 0018: move-result v2 0019: int-to-long v5, v2 001a: add-long/2addr v0, v5 001b: goto 0008 // -0013
  • 49. Loops - Array static int[] array = new ... static long loopStandardArray() { long result = 0; for(int i = 0; i < array.length; i++) { result += array[i]; } return result; }
  • 50. Loops - Array (Java bytecode) 7: lload_0 8: getstatic #28 // Field array:[I 11: iload_2 12: iaload 13: i2l 14: ladd 15: lstore_0 16: iinc 2, 1 19: iload_2 20: getstatic #28 // Field array:[I 23: arraylength 24: if_icmplt 7
  • 51. Loops - Array (Dalvik bytecode) 0003: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I 0005: array-length v3, v3 0006: if-lt v0, v3, 0009 // +0003 0008: return-wide v1 0009: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I 000b: aget v3, v3, v0 000d: int-to-long v3, v3 000e: add-long/2addr v1, v3 000f: add-int/lit8 v0, v0, #int 1 // #01 0011: goto 0003 // -000e
  • 52. Loops - size cached static int[] array = new ... static long loopStandardArraySizeStored() { long result = 0; int length = array.length; for(int i = 0; i < length; i++) { result += array[i]; } return result; }
  • 53. Loops - size stored (Java bytecode) 12: lload_0 13: getstatic #28 // Field array:[I 16: iload_3 17: iaload 18: i2l 19: ladd 20: lstore_0 21: iinc 3, 1 24: iload_3 25: iload_2 26: if_icmplt 12
  • 54. Loops - size stored(Dalvik bytecode) 0006: if-lt v0, v1, 0009 // +0003 0008: return-wide v2 0009: sget-object v4, Lcom/rrafols/bcndevcon/Loop;.array:[I 000b: aget v4, v4, v0 000d: int-to-long v4, v4 000e: add-long/2addr v2, v4 000f: add-int/lit8 v0, v0, #int 1 // #01 0011: goto 0006 // -000b
  • 55. Loops - backwards static int[] array = new ... static long loopStandardArrayBackwards() { long result = 0; for(int i = array.length - 1; i >= 0; i--) { result += array[i]; } return result; }
  • 56. Loops - backwards (Java bytecode) 12: lload_0 13: getstatic #28 // Field array:[I 16: iload_2 17: iaload 18: i2l 19: ladd 20: lstore_0 21: iinc 2, -1 24: iload_2 25: ifge 12
  • 57. Loops - backwards(Dalvik bytecode) 0007: if-gez v0, 000a // +0003 0009: return-wide v1 000a: sget-object v3, Lcom/rrafols/bcndevcon/Loop;.array:[I 000c: aget v3, v3, v0 000e: int-to-long v3, v3 000f: add-long/2addr v1, v3 0010: add-int/lit8 v0, v0, #int -1 // #ff 0012: goto 0007 // -000b
  • 58.
  • 59.
  • 60.
  • 61.
  • 63. String concatenation Java allows us to concatenate Strings using the + operator to make our lifes easier. As what happened before with autoboxing the compiler will generate some additional code.
  • 64. String concatenation String str = ""; for(int i = 0; i < ITERATIONS; i++) { str += ANY_OTHER_STRING; }
  • 65. String concatenation 8: new #26 // class java/lang/StringBuilder 11: dup 12: aload_1 13: invokestatic #28 // Method java/lang/String.valueOf: (Ljava/lang/Object;)Ljava/lang/String; 16: invokespecial #34 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 19: ldc #11 // String ANY_OTHER_STRING 21: invokevirtual #37 // Method java/lang/StringBuilder.append:(Ljava/lang/String;) 24: invokevirtual #41 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 27: astore_1 28: iinc 2, 1 31: iload_2 32: bipush ITERATIONS 34: if_icmplt 8
  • 66. String concatenation • It's creating a StringBuilder Object in every iteration of the loop. It's the same as the following code: String str = ""; for(int i = 0; i < ITERATIONS; i++) { StringBuilder sb = new StringBuilder(String.valueOf(str)); sb.append(ANY_OTHER_STRING); str = sb.toString(); }
  • 67. String concatenation - alternatives • Use String.concat() • Concat cost is O(N) + O(M) - (N,M) length of each String • Concat returns a new String Object. String str = ""; for(int i = 0; i < ITERATIONS; i++) { str = str.concat(ANY_OTHER_STRING);
  • 68. String concatenation - alternatives • Use StringBuilder • StringBuffer.append cost is O(M) amortized time (M length of appended String) • Avoids creation of new objects. StringBuilder sb = new StringBuilder() for(int i = 0; i < ITERATIONS; i++) { sb.append(ANY_OTHER_STRING); } str = sb.toString();
  • 69. String concatenation - alternatives • StringBuilder is the not thread safe implementation of StringBuffer. • No need to add synchronization overhead if it's not going to be used by multiple threads.
  • 71. Tooling - Disassembler Java • javap -c <classfile> Android: • Dexdump -d <dexfile> • Smali - https://code.google.com/p/smali/
  • 72. Tooling - Obfuscation • Proguard & Dexguard o Proguard & Dexguard are code obfuscators but they also do a good job on optimizing at bytecode level, reducing overhead, eliminating dead code, inlining methods, … o Proguard is free and comes with the android sdk, needs to be enabled by just uncommenting one line in project.properties
  • 73. Tooling - Performance measurement • Avoid doing manual performance tests with System.currentTimeMillis() (although gives a good estimate) and multiple tests in one run. • JIT might be evil!
  • 74. Performance measurements Avoid doing multiple tests in one run JIT might be evil!
  • 75. Do not trust the compiler! @rrafols http://blog.rafols.org