web前端性能优化 2014

82
Web前端性能优化 leeight - 2014

Upload: yubei-li

Post on 22-Nov-2014

828 views

Category:

Documents


13 download

DESCRIPTION

百度的BIT课程

TRANSCRIPT

Page 1: Web前端性能优化 2014

Web前端性能优化leeight - 2014

Page 2: Web前端性能优化 2014

What happened?

http://fex.baidu.com/blog/2014/05/what-happen/

Page 3: Web前端性能优化 2014

What happened?• 从输⼊入 URL 到浏览器接收的过程中发⽣生了什么?

• 浏览器如何向⺴⽹网卡发送数据?

• 数据如何从本机⺴⽹网卡发送到服务器?

• 服务器接收到数据后会进⾏行哪些处理?

• 服务器返回数据后浏览器如何处理?

• 浏览器如何将⻚页⾯面展现出来?

Page 4: Web前端性能优化 2014

What happened?• 从输⼊入 URL 到浏览器接收的过程中发⽣生了什么?

• 浏览器如何向⺴⽹网卡发送数据?

• 数据如何从本机⺴⽹网卡发送到服务器?

• 服务器接收到数据后会进⾏行哪些处理?

• 服务器返回数据后浏览器如何处理?

• 浏览器如何将⻚页⾯面展现出来?

Page 5: Web前端性能优化 2014

⼤大纲• 数据的传输

• 带宽和延迟

• ⺴⽹网络协议

• 缓存

• 数据的渲染

• HTML & CSS

• JavaScript Engine

Page 6: Web前端性能优化 2014

带宽和延迟

2168 * 1000 / 300000 ≈ 7ms

Page 7: Web前端性能优化 2014

带宽和延迟

http://chimera.labs.oreilly.com/books/1230000000545/ch01.html#SPEED_FEATURE

Page 8: Web前端性能优化 2014

带宽和延迟

https://www.igvita.com/slides/2012/html5devconf/#1

Page 9: Web前端性能优化 2014

带宽和延迟

http://chimera.labs.oreilly.com/books/1230000000545/ch10.html#LATENCY_BOTTLENECK

Page 10: Web前端性能优化 2014

带宽和延迟

• 带宽 不是那么重要

• 延迟 才是影响⺴⽹网络性能的关键

Page 11: Web前端性能优化 2014

⺴⽹网络协议

• DNS

• TCP/IP

• HTTP/1.0 & 1.1

• SPDY

Page 12: Web前端性能优化 2014

DNS

• DNS查询是⽐比较耗时的⼀一个操作

• dig +trace www.baidu.com @8.8.8.8

Page 13: Web前端性能优化 2014

DNS!; <<>> DiG 9.8.3-P1 <<>> +trace www.baidu.com @8.8.8.8 ;; global options: +cmd . 18409 IN NS j.root-servers.net. . 18409 IN NS b.root-servers.net. ;; Received 228 bytes from 8.8.8.8#53(8.8.8.8) in 127 ms !com. 172800 IN NS l.gtld-servers.net. com. 172800 IN NS m.gtld-servers.net. ;; Received 491 bytes from 202.12.27.33#53(202.12.27.33) in 318 ms !baidu.com. 172800 IN NS dns.baidu.com. baidu.com. 172800 IN NS ns2.baidu.com. baidu.com. 172800 IN NS ns3.baidu.com. baidu.com. 172800 IN NS ns4.baidu.com. baidu.com. 172800 IN NS ns7.baidu.com. ;; Received 201 bytes from 192.31.80.30#53(192.31.80.30) in 409 ms !www.baidu.com. 1200 IN CNAME www.a.shifen.com. a.shifen.com. 1200 IN NS ns5.a.shifen.com. a.shifen.com. 1200 IN NS ns4.a.shifen.com. a.shifen.com. 1200 IN NS ns3.a.shifen.com. a.shifen.com. 1200 IN NS ns2.a.shifen.com. a.shifen.com. 1200 IN NS ns1.a.shifen.com. ;; Received 228 bytes from 220.181.37.10#53(220.181.37.10) in 30 ms

