Top Banner
Performance Matters +Ran Nachmany @shed2k
100

Android Performance Matters - Ran Nachmany, Google

Aug 08, 2015

Download

Technology

DroidConTLV
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Android Performance Matters - Ran Nachmany, Google

Performance Matters

+Ran Nachmany@shed2k

Page 2: Android Performance Matters - Ran Nachmany, Google

Battery PerformanceHey, do you have a power outlet I can use?

Page 3: Android Performance Matters - Ran Nachmany, Google

I’m doing stuff!

LOL!NOPE.

Page 4: Android Performance Matters - Ran Nachmany, Google

What’s eating your mobile battery?goo.gl/nMkSE

25-30%Actual work.

70%Analytics

GPSAds

Page 5: Android Performance Matters - Ran Nachmany, Google

QUIT BEING STUPID.

Better Battery Golden Rule

Page 6: Android Performance Matters - Ran Nachmany, Google

DEFER WORK UNTIL THE BEST TIME FOR BATTERY

Better Battery Golden Rule

Page 7: Android Performance Matters - Ran Nachmany, Google

Battery Problem:

WakeLocks

Page 8: Android Performance Matters - Ran Nachmany, Google

Active Screen Off

ZZ

Z

Asleep

Page 9: Android Performance Matters - Ran Nachmany, Google

Keeping the device awakegoo.gl/uEfy56

ZZ

Z

Wakelock

Page 10: Android Performance Matters - Ran Nachmany, Google
Page 11: Android Performance Matters - Ran Nachmany, Google

Wakelock.aquire(..)goo.gl/ZNCx6n

Page 12: Android Performance Matters - Ran Nachmany, Google

AlarmManager.setInexact*(..)goo.gl/l7m7fb20 minutes

15 minutes

Page 13: Android Performance Matters - Ran Nachmany, Google

Release Wakelocks.Batch Wakeups.

#PERFMATTERS TIP

Page 14: Android Performance Matters - Ran Nachmany, Google

Battery Problem:

Location

Page 15: Android Performance Matters - Ran Nachmany, Google

EXPENSIVE

Page 16: Android Performance Matters - Ran Nachmany, Google

GPS Provider Cell Network Provider

More AccurateMore Power

Less AccurateLess Power

Page 19: Android Performance Matters - Ran Nachmany, Google

GPS Fetch Totally

steal

Passive Provider

Page 20: Android Performance Matters - Ran Nachmany, Google

Only use the resolutionyou need.

#PERFMATTERS TIP #43

Page 21: Android Performance Matters - Ran Nachmany, Google

Networking

Page 22: Android Performance Matters - Ran Nachmany, Google

Battery Drain

Page 23: Android Performance Matters - Ran Nachmany, Google

Bandwidth

Power

Idle

DCH

FACH

Page 24: Android Performance Matters - Ran Nachmany, Google

Bandwidth

Power

Idle

DCH

FACH

Tail Time~10 sec

Tail Time~60 sec

Wake Up Time~2 Sec

Page 25: Android Performance Matters - Ran Nachmany, Google

$$$$$$$$$

User Pays for your requests

Page 26: Android Performance Matters - Ran Nachmany, Google

LESS RADIO TIMELESS DATA

NETWORKING Golden Rule

Page 27: Android Performance Matters - Ran Nachmany, Google

Network Technique:

Batching

Page 28: Android Performance Matters - Ran Nachmany, Google

XHR

wai

t

XHR

wai

tXH

Rw

ait

XHR

wai

t

XHR

wai

t

XHR

wai

t

XHR

XHR

XHR

XHR

XHR

XHR

wai

t

BATCHING!

Page 29: Android Performance Matters - Ran Nachmany, Google

Do Now Server Response Data Push

Types of networking requests

CAN OPTIMIZE

Page 30: Android Performance Matters - Ran Nachmany, Google

requ

ests

pending queue

Page 31: Android Performance Matters - Ran Nachmany, Google

Length == 10

pending queue

requ

ests

Page 32: Android Performance Matters - Ran Nachmany, Google

Get Notified

Page 33: Android Performance Matters - Ran Nachmany, Google

