当前位置:网站首页>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 :

  1. If there is no reference outside the function , Is placed on the stack first ;
  2. 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 :

 Insert picture description here
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 :
 Insert picture description here
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 ?
 Insert picture description here
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 :

 Insert picture description here
We found no escape here . Then expand the length of the slice 10 Double is 10000 How will? ?
 Insert picture description here
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 :

 Insert picture description here
The above code s Variable is just a string Type variable , call fmt.Println() There will be escape :
 Insert picture description here

Closure reference object escape

A famous open source framework implements a return Fibonacci A function of a sequence :
 Insert picture description here
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 :

 Insert picture description here
 Insert picture description here
The above code is passed Fibonacci() Get a closure , Each time the closure is executed, a Fibonacci The number . The output is as follows :
 Insert picture description here
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 :
 Insert picture description here

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 .

原网站

版权声明
本文为[Python's path to becoming a God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201234083025.html