Page 14: Web前端性能优化 2014

DNS

• dig www.baidu.com @8.8.8.8

• dig www.baidu.com @114.114.114.114

Page 15: Web前端性能优化 2014

DNS Cache

Page 16: Web前端性能优化 2014

DNS Prefetch

http://blog.chromium.org/2008/09/dns-prefetching-or-pre-resolving.html

Page 17: Web前端性能优化 2014

TCP/IP

• 三次握⼿手

• SYN, SYN ACK, ACK

• 慢启动和流量控制

Page 18: Web前端性能优化 2014

三次握⼿手

http://chimera.labs.oreilly.com/books/1230000000545/ch02.html#TCP_HANDSHAKE

Page 19: Web前端性能优化 2014

慢启动

http://chimera.labs.oreilly.com/books/1230000000545/ch02.html#TCP_HANDSHAKE

Page 20: Web前端性能优化 2014

Head-of-Line Blocking

http://chimera.labs.oreilly.com/books/1230000000545/ch02.html#TCP_HOL

Page 21: Web前端性能优化 2014

扩展阅读• TCP Fast Open

• SYN 包传输数据,降低 ~15%

• Building Blocks of TCP

• QUIC(Quick UDP Internet Connections)

• 0 - RTT

• 避免 HOLB

Page 22: Web前端性能优化 2014

HTTP 1.0 & 1.1

• 1996: RFC1945

• 1999: RFC2616

• 2014: RFC7230, RFC7231, RFC7232, RFC7233, RFC7234, RFC7235

Page 23: Web前端性能优化 2014

HTTP 1.1 vs 1.0• 新增的⼀一些特性

• Keep-Alive Connection

• Chunked Encoding Transfer

• Byte Range Requests

• Cache Mechanisms

• Request Pipeline

• ……

Page 24: Web前端性能优化 2014

Keep-Alive Connection$ telnet www.baidu.com 80Trying 61.135.169.125...Connected to www.a.shifen.com.Escape character is ‘^]'.GET / HTTP/1.0!HTTP/1.1 200 OKContent-Type: text/htmlConnection: Close………Connection closed by foreign host.

$ telnet www.baidu.com 80Trying 61.135.169.125...Connected to www.a.shifen.com.Escape character is ‘^]'.GET / HTTP/1.1Host: www.baidu.com!HTTP/1.1 200 OK…Content-Type: text/htmlTransfer-Encoding: chunkedConnection: Keep-Alive…!3dd6………0

Page 25: Web前端性能优化 2014

Keep-Alive Connection

Page 26: Web前端性能优化 2014

Byte Range Requests

wget -c http://www.baidu.com

Page 27: Web前端性能优化 2014

Cache Mechanisms• Expires

• Cache-Control

• max-age, no-cache, public, private

• Last-Modified

• ETag

Page 28: Web前端性能优化 2014

Cache Mechanisms• Expires 和 Cache-Control ⼆二选⼀一

• 建议使⽤用Cache-Control,避免Request Peak

• 主⻚页⾯面不设置,或者设置不缓存 Expires: -1

• Last-Modified 和 ETag ⼆二选⼀一

• 建议使⽤用ETag,更准确⼀一些

Page 29: Web前端性能优化 2014

Cache-Control policy

https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#validating-cached-responses-with-etags

Page 30: Web前端性能优化 2014

http://www.17ce.com/

http://bs.baidu.com/adtest/baidu-00c51dd5.html

http://ecma.bdimg.com/adtest/baidu-00c51dd5.html

CDN

Page 31: Web前端性能优化 2014

SPDY & HTTP 2

• 主要⺫⽬目的是降低传输的延迟

• HTTP 2是基于SPDY进⾏行演化

• https://http2.github.io/http2-spec/

Page 32: Web前端性能优化 2014

SPDY & HTTP 2

http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_STREAMS_MESSAGES_FRAMES

Page 33: Web前端性能优化 2014

SPDY & HTTP 2

http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_STREAMS_MESSAGES_FRAMES

Page 34: Web前端性能优化 2014

SPDY & HTTP 2 vs HTTP 1

