<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在defer出現的地方插入了指令CALL runtime.deferproc,在函數返回的地方插入了CALL runtime.deferreturn。goroutine的控制結構中,有一張表記錄defer,呼叫runtime.deferproc時會將需要defer的表示式記錄在表中,而在呼叫runtime.deferreturn的時候,則會依次從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存在的問題:
go1.13中defer的優化:
go1.14中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!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45