android performance matters - ran nachmany, google
TRANSCRIPT
Better Accuracy Better Battery
PRIORITY_HIGH_ACCURACY
PRIORITY_BALANCED_POWER_ACCURACY PRIORITY_LOW_POWER
PRIORITY_NO_POWER
What’s the best State for right now?
ZZ
Z
PRIORITY_HIGH_ACCURACY
PRIORITY_BALANCED_POWER_ACCURACY PRIORITY_LOW_POWER
PRIORITY_NO_POWER
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
JobId
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
Job Endpoint
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
Network Type
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
Timing Constraint
JobInfo uploadJob = new JobInfo.Buildr(mSomeInt, mServiceComponent) .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) .setOverrideDeadline(DateUtils.HOUR_IN_MILLIS) .setRequiresCharging(true) .build();
JobScheduler API
Extra Constraint
// 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
// 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
// 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
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
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
{ "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.
{ "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.
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
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
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
Primitive sizes
boolean 8 bits
int
float 32 bits
32 bits
boolean java.lang.Boolean
int
float java.lang.Float
java.lang.Integer
// 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;
// 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
HashMap <obj, obj>
SparseBoolMap <bool,obj>
SparseIntMap <int,obj>
SparseLongMap <long,obj>
LongSparseMap <long,obj>No
Auto
boxi
ng
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
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