笔记:Chrome 对浏览器的改进
最近公司架构师推荐看了一篇文件,觉得写得很好,特做笔记:
http://aosabook.org/en/posa/high-performance-networking-in-chrome.html
过去的浏览器只有一个进程,所有打开的 page 都共用同一个内存地址空间,任何地方发生的错,都回影响整个进程。
chrome 是基于多进程模式,每一个tab都是一个单独进程,由此提供隔离的内存和安全sandbox
web 程序的执行包括3个方面
1)fetching resources (using Blink)
2)
page layout and rendering: ( v8 engine)
3)
JavaScript executio:
后两个都是单线程的因为 DOM 的修改不能并发;而JS是单线程语言;但尤其现在cpu的强大,它们都不是瓶颈问题。
性能的瓶颈在第1) fetching resources
一个普通 page 包含下面的东西
- 1280KBinsize
- composed of88resources
- connects to15+ distincthosts
资源请求的过程
给定url, browser 首先判断是否在cache, html header有(Expires
,Cache-Control
,
etc.),browser 就访问cache ,否则花费时间的网络访问开始了。
包括
1) proxy 检查
2) dns
3) tcp ( 三次握手 ), 如果是 ssl 增加2遍三次握手
4) 服务处理和相应
- 50 ms forDNS
- 80 ms forTCPhandshake (oneRTT)
- 160 ms forSSLhandshake (twoRTTs)
- 40 ms for request toserver
- 100 ms for serverprocessing
- 40 ms for response from theserver
再看时间,用户可接受的latency 是1秒,所以必须大量减少,网络访问的开销。
Delay | User Reaction |
---|---|
0 - 100 ms | Instant |
100 - 300 ms | Small perceptible delay |
300 - 1000 ms | Machine is working |
1 s+ | Mental context switch |
10 s+ | I’ll come back later… |
原先以为这个开销是没办法减少的,但 google 的确通过改进 chrome 的 network stack 大大减少了这些开销
通过kernel process 的 network模块给其他模块提供统一的服务
好处是可以做 socket pool, socket resue, 验证, cookie, cache 可以被所有的 render 进程共享,还有利于优化
FYI
对于手机,平板上浏览器,其内存小,用电有限制,但用户开的页面也不多,chrome 根据内存大小来决定 render 进程的数目。如果资源太小,android退化为单线程多进程模式。对于手机需要考略 radio 的耗电, 付费网络的使用等因素
除了构造network stack,chrome 对网络的优化 preXXXX ( 提前做某些事情)
Technique | Description |
---|---|
DNSpre-resolve | Resolve hostnames ahead of time, to avoidDNSlatency |
TCPpre-connect | Connect to destination server ahead of time, to avoidTCPhandshake latency |
Resource prefetching | Fetch critical resources on the page ahead of time, to accelerate rendering of the page |
Page prerendering | Fetch the entire page with all of its resources ahead of time, to enable instant navigation when triggered by the user |
但用户鼠标到某link(还没有点下), 当用户在 url 输入 xxx, 就先去做事情,当然只所以能做这些,是对用户行为进行追踪和统计,防止了无效的 preXXXX.
除了这些 google 还发明一些标签,嵌入在 html 中,明确指示 chrome 做 preXXX 的事情。
看来只要仔细研究,是可以把一些不可能的事情变现实。