http://stackoverflow.com/questions/10480122/difference-between-http-pipeling-and-http-multiplexing-with-spdy

Page 35: Web前端性能优化 2014

SPDY & HTTP 2

chrome://net-internals/#events&q=id:747802

Page 36: Web前端性能优化 2014

SPDY & HTTP 2

Page 37: Web前端性能优化 2014

如何⽣生效的?

• bin/openssl s_client -nextprotoneg 'spdy/3.1, spdy/3, http/1.1, spdy/2' -connect google.com:443

Page 38: Web前端性能优化 2014
Page 39: Web前端性能优化 2014

数据的传输• 延迟很重要

• 减少DNS查询 / DNS预查询 / DNS结果缓存

• 减少HTTP请求的数

• 尽量使⽤用CDN

• 合理的配置服务器缓存策略

• 减少传输的内容⼤大⼩小 / 压缩⽂文本 / 删除⽆无⽤用的⽂文本

• 新的协议 SPDY, QUIC, HTTP 2

Page 40: Web前端性能优化 2014

HTML & CSS• PreloadScanner

• 书写⾼高效的 CSS selectors

• 避免使⽤用 CSS expressions

• 把CSS放到⻚页⾯面顶部

• 明确图⽚片的尺⼨寸

• 明确内容的编码

• ……

Page 41: Web前端性能优化 2014

PreloadScanner

https://plus.google.com/+IlyaGrigorik/posts/8AwRUE7wqAE

Page 42: Web前端性能优化 2014

PreloadScanner

<!—— GOOD ——><script src=“large.js”></script><script src=“ad.js” async></script>!<!—— BAD ——><script src=“large.js”></script><script>var s = document.createElement(‘script’);s.src = “ad.js”;document.head.appendChild(s);</script>

Page 43: Web前端性能优化 2014

书写⾼高效的 CSS selectors• Avoid a universal key selector.

• Make your rules as specific as possible.

• Remove redundant qualifiers.

• Avoid using descendant selectors, especially those that specify redundant ancestors.

• Use class selectors instead of descendant selectors.

https://developers.google.com/speed/docs/best-practices/rendering#UseEfficientCSSSelectors

Page 44: Web前端性能优化 2014

避免使⽤用 CSS expressions

• 如果可能的话,使⽤用标准的 CSS 属性

• 如果⽆无法避免,尽量⽤用 JavaScript 来完成所需的功能

Page 45: Web前端性能优化 2014

JavaScript Engine

• JIT Optimization

• Optimization killers

Page 46: Web前端性能优化 2014

JIT Optimization

• Small Integers

• Hidden Class

• Inline Cache

• Type Interface

• ...

Page 47: Web前端性能优化 2014

WTF?

Page 48: Web前端性能优化 2014

Defining JIT

•Finding  a  way  to  generate  native  code  

•Then  execute  the  native  code

Page 49: Web前端性能优化 2014

Defining JIT

!

•unsigned  char[]  code  =  {        0x48,  0x89,  0xf8        0x48,  0x83,  0xc0,  0x04        0xc3 };

Page 50: Web前端性能优化 2014

Defining JIT

•mov  %rdi,  %rax  

•add  $4,  %rax  

•ret

Page 51: Web前端性能优化 2014

Defining JIT

•mov  %rdi,  %rax  

•add  $4,  %rax  

•ret function add4(num) { return num + 4;}

Page 52: Web前端性能优化 2014

Defining JIT

https://hacks.mozilla.org/2009/07/tracemonkey-overview/

Page 53: Web前端性能优化 2014

Small Integer & Value Representation

Page 54: Web前端性能优化 2014

Value Representation

• Tagged pointer

• NaN Boxing

Avoid Pointer Dereference

Page 55: Web前端性能优化 2014

Tagged pointer

• Pointer vs Integer

Page 56: Web前端性能优化 2014

sizeof( void * )

• 32bit

• 64bit

Page 57: Web前端性能优化 2014

Aligned pointer

• pointer  %  8  ==  0  

• pointer  %  4  ==  0

Page 58: Web前端性能优化 2014

Tagged pointer

