GolangNote

Golang笔记

5个GoLang 应用优化措施

Permalink

这是Go 的开发者之一 Dave Cheney 介绍的5个GoLang 优化措施:

清晰赋值类型

比如确认一个数不会超过 uint32 就不要使用int,下表是数值的范围

plaintext: 赋值类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

赋值:

Go: 赋值
1
var gocon uint32 = 2015

数值能用小的用小的,尽量让数值留在CPU cache,而不是速度更慢的内存里。

减少函数调用

函数调用都有 overhead(额外开销),比如保存调用栈,CPU切出。

Go 编译器通常会尝试进行内联,将小函数直接复制并编译。

如下面代码:

Go: 内联编译
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import "fmt"

func main() {
    fmt.Println(DoubleMax(2, 10))
}

func Max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func DoubleMax(a, b int) int {
    return 2 * Max(a, b)
}

加参数 m 查看内联状态:

Bash: 查看内联状态
1
go build -gcflags=-m inline.go

可以看到:

Bash: 查看内联状态
1
2
3
4
5
$ go build -gcflags=-m inline.go 
# command-line-arguments
./inline.go:9: can inline Max
./inline.go:17: inlining call to Max
./inline.go:6: main ... argument does not escape

函数 Max 可以内联。内联使可执行的二进制文件更大了,但性能更好。

内联后上例的 DoubleMax 函数就是这个样子:

Go: 内联函数
1
2
3
4
5
6
7
func DoubleMax(a, b int) int {
    tmp := b
    if a > b {
        tmp = a
    }
    return 2 * tmp
}

尽量使用局部变量

下图是进程地址空间示意:

进程地址空间

stack 作用域是本地的(locals),在函数执行完之后会自动收回,CPU 控制,效率高;而 heap 则需要由程序来管理,效率低。

应该把不需要传出的参数尽量控制在函数内。

局部变量

上图的变量 numbers 只在 Sum 中,编译器也会自动分配100个 int 空间在 stack 中,而不是 heap 中。

局部变量

上图变量c是通过 new 函数生成的,但是因为在 center 外没有 c 的引用,所以 c 也会被存储在 stack 上。

使用 Goroutine 协程

Goroutine 是比进程、线程都小的执行单元,Goroutine 中的会被调度器(scheduler)切出的操作:

  • chan 收发
  • go 语句调用函数
  • 阻塞的syscall
  • gc

第五个太高级了,不常用。

原文 Five things that make Go fast

本文网址: https://golangnote.com/topic/83.html 转摘请注明来源

Related articles

Write a Comment to "5个GoLang 应用优化措施"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms