No matter how aggressively you’ve optimized your networking code for performance, there’s still a huge problematic performance issue that you haven’t seen: Bloated asset files. If you’re not aggressively attacking the size of the assets you’re transferring to your users, you’re basically stealing time (and money) from them.
Video Link


Smaller Is Better

  1. 速度缓慢并不是所有用户都处于高速的4G/wifi环境下
  2. 大数据量导致的大电量消耗
  3. 流量=金钱

引起数据流量过大的,主要有两个因素:图片、序列化数据。


Images

如果不需要图片透明度设定,那就尽量不要使用PNG格式的图片文件,这会产生难以置信的图片体积。

JPG和WEBP是不错的选择,在使用这两种图片格式时,记住图片质量的极小降低,可以大大缩小图片体积。

图片的分辨率并不是越大越好,根据具体的使用场景,返回不同像素的图片。如果只是需要缩略图,那就不要返回高清的无码大图。


Serialized Data

JSON /dʒeɪ’sʌn/ 是一个horrible的数据格式!!!

看看下面的代码,包含了大量的空格、引号、key值等冗余信息,这些字段存在的意义只是为了方便人工阅读,并非数据传输。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{
"__name": "MOPayOrderDo",
"RefundDetails": ["10.1元 2016-02-12 20:00 退款", "10.02元 2016-02-17 19:00 退款", "10.003元 2016-02-18 17:00 退款", "10.0004元 2016-02-18 18:00 退款"],
"BaseOrderId": null,
"OriAmount#D": 2.123456,
"RemainAmount#D": 100.123456,
"SerializedId": null,
"RepayUrl": "dianping://shopinfo?id=2062015",
"CurrentAmount#D": 0.01,
"SaveAmount#D": 0,
"OrderRemarks": null,
"ErrorMsg": "支付成功|此为付款凭证,请向商户展示",
"SerialNumber": "80033630111",
"RightDos": null,
"MerchantAmountString": "",
"Discounts": null,
"HuiTicketShareDo": {
"__name": "HuiTicketShareDo",
"TicketValidPeriod": "",
"TicketDesc": "",
"TicketValue": "",
"TicketShareStatus#I": 0,
"TicketButtonClickable": false,
"PayTicketDesc": "",
"TicketPicUrl": "",
"TicketTitle": "",
"TicketButtonText": "",
"ShareDo": null,
"ExtraTickets": ""
},
"ShopID#I": 2062015,
"PointMallDo": null,
"RefundAmount#D": 40.1234,
"BuffetDescs": null,
"PayFailDescription": "您的付款将于1-5个工作日原路退还",
"Time#U": 1442469357,
"HuiReviewInfo": null,
"Banner": null,
"OrderID#I": 43956789,
"VoucherSerials": [],
"Status#I": 3,
"OperationBanners": null,
"AlertLoginTips": "绑定手机后订单不会丢哦",
"AlertLoginLink": {
"__name": "Link",
"Name": "现在去绑定",
"Url": ""
},
"MerchantAmount#D": 0.01,
"ServiceUrl": null,
"StatusMsg": "已退款",
"ContactMerchantTip": "",
"OrderDetailTipsDo": null,
"ShopPhones": [
"4008205527"
],
"ShopName": "大众点评网",
"IsHobbit#I": 0,
"NoDiscountAmount#D": 0,
"SuccessMsg": "请向服务员出示验证码",
"HasVoiceReport": false,
"MobileNo": "13774283697",
"VerifyStatus#I": 2,
"MOPayShare": {
"__name": "Share",
"IconUrl": "http://qcloud.dpfile.com/pc/9vKBXqRXPl6vyOVFe4zlXMLys21f38u--LZmWbufGI2pRKzSBzQnLPwzWURFZUAvtOnd3gXQdDYlAqlaVaAFeZ0rYYyiRo_EhzufqWWjTjs.jpg",
"Title": "在【大众点评网】不用现金,手机买单有优惠!",
"Desc": "",
"BtnText": null,
"Url": "http://m.dianping.com/hui/share/weixin?shopId=2062015"
},
"UserAmountString": "{\"userAmount\":\"1.123456元\",\"boughtVoucher\":\"\",\"usedVoucher\":\"\"}",
"BizType#I": 10
}

光头哥在video中给出了几种高效的序列化替代方式,讲解参见Serialization performance (Android Performance Patterns Season 4 ep14)

  • Protocol Buffers
  • Nano-Proto-Buffers
  • FlatBuffers

同样,在http压缩过程里,也有进一步优化的空间,参见Smaller Serialized Data (Android Performance Patterns Season 4 ep15)


===Ending===