Go-five things that make Go fast-learn notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Dave Cheney wrote an article Five things that made Go fast

from values, inline function, Escape analysis,groutine,segment and copyings Stack four ways to learn about go performance in go. first you need to know a couple of concepts inline function inline functions This article will bring you some specific details and please Google variables Go:var gocon int32 = 1024 takes four bytes Python:gocon=2014 getsizeof---> 24 bytes Java : int gocon = 2014 is 4 bytes But if the Integer is 32OS 16byte when using to list<integer> 64OS on 24byte Does it make sense to calculate such overhead? The text points out that it makes sense--specifically, see the link here, just take notes . CPU development is more and more rely on the cache then if the cache of useful data can be more so cached performance will be able to improve the performance of the cache can lead to better program performance typically all types of elements in a dynamic type's language array need to be stored separately in the heap instead of a contiguous storage array This makes the CPU cache useless because the CPU cache reads a contiguous amount of memory space into And this kind of data cache scattered in different memory address is not helpful, the CPU can only read the memory cache to read a data really 3 CPU clock cycles and read one data from memory requires 100CPU clock cycle so program performance is reduced and that's for granted. about the CPU cache article here is a link as a reference
Inline Functions to know what an inline function is, the first thing to know is that a function call has overhead, such as a () call to B () with overhead when invoking the B function-- The overhead can be broadly divided into two parts The cost of passing parameters and the cost of saving the current program context information. For In terms of the cost of passing parameters, the more parameters are passed, the greater the overhead; for the cost of saving the current program context, the more the more expensive the complex is to spend. if the function of a very simple function is even more expensive than the cost of a function call, it is extremely uneconomical. The purpose of the inline function is that the compiler will embed some of the very simple functions called function code into the calling function. as shown in the following code
Func Max (A, b int) int {if a>b {return A}return b}func Double (A, b int) int {return Max (b)} because the Max function is extremely simple its function is much less expensive than a function call Consumption so the Max function is embedded into the double function and the compiler converts the above code to the following Func Double (a, b int) int {temp: = bif a>b {temp = a}return 2 * Temp}
There might be questions about putting all the code in the wrong place--but it's actually problematic. in five things thar make go fast there is a passage that explains the reasons for the original text as follows

The Go compiler inlines a function by treating the body of the function as if it were part of the caller.

Inlining have a cost; It increases binary size.

It only makes sense to inline when the overhead of calling a function was large relative to the work of the function does, so Only simple functions is candidates for inlining.

Complicated functions is usually not dominated by the overhead of calling them and is therefore not inlined.

The general meaning is that the compiler of go is inline with a function through the body of the function as if the function were part of the caller's inline function. He's going to grow the size of the binary code. Calling a function is much more expensive than working with the function, so only those functions that are simple can be used as introverted candidates. The overhead of a complex function function is not dominant so there is no need to inline this responsible function
Summing up the above four paragraphs is that if the complex functions are also inline then the size of the memory used by the binary code is larger and this inline is meaningless because the cost of the function call is negligible relative to the complex function and is introduced in the Go article. The Go compiler can automatically inline functions across files and even across packages. This includes code, calls inlinable functions from the standard library.
The go compiler automatically uses files and packages to make those simple functions include functions in the standard library.

Escape analysis (escape analytics)
for an article on escape analysis please poke this link what is escape analysis (escape analytics)

The optimization principle of escape analysis: The compiler does not throw objects into the heap while the team is not escaping during compilation, and throws them directly into the stack so that when the thread executes the end of the stack the space is reclaimed by the local variables within the Recycle Bin, which is much more efficient than GC to recycle. and the Stack allocation page is much faster than the heap allocation. Program performance optimized by escape analysis is bound to improve a lot

The paragraph in the original text is described below Because Escape analysis was performed at compile time, not run time, stack allocation would always be faster than heap a Llocation, no matter how efficient your garbage collector are.

Goroutine This language support for concurrency is really a delight. This is a higher-end thread pool than the Java thread pool is just a thread-control . and go not only control the thread also specific to the control of the cooperation between the process the points mentioned in this article are recorded in the first

Places where Goroutines yield to others is:

  • Channel send and receive operations, if those operations would block.
  • The Go statement, although there is no guarantee that new Goroutine would be scheduled immediately.
  • Blocking syscalls like file and network operations.
  • After being stopped for a garbage collection cycle.
  • There are several situations where a possible process can occur
  • If the operation is blocked when the pipeline sends the received data, there will be a co-process.
  • Where the Go keyword is used is not guaranteed to execute immediately
  • Blocking calls such as file operations and network operations
  • After the garbage collection has stopped

Segment and Copyings Stack is not finished.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.