Analyze 5 features that make the Go language efficient (2/5): function calls are not free

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

Translate the original link reprint/reprint please indicate the source

English original link published in 2014/06/07

function calls are not free

A function call has three steps. Create a new stack frame and record the details of the caller. Save any register content that will be used by the called function to the stack. Computes the address of the called function and executes the jump instruction to that new address.

Because function calls are frequently manipulated, CPU designers spend a lot of effort to optimize the process, but they cannot eliminate all overhead.

Depending on the function of the called function, the call overhead may be negligible or significant. There is an optimization technique called inline (inlining)that lowers the call cost.

The go language compiler implements inline by using the called function code as part of the caller's code. Inline is also a price. It will increase the size of the compiled binary executable file. Inline is meaningful only if the cost of the calling function is a large part of the work that is called by the function itself. So only simple functions are considered to enable inline. The overhead of calling a function often does not account for the large head of complex functions, so they will not be inline.

The above example shows the function double invocation of the Util.max. In order to reduce the call util. Max's cost, the compiler will put util. Max inline into a double function produces the following:

After the inline, util. Max will not be called, but the behavior of double does not change. Inline is not unique to the go language. Almost all compiled or instant-compiled (jited) languages provide this optimization. So how does the inline in the Go language work?

The implementation of the go language is very simple. When a package is compiled, any small function that fits inline is flagged and compiled as expected. It then saves both the source code and the compiled binary.

The picture above shows the contents of the UTIL.A. The source code has been slightly altered to facilitate quick processing by the compiler. When the compiler compiles a double, it will find util. Max is a source code that can be inline and Util.max is also present. Instead of inserting a util, the compiler inserts the source code of the original function. Max's call.

Saving the source code also makes other optimizations possible.

For example above, although the test function always returns false,expensive, it cannot be known until it executes. But when test is inline, we get the following code:

This way the compiler will know that the code is not going to be executed.

This not only saves the cost of calling the test function, it also saves compiling any code that will not be executed. The go compiler can automatically implement function inline between multiple files or packages. If some code calls an inline function from a standard library, the go compiler can also inline these functions.

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.