当前位置:网站首页>Illustration of Google V8 16: how does V8 improve function execution efficiency through inline caching?
Illustration of Google V8 16: how does V8 improve function execution efficiency through inline caching?
2022-06-21 07:45:00 【Kaixiaomo】
explain
The illustration Google V8 Learning notes
What is inline caching ?
inline caching (Inline caching) It is the optimization technology adopted by the runtime system of some programming languages , The earliest is Smalltalk Development . The goal of inline caching is to speed up runtime method binding by remembering the results of previous method queries directly at the call point . Inline caching is particularly useful for dynamically typed languages , Most of them ( If not all ) Method binding occurs at run time , So virtual method tables are usually not available .
Example :
function loadX(o) {
return o.x
}
var o = {
x: 1,
y: 3
}
var o1 = {
x: 3,
y: 6
}
for (var i = 0; i < 90000; i++) {
loadX(o)
loadX(o1)
}
V8 obtain o.x The process of : Look for objects o The hidden class of , Then find... Through hidden classes x Property offset , Then get the attribute value according to the offset .
In the above code loadX The function will be executed repeatedly , So get o.x The process also needs to be executed repeatedly .V8 The strategy taken to speed up function execution is inline caching (Inline Cache), Referred to as IC.
IC How it works
V8 During the execution of the function , Will observe some call points in the function (CallSite) Key intermediate data on , Then cache the data , The next time the function is executed again ,V8 You can directly use these intermediate data , It saves the process of acquiring these data again .
IC The process
IC One for each function is maintained Feedback vector (FeedBack Vector), The feedback vector records some key intermediate data during the execution of the function .
Relation between function and feedback vector :

What is a feedback vector ?
The feedback vector is actually a Table structure , It consists of many items , Each item is called a slot (Slot),V8 Will execute... In turn loadX The intermediate data of the function is written to the slot of the feedback vector .
The index of the slot is included in each slot (slot index)、 Type of slot (type)、 Status of the slot (state)、 Hidden class (map) The address of 、 And the offset of the attribute .
Example :
function loadX(o) {
o.y = 4
return o.x
}
For example, the code above , When V8 When executing this function , It will judge o.y = 4 and return o.x These two paragraphs are Calling point (CallSite), Because they use objects and properties , that V8 Will be in loadX Each call point is assigned a slot in the feedback vector of the function .

How data is written to the feedback vector ?
Example :
function loadX(o) {
return o.x
}
loadX({
x:1})
My bytecode here is :
[generated bytecode for function: loadX (0x023600253611 <SharedFunctionInfo loadX>)]
Bytecode length: 5
Parameter count 2
Register count 0
Frame size 0
Bytecode age: 0
00000236002537BE @ 0 : 2d 03 00 00 GetNamedProperty a0, [0], [0]
00000236002537C2 @ 4 : a9 Return
Constant pool (size = 1)
0000023600253791: [FixedArray] in OldSpace
- map: 0x023600002239 <Map(FIXED_ARRAY_TYPE)>
- length: 1
0: 0x023600253599 <String[1]: #x>
Handler Table (size = 0)
Source Position Table (size = 0)

First, it should be the same as that of big brother Li Bing :
StackCheck
LdaNamedProperty a0, [0], [0]
Return
LdaNamedProperty: Its function is to take out parameters a0 The first property value of , And put the attribute value into the accumulator .
- a0 Namely loadX The first parameter of ;
- The second parameter
[0]Indicates that the object is taken out a0 The first property value of . - The third parameter is related to the feedback vector , It means
LdaNamedPropertyThe intermediate data of the operation is written into the feedback vector , In the middle of square brackets 0 Indicates that it is written in the first slot of the feedback vector .

In function loadX In the feedback vector of , Data has been cached :
- stay type bar : Cached operation type , Here is LOAD type . In the feedback vector , Pass this through
o.xThe operation to access the value of an object property is called LOAD type . - stay state bar : Cached the status of the slot , there MONO ( Is singlet (monomorphic) Abbreviation );
- stay map bar : Cached o The address of the hidden class ;
- stay offset bar : Cached properties x The offset ;
Storage (STORE) Types and function calls (CALL) type
function foo(){
}
function loadX(o) {
o.y = 4
foo()
return o.x
}
loadX({
x:1,y:4})
Bytecode :
StackCheck
LdaSmi [4]
StaNamedProperty a0, [0], [0]
LdaGlobal [1], [2]
Star r0
CallUndefinedReceiver0 r0, [4]
LdaNamedProperty a0, [2], [6]
Return
Execution flow of bytecode :

