
Using inline functions in Golang can significantly improve performance by reducing the overhead of function calls.
In Golang, inline functions are automatically inlined by the compiler when the function body is small enough, typically less than 10-20 lines of code.
The compiler's decision to inline a function depends on various factors, including the size of the function body and the frequency of the function call.
For example, in the article's "Inline Function with Small Body" section, a simple function that calculates the square of a number is shown to be inlined by the compiler, resulting in a performance improvement.
Inlining Techniques
Go's mid-stack inlining allows functions in the middle of a call stack to be inlined without requiring everything below them to be eligible.
This technique was introduced by David Lazar in Go 1.9 and improved in subsequent releases.
Mid-stack inlining can be used to reduce the overhead of a path through the call stack, making it a valuable tool for performance optimization.
A unique perspective: Golang Reflect to Call Function in Package
The primary use case for mid-stack inlining is to inline functions that are part of a larger call stack, but have a low overhead.
Inlining a function like `r.Area()` can be a net win even if its downstream caller to `r.Height()` remains ineligible.
Fast path inlining was used to improve the performance of `sync.Mutex.Lock()` by 14% by splitting the function into an easily inlineable outer function and a complex inner function.
The Go compiler's budget for inlining changes release to release, so it's essential to keep up with the latest compiler versions.
The compiler can inline complex functions like `strconv.ParseInt`, but it's not always supported, so use with caution.
Inlining can be achieved using the `go build` command with the `-gcflags -m` argument.
The `go build` command with `-gcflags -m` can help identify which functions are being inlined.
Here are some key factors to consider when using inlining techniques in Go:
- Mid-stack inlining allows functions in the middle of a call stack to be inlined.
- Go's mid-stack inlining was introduced by David Lazar in Go 1.9.
- The primary use case for mid-stack inlining is to reduce the overhead of a path through the call stack.
- The Go compiler's budget for inlining changes release to release.
- The compiler can inline complex functions like `strconv.ParseInt`.
- Inlining can be achieved using the `go build` command with the `-gcflags -m` argument.
Inlining can have a significant impact on performance, and by understanding the techniques and limitations, you can make informed decisions about when to use inlining in your Go code.
Inlining Limitations and Considerations

Inlining a function into its caller removes the call’s overhead and increases the opportunity for the compiler to apply additional optimisations. This is especially true for small functions that do relatively little work compared to the overhead of calling them.
Inlining trades possibly larger program sizes for potentially faster execution time. As a result, creating many inlined copies of a function can increase compile time and result in larger binaries for marginal gain.
Inlining works best for small functions, and as the size of a function grows, the time saved avoiding the call’s overhead diminishes relative to the work done inside the function. This is why larger functions tend to be more complex, reducing the benefits of optimising their inlined forms versus in situ.
The Go compiler will quietly inline a function from a different package if it is simple enough. This is demonstrated by the inlining of fmt.Printf, which is a common function from the standard packages that is inlined in many people's code.
Inlining can increase the size of the resulting binary, even if it results in faster execution time. This is a trade-off that developers must consider when deciding whether to inline functions in their code.
Related reading: Azure Function C
Debugging and Analysis
Debugging inline functions in Go can be challenging due to their compilation into the surrounding code.
The `go build -gcflags=-m` command can help you understand how the compiler is optimizing your code, including inline functions.
The `go test -c` command can also help you identify issues with inline functions by creating a standalone executable for your test.
The compiler's inlining decisions are based on various factors, including the size of the function and the frequency of its calls.
Inlining can sometimes introduce performance issues if the function is large or complex, but it can also improve performance in many cases.
The `go build -gcflags=-l` command can help you see which functions are being inlined by the compiler.
By understanding how the compiler is treating your inline functions, you can make informed decisions about when to use them and how to optimize your code.
Additional reading: Golang Test Command
Featured Images: pexels.com


