当前位置:网站首页>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 .
边栏推荐
- Esp8266 & sg90 steering gear & Lighting Technology & Arduino
- Atomic alpha development board -- SD card and EMMC burning tool
- Americo technology launches professional desktop video editing solution
- [2022 dark horse programmer] SQL optimization
- We are different
- 100 times larger than the Milky way, Dutch astronomers found mysterious objects in deep space
- Conditional grouping with $exists inside $cond
- Who can teach me how to learn SCM, what to learn first and how to get started?
- Common cluster scripts
- Astronomers may use pulsars to detect merged supermassive black holes
猜你喜欢

Cloning and importing DOM nodes

Ctfhub web - divulgation d'informations - traversée du Répertoire

Why use NoSQL with MySQL?

CTFHub-Web-信息泄露-目錄遍曆

Atomic alpha development board -- SD card and EMMC burning tool

Love Terminator

Practice of hierarchical management based on kubesphere

CTFHub-Web-信息泄露-目录遍历

Event registration Apache pulsar x kubesphere online meetup hot registration

【工具分享】一款颜值与技能并重的软件
随机推荐
Several schemes of traffic exposure in kubernetes cluster
Message queue table structure for storing message data
Wow, it's so rich.
joda. Time get date summary
Derivation of COS (a-b) =cosa*cosb+sina*sinb
集群常用群起脚本
深入解析 Apache BookKeeper 系列:第三篇——读取原理
Make fertilizer Safi from crop residues locally to increase yield by 30% and improve soil
Query JSON data in MySQL table
1W words | 40 pictures | hard core es actual combat
我们不一样
破万,我用了六年!
【他字字不提爱,却句句都是爱】
lotus v1.16.0-rc2 Calibration-net
Qcom--lk phase I2C interface configuration scheme -i2c6
Ctfhub web - divulgation d'informations - traversée du Répertoire
Keil debug view variable prompt not in scope
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
mysql 表查询json数据