当前位置:网站首页>Escape analysis of 982 golang
Escape analysis of 982 golang
2022-06-25 07:12:00 【Python's path to becoming a God】
Preface
So called escape analysis (Escape analysis) It refers to the location of memory allocation determined by the compiler , There is no need for programmers to specify . Function to request a new object
If allocated on the stack , After the function is executed, the memory can be recycled automatically ;
If the allocation is in the heap , Then the function can be handed over to GC( Garbage collection ) Handle ;
With escape analysis , Return function local variables will become possible , besides , Escape analysis is also closely related to closures , Know which scenes the object will escape crucial .
Escape strategy
Whenever a new object is requested in the function , The compiler will decide whether to escape according to whether the object is externally referenced by the function :
- If there is no reference outside the function , Is placed on the stack first ;
- If there is a reference outside the function , It must be in the heap ;
Be careful , For objects not referenced outside the function , It's also possible to put it in the heap , For example, the memory is too large and exceeds the storage capacity of the stack .
Escape scene
The pointer escaped
We know Go You can return a pointer to a local variable , This is actually a typical case of variable escape , The sample code is as follows :

function StudentRegister() Inside s Is a local variable , Its value is returned by the return value of the function ,s Itself is a pointer , The memory address it points to will not be Stack but pile , This is a typical escape case .
By compiling parameters -gcflag=-m You can check the escape analysis in the compilation process :
Visible in StudentRegister() Function , I.e. code No 9 Row display ”escapes to heap”, Indicates that the memory allocation of this line has escaped like .
Insufficient stack space to escape
Look at the code below , Whether there will be escape ?
Above code Slice() A... Is assigned in the function 1000 Slices of length , Whether to escape depends on whether the stack space is large enough . Directly view the compilation tips , Such as Next :

We found no escape here . Then expand the length of the slice 10 Double is 10000 How will? ?
We found that when the slice length expanded to 10000 You'll run away .
In fact, when the stack space is insufficient to store the current object or the current slice length cannot be determined, the object will be allocated to the heap .
Dynamic type escape
Many function parameters are interface type , such as fmt.Println(a …interface{}), It is difficult to determine the specific type of its parameters during compilation , People also escape . As shown in the following code :

The above code s Variable is just a string Type variable , call fmt.Println() There will be escape :
Closure reference object escape
A famous open source framework implements a return Fibonacci A function of a sequence :
This function returns a closure , The closure refers to the local variable of the function a and b, Use this function to obtain the closure , Then each time the closure is executed, it will be in turn Output Fibonacci The sequence . The complete sample program is as follows :


The above code is passed Fibonacci() Get a closure , Each time the closure is executed, a Fibonacci The number . The output is as follows :
Fibonacci() Functions that were originally local variables a and b Due to the reference of closure , I have to put them on the pile , So as to escape :
Escape summary
Allocating memory on the stack is more efficient than allocating memory in the heap
Memory allocated on the stack is not required GC Handle
The memory allocated on the heap will be handed over to GC Handle
The purpose of escape analysis is to determine whether the internal allocation address is stack or heap
Escape analysis is completed in the compilation phase
Think about it
Is function transfer pointer really more efficient than value transfer ?
We know that passing pointers can reduce the copy of underlying values , Can improve efficiency , But if the amount of data copied is small , Due to the pointer passing, there will be escape , The heap may be used , It may also increase GC The burden of , So passing a pointer doesn't have to be It's efficient .
边栏推荐
- fastadmin 联级清空数据
- Direct select sort and quick sort
- Who can teach me how to learn SCM, what to learn first and how to get started?
- Are you still doing the dishes yourself? Teach you how to make dishwasher controller with single chip microcomputer
- 【工具分享】一款颜值与技能并重的软件
- Your local changes to the following files would be overwritten by merge: . vs/slnx. sqlite
- Pratique de gestion hiérarchique basée sur kubesphere
- 【C语言】给字符串增加分隔符
- Conditional grouping with $exists inside $cond
- Fastadmin cascade clear data
猜你喜欢

我们不一样

Three laws of go reflection

1W words | 40 pictures | hard core es actual combat

Finally, when you open source the applet ~

弱大数定理的意义与证明

The significance and proof of weak large number theorem
![[轻松学会shell编程]-5、计划任务](/img/6f/8067d4201f0c2e7a692d89885e3ad9.png)
[轻松学会shell编程]-5、计划任务

了解zbrush雕刻软件,以及游戏建模的分析

Several schemes of traffic exposure in kubernetes cluster

The king scheme in distributed locks - redisson
随机推荐
Navicat防止新建查询误删
【ROS2】为什么要使用ROS2?《ROS2系统特性介绍》
单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
聚类和分类的最基本区别。
【一起上水硕系列】Day 4
アルマ / alchemy girl
100 times larger than the Milky way, Dutch astronomers found mysterious objects in deep space
Navicat prevent new query from being deleted by mistake
Who can teach me how to learn SCM, what to learn first and how to get started?
Analysis on the output, market scale and development status of China's children's furniture industry in 2020 and the competition pattern of children's furniture enterprises [figure]
3dmax软件的制作木桶过程:三步流程
Unity获取资源路径
lotus v1.16.0-rc2 Calibration-net
Direct select sort and quick sort
Baidu map - introductory tutorial
Understand ZBrush carving software and game modeling analysis
TorchServe避坑指南
使用OpenGL绘制shp文件
The upper and lower lines of the shell are merged into one line
1W字|40 图|硬核 ES 实战