This is a creation in Article, where the information may have evolved or changed.
This article is part of the translation, collation. The original version of Dave Cheney, one of the developers of go, Five things that made Gofast
Clear Assignment Type
For example, if you have a value that does not exceed UInt32, do not use int var gocon uint32 = 2015
so that the value of Gocon only takes up 4 bytes. Because, for example, the CPU processing speed is far beyond the memory of the bus speed, so the value can be small with small, try to keep the value in Cpucache, rather than slower memory
Function call has overhead, in order to inline, as far as possible to eliminate the compiler can not detect dead code
When a function is called, it is always made by overhead (extra overhead), such as saving the call stack and cutting out the CPU. So the compiler tries to inline, copying and compiling the small functions directly. Give me a chestnut:
func Max(a,b int) int { if a > b { return a } return b}func DoubleMax(a, b) int { return 2 * Max(a,b)}
-M view inline state $go build -gcflags=-m main.go # utils src/utils/max.go:4: caninline Max src/utils/max.go:11: inlining call to Max
The cost of doing this is that the executable binaries are larger, but because inline is not a function call, performance is naturally better. But there are functions that cannot be inline, such as the following
func Test() bool {return False}func Expensive() { if test(){ //接下来的Expensive没办法内联 // Expensive.... }}
Change it to the bottom so it can be inline.
const TEST = Falsefunc Expensive() { if TEST{ // Expensive.... }}
Escape check
First of all, to understand a concept, the stack and heap stack scope is local (locals), after the function is executed, it will be automatically retracted, CPU control, high efficiency and heap needs to be managed by the program, low efficiency specific article about this: Memory stack Vsheap Therefore, even if there is a GC, the parameters that do not need to be transmitted should be controlled as much as possible within the function. For example, because numbers is only in sum, the compiler automatically assigns 100 int spaces in the stack, not in the heap. Because it is in the stack, it does not require GC participation and is automatically retracted. But that doesn't mean you can't reference it with a pointer, see the second chestnut: Although variable c is generated from the new function, c is stored on the stack because there is no reference to C outside the center. Escape Check Example:
Goroutine
Fourth, I think it's a hint: Goroutine is the operation that will be cut out by the Scheduler (scheduler) in the execution unit goroutine, which is smaller than the process and thread:
- Chan Transceiver
- Go statement Call function
- Blocked Syscall
- Gc
A picture wins thousands of words, which indicates how the scheduler switches between goroutine. The fifth is to Go1.3 after the stack allocation mechanism summary, I do not translate, interested students can see the original:)
Summarize
- What I want to add is that the escape is also set up for the channel, so it is better to pass the channel than the object, not the reference. I've planted this problem once, hahaha.
- Before I did not know what the inline is, Golang, pypy such a JIT, or CPython really relieved my mental burden, of course, understand is also very good.
- A clear assignment of personal feeling is not necessary, because go int type is 2**31-1, performance tuning when carefully comb actually also in time