您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“go中select編譯期如何優(yōu)化處理邏輯”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“go中select編譯期如何優(yōu)化處理邏輯”這篇文章吧。
select
作為Go
chan
通信的重要監(jiān)聽工具,有著很廣泛的使用場景。select
的使用主要是搭配通信case使用,表面上看,只是簡單的select
及case
搭配,實際上根據(jù)case
的數(shù)量及類型,在編譯時select
會進(jìn)行優(yōu)化處理,根據(jù)不同的情況調(diào)用不同的底層邏輯。
select
編譯時的核心處理邏輯如下:
func walkselectcases(cases *Nodes) []*Node { ncas := cases.Len() sellineno := lineno // optimization: zero-case select // 針對沒有case的select優(yōu)化 if ncas == 0 { return []*Node{mkcall("block", nil, nil)} } // optimization: one-case select: single op. // 針對1個case(單個操作)select的優(yōu)化 if ncas == 1 { cas := cases.First() setlineno(cas) l := cas.Ninit.Slice() if cas.Left != nil { // not default: 非default case n := cas.Left // 獲取case表達(dá)式 l = append(l, n.Ninit.Slice()...) n.Ninit.Set(nil) switch n.Op { default: Fatalf("select %v", n.Op) case OSEND: // Left <- Right // already ok // n中已包含left/right case OSELRECV, OSELRECV2: // OSELRECV(Left = <-Right.Left) OSELRECV2(List = <-Right.Left) if n.Op == OSELRECV || n.List.Len() == 0 { // 左側(cè)有0或1個接收者 if n.Left == nil { // 沒有接收者 n = n.Right // 只需保留右側(cè) } else { // n.Op = OAS // 只有一個接收者,更新Op為OAS } break } if n.Left == nil { // 檢查是否表達(dá)式或賦值 nblank = typecheck(nblank, ctxExpr|ctxAssign) n.Left = nblank } n.Op = OAS2 // OSELRECV2多個接收者 n.List.Prepend(n.Left) // 將left放在前面 n.Rlist.Set1(n.Right) n.Right = nil n.Left = nil n.SetTypecheck(0) n = typecheck(n, ctxStmt) } l = append(l, n) } l = append(l, cas.Nbody.Slice()...) // case內(nèi)的處理 l = append(l, nod(OBREAK, nil, nil)) // 添加break return l } // convert case value arguments to addresses. // this rewrite is used by both the general code and the next optimization. var dflt *Node for _, cas := range cases.Slice() { setlineno(cas) n := cas.Left if n == nil { dflt = cas continue } switch n.Op { case OSEND: n.Right = nod(OADDR, n.Right, nil) n.Right = typecheck(n.Right, ctxExpr) case OSELRECV, OSELRECV2: if n.Op == OSELRECV2 && n.List.Len() == 0 { n.Op = OSELRECV } if n.Left != nil { n.Left = nod(OADDR, n.Left, nil) n.Left = typecheck(n.Left, ctxExpr) } } } // optimization: two-case select but one is default: single non-blocking op. if ncas == 2 && dflt != nil { cas := cases.First() if cas == dflt { cas = cases.Second() } n := cas.Left setlineno(n) r := nod(OIF, nil, nil) r.Ninit.Set(cas.Ninit.Slice()) switch n.Op { default: Fatalf("select %v", n.Op) case OSEND: // if selectnbsend(c, v) { body } else { default body } ch := n.Left r.Left = mkcall1(chanfn("selectnbsend", 2, ch.Type), types.Types[TBOOL], &r.Ninit, ch, n.Right) case OSELRECV: // if selectnbrecv(&v, c) { body } else { default body } ch := n.Right.Left elem := n.Left if elem == nil { elem = nodnil() } r.Left = mkcall1(chanfn("selectnbrecv", 2, ch.Type), types.Types[TBOOL], &r.Ninit, elem, ch) case OSELRECV2: // if selectnbrecv2(&v, &received, c) { body } else { default body } ch := n.Right.Left elem := n.Left if elem == nil { elem = nodnil() } receivedp := nod(OADDR, n.List.First(), nil) receivedp = typecheck(receivedp, ctxExpr) r.Left = mkcall1(chanfn("selectnbrecv2", 2, ch.Type), types.Types[TBOOL], &r.Ninit, elem, receivedp, ch) } r.Left = typecheck(r.Left, ctxExpr) r.Nbody.Set(cas.Nbody.Slice()) r.Rlist.Set(append(dflt.Ninit.Slice(), dflt.Nbody.Slice()...)) return []*Node{r, nod(OBREAK, nil, nil)} } if dflt != nil { ncas-- } casorder := make([]*Node, ncas) nsends, nrecvs := 0, 0 var init []*Node // generate sel-struct lineno = sellineno selv := temp(types.NewArray(scasetype(), int64(ncas))) r := nod(OAS, selv, nil) r = typecheck(r, ctxStmt) init = append(init, r) // No initialization for order; runtime.selectgo is responsible for that. order := temp(types.NewArray(types.Types[TUINT16], 2*int64(ncas))) var pc0, pcs *Node if flag_race { pcs = temp(types.NewArray(types.Types[TUINTPTR], int64(ncas))) pc0 = typecheck(nod(OADDR, nod(OINDEX, pcs, nodintconst(0)), nil), ctxExpr) } else { pc0 = nodnil() } // register cases for _, cas := range cases.Slice() { setlineno(cas) init = append(init, cas.Ninit.Slice()...) cas.Ninit.Set(nil) n := cas.Left if n == nil { // default: continue } var i int var c, elem *Node switch n.Op { default: Fatalf("select %v", n.Op) case OSEND: i = nsends nsends++ c = n.Left elem = n.Right case OSELRECV, OSELRECV2: nrecvs++ i = ncas - nrecvs c = n.Right.Left elem = n.Left } casorder[i] = cas setField := func(f string, val *Node) { r := nod(OAS, nodSym(ODOT, nod(OINDEX, selv, nodintconst(int64(i))), lookup(f)), val) r = typecheck(r, ctxStmt) init = append(init, r) } c = convnop(c, types.Types[TUNSAFEPTR]) setField("c", c) if elem != nil { elem = convnop(elem, types.Types[TUNSAFEPTR]) setField("elem", elem) } // TODO(mdempsky): There should be a cleaner way to // handle this. if flag_race { r = mkcall("selectsetpc", nil, nil, nod(OADDR, nod(OINDEX, pcs, nodintconst(int64(i))), nil)) init = append(init, r) } } if nsends+nrecvs != ncas { Fatalf("walkselectcases: miscount: %v + %v != %v", nsends, nrecvs, ncas) } // run the select lineno = sellineno chosen := temp(types.Types[TINT]) recvOK := temp(types.Types[TBOOL]) r = nod(OAS2, nil, nil) r.List.Set2(chosen, recvOK) fn := syslook("selectgo") r.Rlist.Set1(mkcall1(fn, fn.Type.Results(), nil, bytePtrToIndex(selv, 0), bytePtrToIndex(order, 0), pc0, nodintconst(int64(nsends)), nodintconst(int64(nrecvs)), nodbool(dflt == nil))) r = typecheck(r, ctxStmt) init = append(init, r) // selv and order are no longer alive after selectgo. init = append(init, nod(OVARKILL, selv, nil)) init = append(init, nod(OVARKILL, order, nil)) if flag_race { init = append(init, nod(OVARKILL, pcs, nil)) } // dispatch cases dispatch := func(cond, cas *Node) { cond = typecheck(cond, ctxExpr) cond = defaultlit(cond, nil) r := nod(OIF, cond, nil) if n := cas.Left; n != nil && n.Op == OSELRECV2 { x := nod(OAS, n.List.First(), recvOK) x = typecheck(x, ctxStmt) r.Nbody.Append(x) } r.Nbody.AppendNodes(&cas.Nbody) r.Nbody.Append(nod(OBREAK, nil, nil)) init = append(init, r) } if dflt != nil { setlineno(dflt) dispatch(nod(OLT, chosen, nodintconst(0)), dflt) } for i, cas := range casorder { setlineno(cas) dispatch(nod(OEQ, chosen, nodintconst(int64(i))), cas) } return init }
select
編譯時會根據(jù)case的數(shù)量進(jìn)行優(yōu)化:
1.沒有case
直接調(diào)用block
2.1個case
(1)default
case,直接執(zhí)行body
(2) send
/recv
case (block
為true),按照單獨(dú)執(zhí)行的結(jié)果確認(rèn),可能會發(fā)生block
(3) send
調(diào)用對應(yīng)的chansend1
(4) recv
調(diào)用對應(yīng)的chanrecv1/chanrecv2
3.2個case且包含一個default case
(1) send
/recv
case (block為false),按照單獨(dú)執(zhí)行的結(jié)果確認(rèn)case是否ok,!ok則執(zhí)行default case,不會發(fā)生block
(2) send
調(diào)用對應(yīng)的selectnbsend
(3) recv
調(diào)用對應(yīng)的selectnbrecv
/selectnbrecv2
4.一般的caseselectgo
最后,以一張圖進(jìn)行簡單總結(jié)。
以上是“go中select編譯期如何優(yōu)化處理邏輯”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。