• 1010 1111 0101 0011 1100 0000 0000 0000

• 1010 1111 0101 0011 1100 0000 0000 0000 1010 1111 0101 0011 1100 0000 0000 0000

Page 59: Web前端性能优化 2014

Tagged pointer int31

• iiiiiiii|iiiiiiii|iiiiiiii|iiiiiii1  

•Range:  [0,  2^31]

Page 60: Web前端性能优化 2014

Tagged Pointer

• int32 & double will overflow

Page 61: Web前端性能优化 2014

Tagged pointer pointer

• pppppppp|pppppppp|pppppppp|ppppppT0  

•Range:  [0,  2^31]

Page 62: Web前端性能优化 2014

NaN Boxing

Page 63: Web前端性能优化 2014

64 vs 48

Page 64: Web前端性能优化 2014

64 vs 48

http://en.wikipedia.org/wiki/X86_64#Virtual_address_space_details

Page 65: Web前端性能优化 2014

64 vs 48

Page 66: Web前端性能优化 2014

NaN Boxing

Page 67: Web前端性能优化 2014

3.1415926

•S=0  

•E=10000000000  

•M=1001001000011111101101001101000100101101100001001010  

•V=(-­‐1)^S  *  2^(E-­‐0x3ff)  *  1.M

Page 68: Web前端性能优化 2014
Page 69: Web前端性能优化 2014

The end

Page 70: Web前端性能优化 2014

Optimization killers• with

• debugger

• arguments

• for-in

• …

Page 71: Web前端性能优化 2014

with statement

function containsWith() { return 3; with({}) {}}!containsWith();%OptimizeFunctionOnNextCall(containsWith);containsWith();var status = %GetOptimizationStatus(containsWith);console.log(status === 2);

Page 72: Web前端性能优化 2014

debuggervar DEBUG = false;function main() { if (DEBUG) { debugger; } require(“./biz1”); require(“./biz2”); require(“./biz3”); require(“./biz4”);}

var DEBUG = false;function main() { require(“./biz1”); require(“./biz2”); require(“./biz3”); require(“./biz4”);}

Page 73: Web前端性能优化 2014

argumentsfunction fn1(a, b) { b = b || 10; return a + b;}!function fn2() { var args = [].slice.call( arguments);}

function fn3(a, opt_b) { var b = opt_b || 10; return a + b;}!function fn4() { var args = []; for(var i = 0; i < arguments.length; i ++ ){ args[i] = arguments[i]; }}

Page 74: Web前端性能优化 2014

for-infunction nonLocalKey1() { var obj = {} for(var key in obj); return function() { return key; };}!var key;function nonLocalKey2() { var obj = {} for(key in obj);}

function nonLocalKey3() { var obj = {} for(var key in obj);}

Page 75: Web前端性能优化 2014

数据的渲染• HTML & CSS

• 没有什么特殊注意的内容

• 控制代码的体积,选择合理的HTML结构

• JavaScript Engine

• JS引擎的性能越来越好

• Make it happy!

Page 76: Web前端性能优化 2014

公司内的⼀一些平台• http://uaq.baidu.com

• http://webspeed.baidu.com

• http://speedup.baidu.com

• http://yunjiasu.baidu.com

• http://bcs-console.bae.baidu.com

Page 78: Web前端性能优化 2014

Q & A

Page 79: Web前端性能优化 2014

References

Page 80: Web前端性能优化 2014

References

Page 81: Web前端性能优化 2014

References

Page 82: Web前端性能优化 2014

References• https://developers.google.com/speed/docs/best-practices/rendering

• https://developers.google.com/speed/articles/spdy-for-mobile

• https://docs.google.com/spreadsheet/ccc?key=0As3TLupYw2RedG50WW9hNldQaERDTlFHMEc2S2FBTXc#gid=4

• https://www.igvita.com/

• https://www.igvita.com/slides/2012/html5devconf/#52

• http://httparchive.org/trends.php#bytesTotal&reqTotal

• http://http2.github.io/

• https://github.com/h5bp/server-configs

• https://igrigorik.github.io/resource-hints/

• http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html

• ……