Get Notified

Page 34: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

Page 35: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

JobId

Page 36: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

Job Endpoint

Page 37: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

Network Type

Page 38: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

Timing Constraint

Page 39: Android Performance Matters - Ran Nachmany, Google

JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();

JobScheduler API

Extra Constraint

Page 40: Android Performance Matters - Ran Nachmany, Google

SyncAdapters

Pre L Batching

Page 41: Android Performance Matters - Ran Nachmany, Google

SyncAdapters

Pre L Batching

GcmNetworkManager

Page 42: Android Performance Matters - Ran Nachmany, Google

// Schedule a task to occur between five and fifteen minutes from now: OneoffTask myTask = new OneoffTask.Builder() .setService(MyGcmTaskService.class) .setExecutionWindow( 5 * DateUtil.MINUTE_IN_SECONDS, 15 * DateUtil.MINUTE_IN_SECONDS) .setTag("test-upload") .build(); GcmNetworkManager.get(this).schedule(myTask);

GcmNetworkManager

GcmNetworkManagerhttps://goo.gl/hTwIEt

Page 43: Android Performance Matters - Ran Nachmany, Google

// Implement service logic to be notified when the task elapses: MyUploadService extends GcmTaskService { @Override public int onRunTask(TaskParams params) { // Do some upload work. return GcmNetworkManager.RESULT_SUCCESS; } }

GcmNetworkManager

GcmNetworkManagerhttps://goo.gl/hTwIEt

Page 44: Android Performance Matters - Ran Nachmany, Google

// Implement service logic to be notified when the task elapses: MyUploadService extends GcmTaskService { @Override public int onRunTask(TaskParams params) { // Do some upload work. return GcmNetworkManager.RESULT_SUCCESS; } }

GcmNetworkManager

adb shell am broadcast -a "com.google.android.gms.gcm.ACTION_TRIGGER_TASK" \

-e component -e tag

GcmNetworkManagerhttps://goo.gl/hTwIEt

Page 45: Android Performance Matters - Ran Nachmany, Google

Network Technique:

Prefetching

Page 46: Android Performance Matters - Ran Nachmany, Google

10 seconds

6 seconds

Page 47: Android Performance Matters - Ran Nachmany, Google

Fetc

h 1M

B

Fetc

h 1M

B

Fetc

h 1M

B

Fetc

h 1M

B

Fetc

h 4M

B

PRE FETCHING!

wai

t

wai

t

wai

t

wai

t

wai

t

Page 48: Android Performance Matters - Ran Nachmany, Google

@ Startup

Page 49: Android Performance Matters - Ran Nachmany, Google

How much do we prefetch?Quality pre-fetch is :

about 1-5mb of data.about 1-2 minutes of data need

Page 50: Android Performance Matters - Ran Nachmany, Google

2G = fetch 3 new photos

Page 51: Android Performance Matters - Ran Nachmany, Google

4G = fetch 12 new photos

Page 52: Android Performance Matters - Ran Nachmany, Google

Network Problem:

Payload Size

Page 53: Android Performance Matters - Ran Nachmany, Google
Page 54: Android Performance Matters - Ran Nachmany, Google

45kquality : 70

164kquality : 100

Page 55: Android Performance Matters - Ran Nachmany, Google

Site JPG QualityGoogle Images Thumbnails 74-76

Facebook full-size images 85

Yahoo frontpage JPEGs 69-91

Youtube frontpage JPEGs 70-82

Wikipedia images 80

Windows live background 82

Twitter user JPEG images 30-100

Page 56: Android Performance Matters - Ran Nachmany, Google

READ

goo.gl/skf1gp

Page 57: Android Performance Matters - Ran Nachmany, Google

JSONIS

HORRIBLE

Page 58: Android Performance Matters - Ran Nachmany, Google