LdaSmi [4]: Will constant 4 Load into accumulatorStaNamedProperty: Put... In the accumulator 4 Assign too.yLdaGlobal: load foo The address of the function object into the accumulatorCallUndefinedReceiver0: Realization foo Function call
The resulting feedback vector :
Polymorphism and hypermorphism
One slot of a feedback vector can contain information of multiple hidden classes :
- If a slot contains only 1 A hidden class , Then we call this state Singlet (monomorphic);
- If a slot contains 2~4 A hidden class , Then we call this state polymorphic (polymorphic);
- If there is more than... In a slot 4 A hidden class , Then we call this state Superstate (magamorphic).
Execution efficiency :

If the shape of the object is not fixed , signify V8 The hidden classes created for them are also different .V8 The offset information recorded in the feedback vector cannot be used .
Example :
function loadX(o) {
return o.x
}
var o = {
x: 1,
y: 3
}
var o1 = {
x: 3,
y: 6,
z: 4
}
for (var i = 0; i < 90000; i++) {
loadX(o)
loadX(o1)
}
o The hidden class of is followed by o1 The hidden class of is not a hidden class ,V8 Will choose to record the new hidden class in the feedback vector .

When V8 Re execution loadX Function o.x When the sentence is , Also look up the feedback vector table , Compare .
utilize IC performance optimization
The performance of singlet is better than that of polymorphism and super state , Try to avoid polymorphic and hypermorphic situations .
Example : The following two pieces of code , Which section is efficient , Why? ?
let data = [1, 2, 3, 4]
data.forEach((item) => console.log(item.toString()))
let data = ['1', 2, '3', 4]
data.forEach((item) => console.log(item.toString()))
The first is more efficient .
- In the first way , every last item Same type , Subsequent calls toString It's a direct hit , Is singlet .
- In the second way , because item Different types of errors , Change frequently , You need to cache multiple types at the same time , It's polymorphism .
Expand information
边栏推荐
- Mingming has just changed his profession and won five offers as soon as he graduated
- Interview duck interview brush question website system source code
- QML控件类型:Drawer
- mysql如何关闭事务
- Build a code CR diff platform from 0 to 1
- RDKIT | 基于分子指纹的分子相似性
- The concept of tree
- Research Report on market supply and demand and strategy of oil-free scroll compressor industry in China
- Japanese programming learning website
- 2021-06-16 STM32F103 EXTI 中断识别 使用固件库
猜你喜欢

WordPress website security in 2022

How to view the MySQL installation path

What is a multi domain SSL certificate?

2021-06-16 STM32F103 EXTI 中断识别 使用固件库

In order to thoroughly understand the problem of garbled code, I dug up the history of the character set in a rage

Actual battle of wechat applet project -- music applet developed based on wyy music real interface

Sword finger offer (2nd Edition) brush questions | 04 Find in 2D array

2021-06-17 STM32F103 USART串口代码 使用固件库

Horizontal slot, one line of code can directly convert the web page to PDF and save it (pdfkit)

Is the index of nine interview sites of ten companies invalid?
随机推荐
RDKIT | 基于分子指纹的分子相似性
How to optimize MySQL paging query
Mingming has just changed his profession and won five offers as soon as he graduated
Hisilicon series mass production hardware commissioning record
2021-06-18 STM32F103 DMA and DMA serial port code using firmware library
Rdkit | synthetic feasibility score --sa score
A shell script to realize automatic expiration of Prometheus push gateway indicators
微信公众号对接 : 一键推送文章信息至公众号
Market trend report, technical innovation and market forecast of inorganic water treatment chemicals in China
部署Zabbix企业级分布式监控
Interview duck interview brush question website system source code
Research Report on market supply and demand and strategy of shuttleless loom industry in China
RPA(影刀)无需写代码抓取某东的商品信息
sql与mysql有哪些区别
Market trend report, technical innovation and market forecast of scaffold free 3D cell culture plate in China
Rdkit | compound library based on murcko skeleton clustering
Mathematics is a tool for solving problems
Detailed explanation of deep learning technology for building an image search engine that can find similar images
图解 Google V8 # 16:V8是怎么通过内联缓存来提升函数执行效率的?
AutoCAD - drawing units and drawing boundaries