go语言sync.Once

发布时间 2023-04-19 23:07:50作者: 每天提醒自己要学习

go语言sync.Once

sync.Once 是 Go 标准库提供的使函数只执行一次的实现,常应用于单例模式,例如初始化配置、保持数据库连接等。作用与 init 函数类似,但有区别。

  • init 函数是当所在的 package 首次被加载时执行,若迟迟未被使用,则既浪费了内存,又延长了程序加载时间。

  • sync.Once 可以在代码的任意位置初始化和调用,因此可以延迟到使用时再执行,并发场景下是线程安全的。
    在多数情况下,sync.Once 被用于控制变量的初始化,这个变量的读写满足如下三个条件:

    • 当且仅当第一次访问某个变量时,进行初始化(写);
    • 变量初始化过程中,所有读都被阻塞,直到初始化完成;
    • 变量仅初始化一次,初始化完成后驻留在内存里。

数据结构

type Once struct {
	// done indicates whether the action has been performed.
	// It is first in the struct because it is used in the hot path.
	// The hot path is inlined at every call site.
	// Placing done first allows more compact instructions on some architectures (amd64/386),
	// and fewer instructions (to calculate offset) on other architectures.
	done uint32
	m    Mutex
}

Once的数据结构很简单,done用来记录是否执行过,0为未执行,1为一致性,m互斥锁来保证原子操作

为什么done放在第一个字段

hot path:程序频繁执行的一些指令。
在源码中 done 字段频繁被访问(后面源码分析会讲到),所以它处在 hot path 上。
那为什么作为第一个字段就能减少 CPU 指令、提高性能呢?
因为结构体第一个字段的地址和结构体的地址是一样的,要访问第一个字段直接对结构体指针进行解引用即可,而访问后面的字段就要计算偏移量(前面字段所占字节空间 + 是否进行了内存对齐),就会增加 CPU 指令。

Do

func (o *Once) Do(f func()) {
	// Note: Here is an incorrect implementation of Do:
	//
	//	if atomic.CompareAndSwapUint32(&o.done, 0, 1) {
	//		f()
	//	}
	//
	// Do guarantees that when it returns, f has finished.
	// This implementation would not implement that guarantee:
	// given two simultaneous calls, the winner of the cas would
	// call f, and the second would return immediately, without
	// waiting for the first's call to f to complete.
	// This is why the slow path falls back to a mutex, and why
	// the atomic.StoreUint32 must be delayed until after f returns.

	if atomic.LoadUint32(&o.done) == 0 {
		// Outlined slow-path to allow inlining of the fast-path.
		o.doSlow(f)
	}
}

源码注释中给出了一种 Do 的错误实现方式:使用 CAS 操作判断 f 是否已经执行,如果没有则执行,否则不执行。
看起来没什么问题,源码给出解释:Do 应该保证当自己返回时,f 已经执行完毕。当同时调用两次 Do 时,竞争成功者将原子地把 done 从 0 改为 1,失败者再进行 CAS 操作时发现不满足条件将直接返回,没有等成功者将 f 执行完。
这也就是为什么源码实现要用到互斥锁 mutex 以及为什么 atomic.StoreUint32 操作要等 f 返回后再执行(见下文 doSlow 分析)。

func (o *Once) doSlow(f func()) {
	o.m.Lock()
	defer o.m.Unlock()
	if o.done == 0 {
		defer atomic.StoreUint32(&o.done, 1)
		f()
	}
}

使用 defer 可以保证 f 先执行完,在 doSlow 返回时才执行 atomic.StoreUint32(&o.done, 1),当然 o.m.Unlock() 也是在 doSlow 返回时执行。
注:defer 的执行顺序是后进先出,也就是最后 defer 的函数,在返回时最先被执行。

看完源码,上来就先原子加载 done,上锁后还访问一次 done,因此说 done 在 hot path 上(填坑)。

总结

  1. Once可以确保方法执行一次,并且只执行一次
  2. 经常访问的字段可以放在结构体的第一位,可以提高访问效率