{ "id": 1, "jsonrpc": "2.0", "total": 1, "max_id":276372795494133760, "result": [ { "id": 0, "guid": "2084d344-81fe-4714-9e2a-42c83b46083b", "isActive": true, "balance": "$1,507.00", "picture": "http://placehold.it/32x32", "age": 24, "latitude": -85.791587, "longitude": -64.615557, "tags": [ "quis", "est", "ea", "velit", "exercitation", "quis", "veniam" ], "friends": [ { "id": 0, "name": "Sanford Patton" }, { "id": 1, "name": "Tameka Frazier" }, { "id": 2, "name": "Gilliam Leach" } ], "randomArrayItem": "apple" } ]}

Spaces, quotes, returns, names, are all

verbose data.

Page 59: Android Performance Matters - Ran Nachmany, Google

{ "id": 1, "jsonrpc": "2.0", "total": 1, "max_id":276372795494133760, "result": [ { "id": 0, "guid": "2084d344-81fe-4714-9e2a-42c83b46083b", "isActive": true, "balance": "$1,507.00", "picture": "http://placehold.it/32x32", "age": 24, "latitude": -85.791587, "longitude": -64.615557, "tags": [ "quis", "est", "ea", "velit", "exercitation", "quis", "veniam" ], "friends": [ { "id": 0, "name": "Sanford Patton" }, { "id": 1, "name": "Tameka Frazier" }, { "id": 2, "name": "Gilliam Leach" } ], "randomArrayItem": "apple" } ]}

Copied into memory, must determine if

string, int, date, etc.

Page 60: Android Performance Matters - Ran Nachmany, Google

FlatBuffers

Page 61: Android Performance Matters - Ran Nachmany, Google

FlatBuffers

Scheme Compiler

.java

.cpp

.goFlatBuffershttp://goo.gl/r9xTXK

Page 62: Android Performance Matters - Ran Nachmany, Google
Page 63: Android Performance Matters - Ran Nachmany, Google
Page 64: Android Performance Matters - Ran Nachmany, Google

Smaller datawins the Internet.

#PERFMATTERS TIP #9

Page 65: Android Performance Matters - Ran Nachmany, Google

Memory PerformanceWHY IS EVERYTHING SLOW????? I FORGET……...

Page 66: Android Performance Matters - Ran Nachmany, Google

GC Event!

Allo

cate

d m

emor

y

Memory Threshold

Page 67: Android Performance Matters - Ran Nachmany, Google

Fib3(..) GC Fib3(..)Mainfunction

FIB3 fcn haulted

FIB3 fcn Resume

Page 68: Android Performance Matters - Ran Nachmany, Google

16ms

DRAW!DRAW! DRAW!

16ms 16ms

UPDATE GC

Page 69: Android Performance Matters - Ran Nachmany, Google

GC events eat kittens

framerate

MEMORY Golden Rule

Page 70: Android Performance Matters - Ran Nachmany, Google

Memory Problem:

Bitmaps

Page 71: Android Performance Matters - Ran Nachmany, Google

HEA

P

IMGIMG

GC

IMGIMG

HEA

P

Page 72: Android Performance Matters - Ran Nachmany, Google

Smaller Pixel Formats

Down-ScaleImagesBitmap Re-use

Page 73: Android Performance Matters - Ran Nachmany, Google

Glidegoo.gl/flNbyf

Picassogoo.gl/tpTc1

Page 74: Android Performance Matters - Ran Nachmany, Google

Bitmaps are bigMake them smaller

#PERFMATTERS TIP #44

Page 75: Android Performance Matters - Ran Nachmany, Google

Memory Problem:

HashMap

Page 76: Android Performance Matters - Ran Nachmany, Google

KEY

Hash-to-index

Allocated, but unused

Allocated, but unused

Stored Value

Page 77: Android Performance Matters - Ran Nachmany, Google

ArrayMap.javagoo.gl/SCeQEL

ArrayMap

HASH #1

HASH #2

HASH #3

Hashes of keyssorted

VALUE 2

KEY 3

VALUE 3

KEY 1

VALUE 1

KEY 2

Interwovenkey / value

Page 78: Android Performance Matters - Ran Nachmany, Google

ArrayMap.javagoo.gl/SCeQEL

HASH #8

HASH #9HASH #10

KEY HASH #5

HASH #6

HASH #7

HASH #1

VALUE 7

KEY 8

VALUE 8

KEY 1

VALUE 1

