A piece of insight from the inputURL
The process to page rendering
From entering a URL to rendering a page requires multiple processes in Chrome, so let’s talk about the current multi-process architecture of Chrome.
A,Chrome
architecture
Chrome currently adopts a multi-process architecture, which can be divided into five main categories: Browser main process, GPU process, NetWork process, multiple rendering process and multiple plug-in process.
- Browser process. It is mainly responsible for interface display, user interaction, sub-process management, and storage.
- Rendering process. The core task is to make
HTML
,CSS
和JavaScript
Convert to web pages that users can interact with, a typography engineBlink
andJavaScript
engineV8
Are running in this process, and by default,Chrome
For eachTab
Tag creates a rendering process. For security reasons, renderers are run in sandbox mode. GPU
process. In fact,Chrome
Not when we first launchedGPU
The process. whileGPU
The original purpose of using is to achieve3D CSS
The effect is only subsequent pages,Chrome
theUI
The interface is selectedGPU
To draw, which makesGPU
Become a common browser requirement. In the end,Chrome
It is also introduced on its multi-process architectureGPU
Process.- Network process. It is responsible for loading web resources on the page. It used to run as a module in the browser process until recently, when it became a separate process.
- Plug-in process. It is mainly responsible for the running of plug-ins. Plug-ins are prone to crash. Therefore, plug-ins need to be isolated through the plug-in process to ensure that the plug-in process crash does not affect the browser and page
With Chrome’s multi-process architecture, you’ll be able to understand the process from entering urls to rendering, which is divided into navigation and rendering stages.
Second, the navigation stage
ⅰ. Browser main process
1. User inputURL
- **1, ** browser process check
url
, assembly agreement, constitute completeurl
, there are two situations:- Enter search content: The address bar will use the browser’s default search engine to synthesize a new one with search keywords
URL
. - The input is a request
URL
: The address bar will add protocol to this section of content according to the rules and synthesize it into a complete oneURL
;
- Enter search content: The address bar will use the browser’s default search engine to synthesize a new one with search keywords
- ** Browser processes communicate through interprocess communication (
IPC
) theurl
The request is sent to the network process;
ⅱ. Network processes
2.URL
Request process
- **3. ** The network process receives the message
url
After the request, check whether the local cache has cached the requested resource, and return the resource to the browser process if so.
This involves the browser and HTTP protocol cache policy issues, interested can see this article: a detailed explanation of the HTTP protocol
-
**4, ** Prepare IP addresses and ports: Search the cache for DNS resolution before using the DNS server.
- Browser cache;
- Native cache;
hosts
File;- Router cache;
ISP DNS
The cache.DNS
Recursive query (localDNS
Server -> PermissionsDNS
Server -> Top levelDNS
Server – >13
Taiwan rootDNS
The server)
-
**5, ** waiting TCP queue: the browser maintains a maximum of six TCP connections for each domain name. If all six TCP connections are busy when an HTTP request is made, the request will be queued. Solution:
- Adopt domain name sharding technology: put the resources of one site into multiple (
CDN
) domain name. - Upgrade to
HTTP2
There is no6
aTCP
Connection limits;
- Adopt domain name sharding technology: put the resources of one site into multiple (
-
** Establish TCP connection through three-way handshake:
- ** The first time: ** The client sends a synchronization packet to the server
TCP
Header in: flag bit: ** synchronizationSYN
* * for1
, indicating that this is a packet requesting to establish a connection. The serial numberSeq=x
.x
Is the sequence number of the first byte of data to be transmitted, followed by entrySYN-SENT
State;
If the flag bit value is 1, the flag bit is valid.
- ** The second time: ** The server received the packet according to
SYN
The flag bit is judged to be a request to establish a connection, and an acknowledgement packet is returned with the flag bitSYN=1
.ACK=1
And the serial numberseq=y
And confirm,ack=x + 1
Indicates that the client has received the transmissionx
Byte data and hope next time fromx+1
Bytes start to pass and enterSYN-RCVD
State;
Here we need to distinguish the flag bit ACK and the confirmation number ACK;
- ** Third time: ** After the client receives the packet, it sends a confirmation packet to the server, indicating the bit
ACK=1
And the serial numberseq=x+1
And confirm,ack=y+1
And then enterESTABLISHED
State;
After receiving the message, the server enters the ESTABLISHED state. The TCP connection is ESTABLISHED and data can be transmitted.
- Why the third wave? Avoid wasting resources caused by server waiting. Specific reasons are as follows:
If there is no last packet confirmation (third handshake), A sends A request packet to establish A connection first, but is detour due to network reasons. User A does not receive the confirmation packet from user B after the preset timeout period.
A second request packet is sent to establish A connection, this time the network is free, and B’s confirmation packet soon reaches A. So A and B start transmitting data;
After A while, the request packet sent by A for establishing A connection for the first time reaches B, and B thinks it is establishing A connection again, so it sends another confirmation packet. Since A has already received one confirmation packet, it will ignore the second one sent by B. However, after B sends the confirmation packet, it has to wait for A’s reply, and A will never reply.
This causes a waste of server resources, in which case the computer may stop responding.
- ** The first time: ** The client sends a synchronization packet to the server
-
** Build and send HTTP request information;
-
** The server side processes the request;
-
**9, ** Client processing response, first check the server response packet status code:
- If it is
301/302
Indicates that the server has changed its domain name and needs to be redirectedLocation
Field reads the redirect address, and then initiates a new oneHTTP
orHTTPS
Request, jump back to number one4
Step. - If it is
200
, you checkContent-Type
Field, with a value oftext/html
That is theHTML
The document isapplication/octet-stream
File download.
- If it is
- ** the request ends when the header field is generic
Conection
notKeep-Alive
When, that is notTCP
Long connection, by four wave disconnectionTCP
Connection:
- ** First time: ** The client (actively disconnects) sends a packet to the server with flag bits
FIN=1
And the serial numberseq=u
And stop sending data; - ** The second time: ** The server cannot close the connection immediately after receiving the packet because it still needs to transmit the data. It returns a flag bit first
ACK=1
And the serial numberseq=v
And confirm,ack=u+1
Data packets of; - ** Third time: ** When the server is ready to disconnect, it returns a packet with flag bits
FIN=1
, markACK=1
And the serial numberseq=w
And confirm,ack=u+1
; - ** Fourth time: ** The client returns a flag bit after receiving the packet
ACK=1
And the serial numberseq=u+1
And confirm,ack=w+1
Packet of data.
This disconnects the TCP connection with four waves of the hand.
For details, see “Three-way handshake” and “four-way handshake” (part 1).
- Why does ** wave four times? ** The server could not be disconnected immediately
FIN
Release the connection packet andACK
Confirm that the received packet is transmitted in two times, that is, the second and third wave.
3. Prepare the rendering process
- **11, ** Prepare render process: the browser process checks the current
url
Whether it is the same as the root domain of the page that opened the rendering process before, if the same, then reuse the original process, if not, then start a new render process;
4. Submit documents
- **12, ** Submit documents:
- When the renderer process is ready, the browser sends a “submit document” message to the renderer process. After receiving the message, the renderer process establishes a “pipeline” with the network process to transfer data.
- After receiving the data, the renderer sends a “confirm submit” message to the browser
- After receiving the confirmation message, the browser process updates the browser interface status, including the security status, URL of the address bar, historical status of forward and backward operations, and Web page updates
Third, the rendering stage
In the rendering stage, the rendering line is used to complete the rendering of the page with the cooperation of the main thread of the rendering process and the synthetic thread.
ⅲ. Rendering process
The main thread part of the rendering process
5. BuildDOM
The tree
-
13. Decompress the requested data, then the HTML parser splits the HTML byte flow word into tokens, and then generates nodes, which are finally parsed into a browser-recognized DOM tree structure.
You can use the Console option of the Chrome debugger to open the Console and type Document to see the DOM tree.
The rendering engine also has a security check module called XSSAuditor that checks for lexical security. After the toener parses the tokens, it checks whether the modules are secure, such as referencing external scripts, complying with CSP specifications, and making cross-site requests. XSSAuditor intercepts the script or the download task if non-conforming content occurs.
When the HTML is parsed for the first time, the renderer starts a pre-parsed thread. When it encounters JavaScript and CSS references embedded in the HTML document, the renderer synchronously downloads these files in advance, depending on the time of the last file to be downloaded.
6. BuildCSSOM
-
The CSS parser converts CSS to browser-aware styleSheets (CSSOM), which can be viewed by typing document.styleSheets on the console.
Consider blocking, because JavaScript has the ability to modify CSS and HTML, so wait until the CSS file is downloaded and the CSSOM is generated, then execute the JavaScript script, and then continue building the DOM. Because of this blocking, the parse screen is blank;
Optimization scheme:
- remove
js
andcss
Download files from: InlineJavaScript
, inlineCSS
;- Minimize file size: such as by
webpack
Tools such asremoveunnecessaryannotationAnd,The compressionjs
file;- Will not be in
DOM
Operation orCSS
style-modifiedJavaScript
On the tagsync
ordefer
Asynchronous introduction;- Use media to query properties: will be big
CSS
A document divided into several different usesCSS
Files are loaded only in certain scenariosCSS
File.
You can use DOMContentLoaded in the Network panel of the browser debugger to see how long it takes to generate the DOM tree.
7. Style calculation
- ** convert attribute values in the stylesheet to standardize them. Such as the
em
convertpx
.color
convertrgb
; - **16, ** calculation
DOM
The specific style of each node in the tree is followed hereCSS
Inheritance and cascading rules of Can be achieved byChrome
Debug toolElements
Of the optionsComputed
View the final style of a label.
8. Layout
-
** Create a layout tree that traverses all nodes in the DOM tree, removing all hidden nodes (e.g., head, display: None) and leaving only visible nodes in the layout tree.
-
** Calculates the coordinate positions of nodes in the layout tree (complicated, not expanded here);
9. The layered
- **19, ** layer the layout tree and generate a hierarchical tree (
Layer Tree
), can passChrome
Debug toolLayer
Option view. Each node in a hierarchical tree belongs directly or indirectly to a layer (if a node has no corresponding layer, then it belongs to the parent node’s layer).
10. Layer drawing
- ** generates a draw list (i.e., draw instructions) for each layer and submits it to the composition thread. All of the above operations are done in the main thread of the renderer process. After submission to the composite thread, the main thread is not blocked.
The composition thread portion of the renderer process
11. Slice and dice
The composition thread will cut the layer into fixed size blocks (256×256 or 512×512) and then draw the blocks close to the viewport first, which can greatly speed up the page display speed;
Ⅳ.GPU
process
12. Rasterization operation
- 22,inRasterize thread poolsLt.The mapConverted toThe bitmap“Is usually used in this procedure
GPU
To speed up the generation, useGPU
The process of generating a bitmap is calledFast rasterizationOr,GPU
Rasterized, the generated bitmap is saved inGPU
Memory.
ⅴ. Browser main process
13. Synthesis and display
- **23, ** Composition: Once all blocks have been rasterized,Synthesis of the threadIt combines them into a single image and generates a command to draw a block –“
DrawQuad
“And then submits the command to the browser process.
Note: the compositing process is done in the renderer’s compositing thread and does not affect the main thread of the renderer;
- **24, ** display: there is a browser process called
viz
Component to receive messages from the composite threadDrawQuad
Command, and then according toDrawQuad
Command to draw the contents of their page into memory, and finally display the memory on the screen.
At this point, through this series of stages, the HTML, CSS, JavaScript, etc., written by the browser will display a beautiful page.
Resources: How browsers work and practice