首頁 > 軟體

Golang的關鍵字defer的使用方法

2022-06-08 18:01:39

核心思想

在defer出現的地方插入了指令CALL runtime.deferproc,在函數返回的地方插入了CALL runtime.deferreturn。goroutine的控制結構中,有一張表記錄defer,呼叫runtime.deferproc時會將需要defer的表示式記錄在表中,而在呼叫runtime.deferreturn的時候,則會依次從defer表中“出棧”並執行

如果有多個defer,呼叫順序類似棧,越後面的defer表示式越先被呼叫

defer鏈

defer資訊會註冊到連結串列,當前執行的 goroutine 持有這個連結串列的頭指標,每個 goroutine 都有一個對應的結構體struct G,其中有一個欄位指向這個defer連結串列頭

type g struct {
	// Stack parameters.
	// stack describes the actual stack memory: [stack.lo, stack.hi).
	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
	// It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
	// stackguard1 is the stack pointer compared in the C stack growth prologue.
	// It is stack.lo+StackGuard on g0 and gsignal stacks.
	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
	stack       stack   // offset known to runtime/cgo
	stackguard0 uintptr // offset known to liblink
	stackguard1 uintptr // offset known to liblink

	_panic       *_panic // innermost panic - offset known to liblink
    // _defer 這個欄位指向defer連結串列頭
	_defer       *_defer // innermost defer
    ...
}

新註冊的defer會新增到連結串列頭,所以感覺像是棧那樣先進後出的呼叫:

原始碼分析

deferproc一共有兩個引數,第一個是引數和返回值的大小,第二個是指向funcval的指標

// Create a new deferred function fn with siz bytes of arguments.
// The compiler turns a defer statement into a call to this.
//go:nosplit
func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
    // 獲取當前goroutine
	gp := getg()
	if gp.m.curg != gp {
		// go code on the system stack can't defer
		throw("defer on system stack")
	}
	// the arguments of fn are in a perilous state. The stack map
	// for deferproc does not describe them. So we can't let garbage
	// collection or stack copying trigger until we've copied them out
	// to somewhere safe. The memmove below does that.
	// Until the copy completes, we can only call nosplit routines.
    // 獲取呼叫者指標
	sp := getcallersp()
    // 通過偏移獲得引數
	argp := uintptr(unsafe.Pointer(&fn)) + unsafe.Sizeof(fn)
	callerpc := getcallerpc()

    // 建立defer結構體
	d := newdefer(siz)
	if d._panic != nil {
		throw("deferproc: d.panic != nil after newdefer")
	}
    // 初始化
	d.link = gp._defer
	gp._defer = d
	d.fn = fn
	d.pc = callerpc
	d.sp = sp
	switch siz {
	case 0:
		// Do nothing.
	case sys.PtrSize:
		*(*uintptr)(deferArgs(d)) = *(*uintptr)(unsafe.Pointer(argp))
	default:
		memmove(deferArgs(d), unsafe.Pointer(argp), uintptr(siz))
	}

	// deferproc returns 0 normally.
	// a deferred func that stops a panic
	// makes the deferproc return 1.
	// the code the compiler generates always
	// checks the return value and jumps to the
	// end of the function if deferproc returns != 0.
	return0()
	// No code can go here - the C return register has
	// been set and must not be clobbered.
}
// 以下是_defer結構體
// A _defer holds an entry on the list of deferred calls.
// If you add a field here, add code to clear it in freedefer and deferProcStack
// This struct must match the code in cmd/compile/internal/gc/reflect.go:deferstruct
// and cmd/compile/internal/gc/ssa.go:(*state).call.
// Some defers will be allocated on the stack and some on the heap.
// All defers are logically part of the stack, so write barriers to
// initialize them are not required. All defers must be manually scanned,
// and for heap defers, marked.
type _defer struct {
    // siz 記錄defer的引數和返回值共佔多少位元組
    // 會直接分配在_defer後面,在註冊時儲存引數,在執行完成時拷貝到呼叫者引數和返回值空間
	siz     int32 // includes both arguments and results
	// started 標記是否已經執行
    started bool
    // heap go1.13優化,標識是否為堆分配
	heap    bool
	// openDefer indicates that this _defer is for a frame with open-coded
	// defers. We have only one defer record for the entire frame (which may
	// currently have 0, 1, or more defers active).
    // openDefer 是否是open defer,通過這些資訊可以找到未註冊到連結串列的defer函數
	openDefer bool
    // sp 記錄呼叫者棧指標,可以通過它判斷自己註冊的defer是否已經執行完了
	sp        uintptr  // sp at time of defer
    // pc deferproc的返回地址
	pc        uintptr  // pc at time of defer
    // fn 要註冊的funcval
	fn        *funcval // can be nil for open-coded defers
    // _panic 指向當前的panic,表示這個defer是由這個panic觸發的
	_panic    *_panic  // panic that is running defer
    // link 鏈到前一個註冊的defer結構體
	link      *_defer

	// If openDefer is true, the fields below record values about the stack
	// frame and associated function that has the open-coded defer(s). sp
	// above will be the sp for the frame, and pc will be address of the
	// deferreturn call in the function.
    // 通過這些資訊可以找到未註冊到連結串列的defer函數
	fd   unsafe.Pointer // funcdata for the function associated with the frame
	varp uintptr        // value of varp for the stack frame
	// framepc is the current pc associated with the stack frame. Together,
	// with sp above (which is the sp associated with the stack frame),
	// framepc/sp can be used as pc/sp pair to continue a stack trace via
	// gentraceback().
	framepc uintptr
}