KEY 7

Key Index = (hashIndex *2) + 1

binary search

HASH #7

Page 79: Android Performance Matters - Ran Nachmany, Google

collision search

VALUE 7

KEY 8

VALUE 8

KEY 1

VALUE 1

KEY 7

Key Index = (hashIndex *2) + 1

HASH #8

HASH #9HASH #10

KEY HASH #5

HASH #6

HASH #7

HASH #1binary search

Page 80: Android Performance Matters - Ran Nachmany, Google

EmptyBut array still allocated

Emptynothing allocated

HashMap ArrayMap

Page 81: Android Performance Matters - Ran Nachmany, Google

<1000objects

Mapscontaining maps

Page 82: Android Performance Matters - Ran Nachmany, Google

Use ArrayMapin the right places.

#PERFMATTERS TIP #76

Page 83: Android Performance Matters - Ran Nachmany, Google

Memory Problem:

Auto Boxing

Page 84: Android Performance Matters - Ran Nachmany, Google

Primitive sizes

boolean 8 bits

int

float 32 bits

32 bits

boolean java.lang.Boolean

int

float java.lang.Float

java.lang.Integer

Page 85: Android Performance Matters - Ran Nachmany, Google

Integer value = 0

Primitive int

Integer Object

Autoboxing

Page 86: Android Performance Matters - Ran Nachmany, Google

// Primitive versionint total = 0;for (int i = 0; i < 100; i++) total += i;

// Generic versionInteger total = 0;for (int i = 0; i < 100; i++) total += i;

Page 87: Android Performance Matters - Ran Nachmany, Google

// Primitive versionint total = 0;for (int i = 0; i < 100; i++) total += i;

// Generic versionInteger total = 0;for (int i = 0; i < 100; i++) //total += i;

Allocates 83 new objects!(yea, i know, it’s confusing..)

Allocates 0 new objects!

// create new Integer(), // push in new value // add to total

Page 88: Android Performance Matters - Ran Nachmany, Google

HashMap <Primitive,Object>

put update get

autoboxing autoboxingautoboxing

Page 89: Android Performance Matters - Ran Nachmany, Google

HashMap <obj, obj>

SparseBoolMap <bool,obj>

SparseIntMap <int,obj>

SparseLongMap <long,obj>

LongSparseMap <long,obj>No

Auto

boxi

ng

Page 90: Android Performance Matters - Ran Nachmany, Google

Lots

of G

ener

ics

Single Call Site

AllocationTracker

Memory Profiling 101goo.gl/h1Cl9k

Page 91: Android Performance Matters - Ran Nachmany, Google

TraceView

Page 92: Android Performance Matters - Ran Nachmany, Google

Avoid Autoboxing

#PERFMATTERS TIP #21

Page 93: Android Performance Matters - Ran Nachmany, Google

Memory Problem:

ENUMs

Page 94: Android Performance Matters - Ran Nachmany, Google
Page 95: Android Performance Matters - Ran Nachmany, Google

2556Bytes

Page 96: Android Performance Matters - Ran Nachmany, Google

public static final int VALUE1 = 1;public static final int VALUE2 = 2;public static final int VALUE3 = 3;

int func(int value) {switch (value) {

case VALUE1:return -1;

case VALUE2:return -2;

case VALUE3:return -3;

}return 0;

}Bytes

2680

+124 bytes

Page 97: Android Performance Matters - Ran Nachmany, Google

public static enum Value {VALUE1,VALUE2,VALUE3

}int func(Value value) {

switch (value) {case VALUE1:

return -1;case VALUE2:

return -2;case VALUE3:

return -3;}return 0;

} Bytes4188

13x more than int version

Page 98: Android Performance Matters - Ran Nachmany, Google

public static enum Value { VALUE1, VALUE2, VALUE3 }

12-16 bytesfor the array

20+ bytes (each)

Page 99: Android Performance Matters - Ran Nachmany, Google

If you’re using ENUMSSTOP.

#PERFMATTERS TIP #73

Page 100: Android Performance Matters - Ran Nachmany, Google

QUIT BEING STUPID.

#PerfMatters Golden Rule