defer將引數註冊的時候拷貝到堆上,執行時再(將引數和返回值)拷貝回棧上

go會分配不同規格的_defer pool,執行時從空閒_defer中取一個出來用,沒有合適的再進行堆分配。用完以後再放回空閒_defer pool。以避免頻繁的堆分配和回收

優化

go1.12中defer存在的問題:

  • defer資訊主要儲存在堆上,要在堆和棧上來回拷貝返回值和引數很慢
  • defer結構體通過連結串列鏈起來,而連結串列的操作也很慢

go1.13中defer的優化:

  • 減少了defer資訊的堆分配。再通過deferprocStack將整個defer註冊到defer連結串列中
  • 將一般情況的defer資訊儲存在函數棧幀的區域性變數區域
  • 顯示迴圈或者是隱式迴圈的defer還是需要用到go1.12中defer資訊的堆分配
  • 官方給出的效能提升是30%

go1.14中defer的優化:

  • 在編譯階段插入程式碼,把defer函數的執行邏輯展開在所屬函數內,避免建立defer結構體,而且不需要註冊到defer連結串列。稱為 open coded defer
  • 與1.13一樣不適用於迴圈中的defer
  • 效能幾乎提升了一個數量級
  • open coded defer 中發生panic 或 呼叫runtime.Goexit(),後面未註冊到的defer函數無法執行到,需要棧掃描。defer結構體中就多新增了一些欄位,藉助這些欄位可以找到未註冊到連結串列中的defer函數

結果就是defer變快了,但是panic變慢了

defer新增了區域性變數去判斷是否需要執行,需要執行的話就將標識df對應的位上或一下,如果是有條件的defer,需要根據具體條件去或df

deferprocStack

// deferprocStack queues a new deferred function with a defer record on the stack.
// The defer record must have its siz and fn fields initialized.
// All other fields can contain junk.
// The defer record must be immediately followed in memory by
// the arguments of the defer.
// Nosplit because the arguments on the stack won't be scanned
// until the defer record is spliced into the gp._defer list.
//go:nosplit
func deferprocStack(d *_defer) {
    // 獲得當前 goroutine
	gp := getg()
	if gp.m.curg != gp {
		// go code on the system stack can't defer
		throw("defer on system stack")
	}
	// siz and fn are already set.
	// The other fields are junk on entry to deferprocStack and
	// are initialized here.
    // 初始化 _defer 資訊
	d.started = false
	d.heap = false
	d.openDefer = false
	d.sp = getcallersp()
	d.pc = getcallerpc()
	d.framepc = 0
	d.varp = 0
	// The lines below implement:
	//   d.panic = nil
	//   d.fd = nil
	//   d.link = gp._defer
	//   gp._defer = d
	// But without write barriers. The first three are writes to
	// the stack so they don't need a write barrier, and furthermore
	// are to uninitialized memory, so they must not use a write barrier.
	// The fourth write does not require a write barrier because we
	// explicitly mark all the defer structures, so we don't need to
	// keep track of pointers to them with a write barrier.
	*(*uintptr)(unsafe.Pointer(&d._panic)) = 0
	*(*uintptr)(unsafe.Pointer(&d.fd)) = 0
	*(*uintptr)(unsafe.Pointer(&d.link)) = uintptr(unsafe.Pointer(gp._defer))
	*(*uintptr)(unsafe.Pointer(&gp._defer)) = uintptr(unsafe.Pointer(d))

	return0()
	// No code can go here - the C return register has
	// been set and must not be clobbered.
}

到此這篇關於Golang的關鍵字defer的使用的文章就介紹到這了,更多相關 Golang關鍵字defer內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com