{"id":10006974,"date":"2022-12-15T09:53:17","date_gmt":"2022-12-15T01:53:17","guid":{"rendered":"https:\/\/ityet.com\/?p=10006974"},"modified":"2022-12-15T09:53:17","modified_gmt":"2022-12-15T01:53:17","slug":"go%e8%af%ad%e8%a8%80%e6%80%a7%e8%83%bd%e5%89%96%e6%9e%90%e5%88%a9%e5%99%a8-pprof%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006974","title":{"rendered":"Go\u8bed\u8a00\u6027\u80fd\u5256\u6790\u5229\u5668&#8211;pprof\u5b9e\u6218"},"content":{"rendered":"<p><strong>\u4f5c\u8005\uff1a\u803f\u5b97\u6770<\/strong><\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5173\u4e8epprof\u7684\u6587\u7ae0\u5728\u7f51\u4e0a\u5df2\u662f\u6c57\u725b\u5145\u680b\uff0c\u5374\u662f\u5343\u7bc7\u4e00\u5f8b\u7684\u547d\u4ee4\u4ecb\u7ecd\uff0c\u9c9c\u6709\u771f\u6b63\u5b9e\u64cd\u7684\uff0c\u672c\u6587\u5c06\u53c2\u8003Go\u793e\u533a\u8d44\u6599\uff0c\u7ed3\u5408\u81ea\u5df1\u7684\u7ecf\u9a8c\uff0c\u5b9e\u6218Go\u7a0b\u5e8f\u7684\u6027\u80fd\u5206\u6790\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002<\/p>\n<h2>\u4f18\u5316\u601d\u8def<\/h2>\n<p>\u9996\u5148\u8bf4\u4e00\u4e0b\u6027\u80fd\u4f18\u5316\u7684\u4e00\u822c\u601d\u8def\u3002\u7cfb\u7edf\u6027\u80fd\u7684\u5206\u6790\u4f18\u5316\uff0c\u4e00\u5b9a\u662f\u4ece\u5927\u5230\u5c0f\u7684\u6b65\u9aa4\u6765\u8fdb\u884c\u7684\uff0c\u5373\u4ece\u4e1a\u52a1\u67b6\u6784\u7684\u4f18\u5316\uff0c\u5230\u7cfb\u7edf\u67b6\u6784\u7684\u4f18\u5316\uff0c\u518d\u5230\u7cfb\u7edf\u6a21\u5757\u95f4\u7684\u4f18\u5316\uff0c\u6700\u540e\u5230\u4ee3\u7801\u7f16\u5199\u5c42\u9762\u7684\u4f18\u5316\u3002\u4e1a\u52a1\u67b6\u6784\u7684\u4f18\u5316\u662f\u6700\u5177\u6027\u4ef7\u6bd4\u7684\uff0c\u6280\u672f\u96be\u5ea6\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5374\u53ef\u4ee5\u5e26\u6765\u5927\u5e45\u7684\u6027\u80fd\u63d0\u5347\u3002\u6bd4\u5982\u901a\u8fc7\u548c\u540c\u4e8b\u6216\u5916\u90e8\u95e8\u6c9f\u901a\uff0c\u51cf\u5c11\u4e86\u4e00\u4e9b\u63a5\u53e3\u8c03\u7528\u6216\u8005\u53bb\u6389\u4e86\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u53ef\u4ee5\u8f7b\u677e\u63d0\u5347\u6574\u4e2a\u7cfb\u7edf\u7684\u6027\u80fd\u3002<\/p>\n<p>\u7cfb\u7edf\u67b6\u6784\u7684\u4f18\u5316\uff0c\u6bd4\u5982\u52a0\u5165\u7f13\u5b58\uff0c\u7531http\u6539\u8fdb\u4e3arpc\u7b49\uff0c\u4e5f\u53ef\u4ee5\u5728\u5c11\u91cf\u6295\u5165\u4e0b\u5e26\u6765\u8f83\u5927\u7684\u6027\u80fd\u63d0\u5347\u3002\u6700\u540e\u662f\u7a0b\u5e8f\u4ee3\u7801\u7ea7\u522b\u7684\u6027\u80fd\u4f18\u5316\uff0c\u8fd9\u53c8\u5206\u4e3a\u4e24\u65b9\u9762\uff0c\u4e00\u662f\u5408\u683c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u4f7f\u7528\uff0c\u4e8c\u624d\u662f\u5728\u6b64\u57fa\u7840\u4e0a\u7684\u6027\u80fd\u5256\u6790\u3002\u6bd4\u5982\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528slice\u8fd9\u79cd\u65b9\u4fbf\u7684\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5c3d\u53ef\u80fd\u63d0\u524d\u7533\u8bf7\u8db3\u591f\u7684\u5185\u5b58\u9632\u6b62append\u8d85\u8fc7\u5bb9\u91cf\u65f6\u7684\u5185\u5b58\u7533\u8bf7\u548c\u6570\u636e\u62f7\u8d1d\uff1b\u4f7f\u7528\u5e76\u53d1\u4fdd\u62a4\u65f6\u5c3d\u91cf\u7531RWMutex \u4ee3\u66ffmutex\uff0c\u751a\u81f3\u5728\u6781\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4f7f\u7528\u66f4\u7ec6\u7c92\u5ea6\u7684\u539f\u5b50\u64cd\u4f5c\u4ee3\u66ff\u9501\u7b49\u7b49\u3002<\/p>\n<h2>\u4f18\u5316\u5b9e\u8df5<\/h2>\n<p>\u4e0b\u9762\u8fdb\u5165\u6b63\u6587\uff0c\u5f85\u4f18\u5316\u7a0b\u5e8f\u662f\u793e\u533a\u4e2d\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4ee3\u7801\u6709\u70b9\u957f\uff0c\u5b9e\u73b0\u7684\u7b97\u6cd5\u662f\u8457\u540d\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6Tarjan\u7684\u6c42\u56fe\u7684\u5f3a\u8fde\u901a\u5206\u91cf\u7b97\u6cd5\uff0c\u5173\u4e8e\u8fd9\u4e2a\u7b97\u6cd5\u7684\u601d\u60f3\u8bf7\u81ea\u884cgoogle\uff08\u5c31\u522b\u81ea\u884c\u767e\u5ea6\u4e86~\uff09\u3002\u4ee5\u4e0b\u4e3a\u5b9e\u64cd\u8fc7\u7a0b\uff08\u4f1a\u6709\u90a3\u4e48\u4e00\u4e22\u4e22\u957f\u3002\u3002\u3002\uff09\uff1a<\/p>\n<p>\u521d\u59cb\u7248\u672c\u4ee3\u7801 havlak1.go:<\/p>\n<pre><code>\/\/ Go from multi-language-benchmark\/src\/havlak\/go_pro\/\/ Copyright 2011 Google Inc.\/\/\/\/ Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);\/\/ you may not use this file except in compliance with the License.\/\/ You may obtain a copy of the License at\/\/\/\/     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\/\/\/\/ Unless required by applicable law or agreed to in writing, software\/\/ distributed under the License is distributed on an &quot;AS IS&quot; BASIS,\/\/ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\/\/ See the License for the specific language governing permissions and\/\/ limitations under the License.\/\/ Test Program for the Havlak loop finder.\/\/\/\/ This program constructs a fairly large control flow\/\/ graph and performs loop recognition. This is the Go\/\/ version.\/\/package mainimport (   &quot;flag&quot;   &quot;fmt&quot;   &quot;log&quot;   &quot;os&quot;   &quot;runtime\/pprof&quot;)type BasicBlock struct {   Name     int   InEdges  []*BasicBlock   OutEdges []*BasicBlock}func NewBasicBlock(name int) *BasicBlock {   return &amp;BasicBlock{Name: name}}func (bb *BasicBlock) Dump() {   fmt.Printf(&quot;BB#%06d:&quot;, bb.Name)   if len(bb.InEdges) &gt; 0 {      fmt.Printf(&quot; in :&quot;)      for _, iter := range bb.InEdges {         fmt.Printf(&quot; BB#%06d&quot;, iter.Name)      }   }   if len(bb.OutEdges) &gt; 0 {      fmt.Print(&quot; out:&quot;)      for _, iter := range bb.OutEdges {         fmt.Printf(&quot; BB#%06d&quot;, iter.Name)      }   }   fmt.Printf(&quot;\/n&quot;)}func (bb *BasicBlock) NumPred() int {   return len(bb.InEdges)}func (bb *BasicBlock) NumSucc() int {   return len(bb.OutEdges)}func (bb *BasicBlock) AddInEdge(from *BasicBlock) {   bb.InEdges = append(bb.InEdges, from)}func (bb *BasicBlock) AddOutEdge(to *BasicBlock) {   bb.OutEdges = append(bb.OutEdges, to)}\/\/-----------------------------------------------------------type CFG struct {   Blocks []*BasicBlock   Start  *BasicBlock}func NewCFG() *CFG {   return &amp;CFG{}}func (cfg *CFG) NumNodes() int {   return len(cfg.Blocks)}func (cfg *CFG) CreateNode(node int) *BasicBlock {   if node &lt; len(cfg.Blocks) {      return cfg.Blocks[node]   }   if node != len(cfg.Blocks) {      println(&quot;oops&quot;, node, len(cfg.Blocks))      panic(&quot;wtf&quot;)   }   bblock := NewBasicBlock(node)   cfg.Blocks = append(cfg.Blocks, bblock)   if len(cfg.Blocks) == 1 {      cfg.Start = bblock   }   return bblock}func (cfg *CFG) Dump() {   for _, n := range cfg.Blocks {      n.Dump()   }}\/\/-----------------------------------------------------------type BasicBlockEdge struct {   Dst *BasicBlock   Src *BasicBlock}func NewBasicBlockEdge(cfg *CFG, from int, to int) *BasicBlockEdge {   self := new(BasicBlockEdge)   self.Src = cfg.CreateNode(from)   self.Dst = cfg.CreateNode(to)   self.Src.AddOutEdge(self.Dst)   self.Dst.AddInEdge(self.Src)   return self}\/\/-----------------------------------------------------------\/\/ Basic Blocks and Loops are being classified as regular, irreducible,\/\/ and so on. This enum contains a symbolic name for all these classifications\/\/const (   _             = iota \/\/ Go has an interesting iota concept   bbTop                \/\/ uninitialized   bbNonHeader          \/\/ a regular BB   bbReducible          \/\/ reducible loop   bbSelf               \/\/ single BB loop   bbIrreducible        \/\/ irreducible loop   bbDead               \/\/ a dead BB   bbLast               \/\/ sentinel)\/\/ UnionFindNode is used in the Union\/Find algorithm to collapse\/\/ complete loops into a single node. These nodes and the\/\/ corresponding functionality are implemented with this class\/\/type UnionFindNode struct {   parent    *UnionFindNode   bb        *BasicBlock   loop      *SimpleLoop   dfsNumber int}\/\/ Init explicitly initializes UnionFind nodes.\/\/func (u *UnionFindNode) Init(bb *BasicBlock, dfsNumber int) {   u.parent = u   u.bb = bb   u.dfsNumber = dfsNumber   u.loop = nil}\/\/ FindSet implements the Find part of the Union\/Find Algorithm\/\/\/\/ Implemented with Path Compression (inner loops are only\/\/ visited and collapsed once, however, deep nests would still\/\/ result in significant traversals).\/\/func (u *UnionFindNode) FindSet() *UnionFindNode {   var nodeList []*UnionFindNode   node := u   for ; node != node.parent; node = node.parent {      if node.parent != node.parent.parent {         nodeList = append(nodeList, node)      }   }   \/\/ Path Compression, all nodes' parents point to the 1st level parent.   for _, ll := range nodeList {      ll.parent = node.parent   }   return node}\/\/ Union relies on path compression.\/\/func (u *UnionFindNode) Union(B *UnionFindNode) {   u.parent = B}\/\/ Constants\/\/\/\/ Marker for uninitialized nodes.const unvisited = -1\/\/ Safeguard against pathological algorithm behavior.const maxNonBackPreds = 32 * 1024\/\/ IsAncestor\/\/\/\/ As described in the paper, determine whether a node 'w' is a\/\/ &quot;true&quot; ancestor for node 'v'.\/\/\/\/ Dominance can be tested quickly using a pre-order trick\/\/ for depth-first spanning trees. This is why DFS is the first\/\/ thing we run below.\/\/\/\/ Go comment: Parameters can be written as w,v int, inlike in C, where\/\/   each parameter needs its own type.\/\/func isAncestor(w, v int, last []int) bool {   return ((w &lt;= v) &amp;&amp; (v &lt;= last[w]))}\/\/ listContainsNode\/\/\/\/ Check whether a list contains a specific element. \/\/func listContainsNode(l []*UnionFindNode, u *UnionFindNode) bool {   for _, ll := range l {      if ll == u {         return true      }   }   return false}\/\/ DFS - Depth-First-Search and node numbering.\/\/func DFS(currentNode *BasicBlock, nodes []*UnionFindNode, number map[*BasicBlock]int, last []int, current int) int {   nodes[current].Init(currentNode, current)   number[currentNode] = current   lastid := current   for _, target := range currentNode.OutEdges {      if number[target] == unvisited {         lastid = DFS(target, nodes, number, last, lastid+1)      }   }   last[number[currentNode]] = lastid   return lastid}\/\/ FindLoops\/\/\/\/ Find loops and build loop forest using Havlak's algorithm, which\/\/ is derived from Tarjan. Variable names and step numbering has\/\/ been chosen to be identical to the nomenclature in Havlak's\/\/ paper (which, in turn, is similar to the one used by Tarjan).\/\/func FindLoops(cfgraph *CFG, lsgraph *LSG) {   if cfgraph.Start == nil {      return   }   size := cfgraph.NumNodes()   nonBackPreds := make([]map[int]bool, size)   backPreds := make([][]int, size)   number := make(map[*BasicBlock]int)   header := make([]int, size, size)   types := make([]int, size, size)   last := make([]int, size, size)   nodes := make([]*UnionFindNode, size, size)   for i := 0; i &lt; size; i++ {      nodes[i] = new(UnionFindNode)   }   \/\/ Step a:   \/\/   - initialize all nodes as unvisited.   \/\/   - depth-first traversal and numbering.   \/\/   - unreached BB's are marked as dead.   \/\/   for i, bb := range cfgraph.Blocks {      number[bb] = unvisited      nonBackPreds[i] = make(map[int]bool)   }   DFS(cfgraph.Start, nodes, number, last, 0)   \/\/ Step b:   \/\/   - iterate over all nodes.   \/\/   \/\/   A backedge comes from a descendant in the DFS tree, and non-backedges   \/\/   from non-descendants (following Tarjan).   \/\/   \/\/   - check incoming edges 'v' and add them to either   \/\/     - the list of backedges (backPreds) or   \/\/     - the list of non-backedges (nonBackPreds)   \/\/   for w := 0; w &lt; size; w++ {      header[w] = 0      types[w] = bbNonHeader      nodeW := nodes[w].bb      if nodeW == nil {         types[w] = bbDead         continue \/\/ dead BB      }      if nodeW.NumPred() &gt; 0 {         for _, nodeV := range nodeW.InEdges {            v := number[nodeV]            if v == unvisited {               continue \/\/ dead node            }            if isAncestor(w, v, last) {               backPreds[w] = append(backPreds[w], v)            } else {               nonBackPreds[w][v] = true            }         }      }   }   \/\/ Start node is root of all other loops.   header[0] = 0   \/\/ Step c:   \/\/   \/\/ The outer loop, unchanged from Tarjan. It does nothing except   \/\/ for those nodes which are the destinations of backedges.   \/\/ For a header node w, we chase backward from the sources of the   \/\/ backedges adding nodes to the set P, representing the body of   \/\/ the loop headed by w.   \/\/   \/\/ By running through the nodes in reverse of the DFST preorder,   \/\/ we ensure that inner loop headers will be processed before the   \/\/ headers for surrounding loops.   \/\/   for w := size - 1; w &gt;= 0; w-- {      \/\/ this is 'P' in Havlak's paper      var nodePool []*UnionFindNode      nodeW := nodes[w].bb      if nodeW == nil {         continue \/\/ dead BB      }      \/\/ Step d:      for _, v := range backPreds[w] {         if v != w {            nodePool = append(nodePool, nodes[v].FindSet())         } else {            types[w] = bbSelf         }      }      \/\/ Copy nodePool to workList.      \/\/      workList := append([]*UnionFindNode(nil), nodePool...)      if len(nodePool) != 0 {         types[w] = bbReducible      }      \/\/ work the list...      \/\/      for len(workList) &gt; 0 {         x := workList[0]         workList = workList[1:]         \/\/ Step e:         \/\/         \/\/ Step e represents the main difference from Tarjan's method.         \/\/ Chasing upwards from the sources of a node w's backedges. If         \/\/ there is a node y' that is not a descendant of w, w is marked         \/\/ the header of an irreducible loop, there is another entry         \/\/ into this loop that avoids w.         \/\/         \/\/ The algorithm has degenerated. Break and         \/\/ return in this case.         \/\/         nonBackSize := len(nonBackPreds[x.dfsNumber])         if nonBackSize &gt; maxNonBackPreds {            return         }         for iter := range nonBackPreds[x.dfsNumber] {            y := nodes[iter]            ydash := y.FindSet()            if !isAncestor(w, ydash.dfsNumber, last) {               types[w] = bbIrreducible               nonBackPreds[w][ydash.dfsNumber] = true            } else {               if ydash.dfsNumber != w {                  if !listContainsNode(nodePool, ydash) {                     workList = append(workList, ydash)                     nodePool = append(nodePool, ydash)                  }               }            }         }      }      \/\/ Collapse\/Unionize nodes in a SCC to a single node      \/\/ For every SCC found, create a loop descriptor and link it in.      \/\/      if (len(nodePool) &gt; 0) || (types[w] == bbSelf) {         loop := lsgraph.NewLoop()         loop.SetHeader(nodeW)         if types[w] != bbIrreducible {            loop.IsReducible = true         }         \/\/ At this point, one can set attributes to the loop, such as:         \/\/         \/\/ the bottom node:         \/\/    iter  = backPreds[w].begin();         \/\/    loop bottom is: nodes[iter].node);         \/\/         \/\/ the number of backedges:         \/\/    backPreds[w].size()         \/\/         \/\/ whether this loop is reducible:         \/\/    type[w] != BasicBlockClass.bbIrreducible         \/\/         nodes[w].loop = loop         for _, node := range nodePool {            \/\/ Add nodes to loop descriptor.            header[node.dfsNumber] = w            node.Union(nodes[w])            \/\/ Nested loops are not added, but linked together.            if node.loop != nil {               node.loop.Parent = loop            } else {               loop.AddNode(node.bb)            }         }         lsgraph.AddLoop(loop)      } \/\/ nodePool.size   } \/\/ Step c}\/\/ External entry point.func FindHavlakLoops(cfgraph *CFG, lsgraph *LSG) int {   FindLoops(cfgraph, lsgraph)   return lsgraph.NumLoops()}\/\/======================================================\/\/ Scaffold Code\/\/======================================================\/\/ Basic representation of loops, a loop has an entry point,\/\/ one or more exit edges, a set of basic blocks, and potentially\/\/ an outer loop - a &quot;parent&quot; loop.\/\/\/\/ Furthermore, it can have any set of properties, e.g.,\/\/ it can be an irreducible loop, have control flow, be\/\/ a candidate for transformations, and what not.\/\/type SimpleLoop struct {   \/\/ No set, use map to bool   basicBlocks map[*BasicBlock]bool   Children    map[*SimpleLoop]bool   Parent      *SimpleLoop   header      *BasicBlock   IsRoot       bool   IsReducible  bool   Counter      int   NestingLevel int   DepthLevel   int}func (loop *SimpleLoop) AddNode(bb *BasicBlock) {   loop.basicBlocks[bb] = true}func (loop *SimpleLoop) AddChildLoop(child *SimpleLoop) {   loop.Children[child] = true}func (loop *SimpleLoop) Dump(indent int) {   for i := 0; i &lt; indent; i++ {      fmt.Printf(&quot;  &quot;)   }   \/\/ No ? operator ?   fmt.Printf(&quot;loop-%d nest: %d depth %d &quot;,      loop.Counter, loop.NestingLevel, loop.DepthLevel)   if !loop.IsReducible {      fmt.Printf(&quot;(Irreducible) &quot;)   }   \/\/ must have &gt; 0   if len(loop.Children) &gt; 0 {      fmt.Printf(&quot;Children: &quot;)      for ll := range loop.Children {         fmt.Printf(&quot;loop-%d&quot;, ll.Counter)      }   }   if len(loop.basicBlocks) &gt; 0 {      fmt.Printf(&quot;(&quot;)      for bb := range loop.basicBlocks {         fmt.Printf(&quot;BB#%06d &quot;, bb.Name)         if loop.header == bb {            fmt.Printf(&quot;*&quot;)         }      }      fmt.Printf(&quot;\/b)&quot;)   }   fmt.Printf(&quot;\/n&quot;)}func (loop *SimpleLoop) SetParent(parent *SimpleLoop) {   loop.Parent = parent   loop.Parent.AddChildLoop(loop)}func (loop *SimpleLoop) SetHeader(bb *BasicBlock) {   loop.AddNode(bb)   loop.header = bb}\/\/------------------------------------\/\/ Helper (No templates or such)\/\/func max(x, y int) int {   if x &gt; y {      return x   }   return y}\/\/ LoopStructureGraph\/\/\/\/ Maintain loop structure for a given CFG.\/\/\/\/ Two values are maintained for this loop graph, depth, and nesting level.\/\/ For example:\/\/\/\/ loop        nesting level    depth\/\/----------------------------------------\/\/ loop-0      2                0\/\/   loop-1    1                1\/\/   loop-3    1                1\/\/     loop-2  0                2\/\/var loopCounter = 0type LSG struct {   root  *SimpleLoop   loops []*SimpleLoop}func NewLSG() *LSG {   lsg := new(LSG)   lsg.root = lsg.NewLoop()   lsg.root.NestingLevel = 0   return lsg}func (lsg *LSG) NewLoop() *SimpleLoop {   loop := new(SimpleLoop)   loop.basicBlocks = make(map[*BasicBlock]bool)   loop.Children = make(map[*SimpleLoop]bool)   loop.Parent = nil   loop.header = nil   loop.Counter = loopCounter   loopCounter++   return loop}func (lsg *LSG) AddLoop(loop *SimpleLoop) {   lsg.loops = append(lsg.loops, loop)}func (lsg *LSG) Dump() {   lsg.dump(lsg.root, 0)}func (lsg *LSG) dump(loop *SimpleLoop, indent int) {   loop.Dump(indent)   for ll := range loop.Children {      lsg.dump(ll, indent+1)   }}func (lsg *LSG) CalculateNestingLevel() {   for _, sl := range lsg.loops {      if sl.IsRoot {         continue      }      if sl.Parent == nil {         sl.SetParent(lsg.root)      }   }   lsg.calculateNestingLevel(lsg.root, 0)}func (lsg *LSG) calculateNestingLevel(loop *SimpleLoop, depth int) {   loop.DepthLevel = depth   for ll := range loop.Children {      lsg.calculateNestingLevel(ll, depth+1)      ll.NestingLevel = max(loop.NestingLevel, ll.NestingLevel+1)   }}func (lsg *LSG) NumLoops() int {   return len(lsg.loops)}func (lsg *LSG) Root() *SimpleLoop {   return lsg.root}\/\/======================================================\/\/ Testing Code\/\/======================================================func buildDiamond(cfgraph *CFG, start int) int {   bb0 := start   NewBasicBlockEdge(cfgraph, bb0, bb0+1)   NewBasicBlockEdge(cfgraph, bb0, bb0+2)   NewBasicBlockEdge(cfgraph, bb0+1, bb0+3)   NewBasicBlockEdge(cfgraph, bb0+2, bb0+3)   return bb0 + 3}func buildConnect(cfgraph *CFG, start int, end int) {   NewBasicBlockEdge(cfgraph, start, end)}func buildStraight(cfgraph *CFG, start int, n int) int {   for i := 0; i &lt; n; i++ {      buildConnect(cfgraph, start+i, start+i+1)   }   return start + n}func buildBaseLoop(cfgraph *CFG, from int) int {   header := buildStraight(cfgraph, from, 1)   diamond1 := buildDiamond(cfgraph, header)   d11 := buildStraight(cfgraph, diamond1, 1)   diamond2 := buildDiamond(cfgraph, d11)   footer := buildStraight(cfgraph, diamond2, 1)   buildConnect(cfgraph, diamond2, d11)   buildConnect(cfgraph, diamond1, header)   buildConnect(cfgraph, footer, from)   footer = buildStraight(cfgraph, footer, 1)   return footer}var cpuprofile = flag.String(&quot;cpuprofile&quot;, &quot;&quot;, &quot;write cpu profile to this file&quot;)func main() {   flag.Parse()   if *cpuprofile != &quot;&quot; {      f, err := os.Create(*cpuprofile)      if err != nil {         log.Fatal(err)      }      pprof.StartCPUProfile(f)      defer pprof.StopCPUProfile()   }   lsgraph := NewLSG()   cfgraph := NewCFG()   cfgraph.CreateNode(0) \/\/ top   cfgraph.CreateNode(1) \/\/ bottom   NewBasicBlockEdge(cfgraph, 0, 2)   for dummyloop := 0; dummyloop &lt; 15000; dummyloop++ {      FindHavlakLoops(cfgraph, NewLSG())   }   n := 2   for parlooptrees := 0; parlooptrees &lt; 10; parlooptrees++ {      cfgraph.CreateNode(n + 1)      buildConnect(cfgraph, 2, n+1)      n = n + 1      for i := 0; i &lt; 100; i++ {         top := n         n = buildStraight(cfgraph, n, 1)         for j := 0; j &lt; 25; j++ {            n = buildBaseLoop(cfgraph, n)         }         bottom := buildStraight(cfgraph, n, 1)         buildConnect(cfgraph, n, top)         n = bottom      }      buildConnect(cfgraph, n, 1)   }   FindHavlakLoops(cfgraph, lsgraph)   for i := 0; i &lt; 50; i++ {      FindHavlakLoops(cfgraph, NewLSG())   }   fmt.Printf(&quot;# of loops: %d (including 1 artificial root node)\/n&quot;, lsgraph.NumLoops())   lsgraph.CalculateNestingLevel()}<\/code><\/pre>\n<p>\u6211\u4eec\u501f\u52a9macbook\u7cfb\u7edf\u4e0a\u7684time\u547d\u4ee4\u6765\u6253\u5370\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\uff08\u5185\u6838\u6001\u3001\u7528\u6237\u6001\u3001\u603b\u65f6\u95f4\uff09\uff1a<\/p>\n<p>\u7f16\u8bd1\u540e\u8fd0\u884c\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082931\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u7528\u6237\u6001\u8017\u65f623.07s\uff0c\u5185\u6838\u6001\u8017\u65f60.4s\uff0c\u603b\u8017\u65f613.7s\uff08\u7528\u4e86\u4e24\u4e2a\u6838\uff0c170%\uff09\u3002\u56e0\u4e3a\u7a0b\u5e8f\u91cc\u9762\u5df2\u7ecf\u5148\u5f00\u542f\u4e86pprof\u7edf\u8ba1cpu\u8017\u65f6\uff0c\u76f4\u63a5top\u547d\u4ee4\u770b\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082932\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cpprof\u6570\u636e\u91c7\u96c6\u6301\u7eed\u4e8612.99s\uff0c\u91c7\u6837\u65f6\u957f\u662f19.49s\uff08\u8fd8\u662f\u4e24\u6838\u7684\u7f18\u6545\uff09\u3002<\/p>\n<p>\u8fd9\u91cc\u8981\u8bf4\u4e00\u4e0b\uff0c\u65e0\u8bba\u662fcpu\u8017\u65f6\u7edf\u8ba1\u8fd8\u662f\u5185\u5b58\u5360\u7528\u7edf\u8ba1\uff0c\u90fd\u662f\u95f4\u9694\u91c7\u6837\u3002cpu\u8017\u65f6\u65f6\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\uff08\u5927\u6982\u662f10ms\uff09\u5bf9\u8c03\u7528\u6808\u7684\u51fd\u6570\u8fdb\u884c\u8bb0\u5f55\uff0c\u6700\u540e\u5206\u6790\u5728\u6240\u6709\u7684\u8bb0\u5f55\u6b21\u6570\u4e2d\uff0c\u5404\u4e2a\u51fd\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5305\u62ec\u5728\u8fd0\u884c\u4e2d\u7684\u6b21\u6570\uff0c\u548c\u5165\u6808\u6b21\u6570\uff08\u8bf4\u660e\u5b83\u8c03\u7528\u4e86\u522b\u7684\u51fd\u6570\uff09\u3002\u5185\u5b58\u5360\u7528\u7edf\u8ba1\u662f\u6bcf\u5206\u914d512K\u8bb0\u5f55\u4e00\u6b21\u5206\u914d\u8def\u5f84\u3002<\/p>\n<p>\u8017\u65f6\u6700\u591a\u7684\u662fmapaccess1_fast64\uff0c\u8fd9\u662f\u8fd0\u884c\u65f6\u4e2d\u7684map\u8bfb\u5199\u65f6\u7684\u6570\u636e\u67e5\u8be2\u51fd\u6570\u3002\u5982\u679c\u7f16\u8bd1\u7a0b\u5e8f\u65f6\u6ca1\u6709\u7981\u7528\u5185\u8054\uff0c\u770b\u5230\u7684\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u5176\u4e2d\u4f1a\u663e\u793aFindHavlakLoops\u51fd\u6570\uff0c\u5e76\u6807\u8bc6\u4e3ainline\u3002\u56e0\u4e3aFindHavlakLoops\u91cc\u9762\u5c31\u8c03\u7528\u4e86FindLoops\uff0c\u6240\u4ee5\u5728\u7f16\u8bd1\u5668\u4f1a\u76f4\u63a5\u628a\u8fd9\u4e2a\u51fd\u6570\u5c55\u5f00\uff0c\u7528FindLoops\u66ff\u6362FindHavlakLoops\u51fd\u6570\u3002\u4e5f\u53ef\u4ee5\u5728\u4e0d\u7981\u7528\u5185\u8054\u7f16\u8bd1\u65f6\uff0c\u8bbe\u7f6epprof\u7684noinlines\u5f00\u5173\u4e3atrue\uff0c\u9ed8\u8ba4\u4e3afalse\uff0c\u5373noinlines=true\u3002<\/p>\n<p>\u8fd9\u91cc\u770b\u5230\u6700\u5927\u7684\u51fd\u6570\u5e76\u4e0d\u662f\u4e1a\u52a1\u51fd\u6570\u800c\u662f\u7cfb\u7edf\u51fd\u6570\uff0c\u90a3\u6ca1\u5565\u597d\u4f18\u5316\u7cfb\u7edf\u51fd\u6570\u7684\uff08\u53ea\u80fd\u662f\u54b1\u7528\u7684\u4e0d\u5bf9\u4e86\u5457~\uff09\u3002\u5c31\u770b\u662f\u54ea\u91cc\u8c03\u7528\u7684\u8fd9\u4e2a\u7cfb\u7edf\u51fd\u6570\uff1a<\/p>\n<p>web mapaccess1_fast64<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082933\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8c03\u7528\u6700\u591a\u7684\u662fDFS\u548cFindLoops\u3002\u90a3\u5c31\u770b\u770b\u8fd9\u4fe9\u51fd\u6570\u91cc\u9762\u662f\u600e\u4e48\u4f7f\u7528map\u7684\uff0c\u6765\u770bDFS\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082934\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cDFS\u51fd\u6570\u91cc\u8017\u65f6\u8f83\u957f\u53c8\u662fmap\u64cd\u4f5c\u7684\uff0c\u662f242 246 \u548c250\u4e09\u884c\u3002\u5bf9\u4e8e\u8fd9\u91cc\u7684\u4f18\u5316\u65b9\u6cd5\uff0c\u662f\u7528list\u7ed3\u6784\u4ee3\u66ffmap\u7ed3\u6784\uff0c\u56e0\u4e3a\u80fd\u7528list\u8fbe\u5230\u6548\u679c\u7684\u60c5\u51b5\u4e0b\u6ca1\u5fc5\u8981\u7528map\u3002\u8fd9\u4e2a\u548b\u4e00\u770b\u6ca1\u95ee\u9898\uff0c\u4f46\u597d\u50cf\u53c8\u6709\u70b9\u522b\u626d\u5bf9\u5427\uff1f\u5176\u5b9e\u662f\u56e0\u4e3a\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u672c\u8eab\u7279\u70b9\uff0c\u8fd9\u91cc\u6709\u660e\u663e\u7684\u4e00\u70b9\uff0c\u5c31\u662fBasicBlock\u7ed3\u6784\u7684\u7279\u6b8a\u6027\uff0c\u672c\u8eab\u7684Name\u5c5e\u6027\u5c31\u662f\u81ea\u8eab\u7684\u7d22\u5f15\u4e0b\u6807\u3002\u67e5\u627e\u67d0\u4e2aBasicBlock\u4e0d\u9700\u8981\u4f7f\u7528map\u6765\u8bb0\u5f55\uff0c\u76f4\u63a5\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\u663e\u7136\u662f\u6700\u4f4e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff08\u5173\u4e8emap\u548clist\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4e0d\u591a\u8bf4\u4e86\uff0cmap\u770b\u8d77\u6765\u662fO1\uff0c\u4f46\u5176\u5b9e\u6ca1\u6709\u8003\u8651hash\u8ba1\u7b97\u672c\u8eab\u7684\u8fc7\u7a0b\u548c\u89e3\u51b3hash\u51b2\u7a81\u7684\u6210\u672c\uff0c\u800clist\u662f\u5fc5\u7136\u7684O1\uff09\u3002\u901a\u8fc7\u628a\u8fd9\u90e8\u5206\u7684map\u4fee\u6539\u4e3alist\u6570\u636e\u7ed3\u6784\uff0c\u7248\u672cH2\uff0c\u518d\u7f16\u8bd1\u67e5\u770b\u8017\u65f6\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082935\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u6b64\u65f6\u8017\u65f6\u964d\u4f4e\u5230\u4e868s\u3002\u518d\u6b21\u67e5\u770bcpu\u8017\u65f6\u5206\u5e03\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082936\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0ctop1\u53d8\u6210\u4e86scanobject\u51fd\u6570\uff0c\u4e0d\u518d\u662fmap\u8bfb\u5199\u4e86\u3002\u770b\u5230\u8fd9\u4e2a\u51fd\u6570\uff0c\u4ee5\u53ca\u4e0b\u8fb9\u7684mallocgc\uff0c\u6211\u4eec\u8981\u77e5\u9053\uff0c\u8fd9\u90fd\u662f\u5185\u5b58\u76f8\u5173\u7684\uff0c\u524d\u8005\u662f\u5185\u5b58\u5bf9\u8c61\u626b\u63cf\u7684\u5904\u7406\u51fd\u6570\uff0c\u662f\u5783\u573e\u56de\u6536\u4e09\u8272\u6807\u8bb0\u7684\u4e00\u4e2a\u8fc7\u7a0b\uff0c\u540e\u8005\u5219\u662f\u76f4\u63a5\u7ba1\u7406\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u7684\uff08\u6ce8\u610f\u662f\u540c\u65f6\u8d1f\u8d23\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\uff0cmalloc &#038;&#038; gc\uff09\u3002\u8fd9\u8bf4\u660e\u76ee\u524d\u7a0b\u5e8f\u82b1\u4e86\u5f88\u591a\u65f6\u95f4\u5728\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u770b\u6bd4\u4f8b\u662f8%\u3002\u90a3\u5c31\u8981\u5206\u6790\u4e00\u4e0b\u4e86\uff0c\u662f\u4ec0\u4e48\u4e1c\u897f\u4ea7\u751f\u4e86\u8fd9\u4e48\u591a\u5185\u5b58\u53d8\u5316\uff0c\u5408\u4e0d\u5408\u7406\u3002\u5206\u6790\u5185\u5b58\uff0c\u5c31\u662fpprof\u7684memprofile\u529f\u80fd\u4e86\u3002\u6dfb\u52a0\u4e00\u4e0b\u76f8\u5173\u7684\u5185\u5b58\u7edf\u8ba1\u4ee3\u7801\uff0c\u5177\u4f53\u600e\u4e48\u6dfb\u52a0\u5927\u5bb6\u80af\u5b9a\u90fd\u4f1a\uff0c\u5c31\u4e0d\u8d34\u4e86\uff08\u7f51\u4e0a\u591a\u5f97\u662f\uff09\u3002\u6dfb\u52a0\u5b8c\u4e4b\u540e\uff0c\u91cd\u65b0\u7f16\u8bd1\u751f\u6210\u7248\u672cH3\uff0c\u6267\u884cH3\uff0c\u751f\u6210\u5bf9\u5e94\u7684\u5185\u5b58\u76d1\u6d4b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082937\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u67e5\u770b\u5185\u5b58\u7684\u5206\u914d\u60c5\u51b5\uff0c\u5728\u8fd9\u91cc\u4e0d\u7981\u6b62\u5185\u8054\uff0c\u56e0\u4e3a\u5728\u5b9e\u9645\u8fd0\u884c\u65f6\u8be5\u5185\u8054\u7684\u51fd\u6570\u4e5f\u662f\u4f1a\u88ab\u5c55\u5f00\u66ff\u6362\u6389\u7684\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082938\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8282\u70b9\u521b\u5efa\u662f\u7b2c\u4e00\uff0cFindLoops\u662f\u7b2c\u4e8c\u3002\u56e0\u4e3a\u521b\u5efaBasicBlock\u9996\u5148\u5f88\u7b80\u5355\uff0c\u6ca1\u6709\u590d\u6742\u7684\u8fc7\u7a0b\uff0c\u5176\u6b21\u8fd9\u662f\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u57fa\u7840\u5bf9\u8c61\u7ed3\u6784\uff0c\u82e5\u8981\u6539\u7ed3\u6784\u4f53\uff0c\u90a3\u53ef\u80fd\u6d89\u53ca\u5230\u7b97\u6cd5\u4e5f\u5f97\u6539\uff0c\u8fd9\u4e2a\u663e\u7136\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4e0d\u4ec5\u53ef\u80fd\u6539\u51fabug\uff0c\u8fd8\u53ef\u80fd\u6536\u76ca\u4e0d\u9ad8\u3002\u90a3\u6211\u4eec\u5c31\u987a\u7740\u770b\u7b2c\u4e8c\u4f4d\u7684FindLoops\uff0c\u8fd9\u4e2a\u5c31\u662f\u524d\u8fb9\u6211\u4eec\u770b\u5230\u7684\u8c03\u7528mapaccess\u5185\u7f6e\u51fd\u6570\u7684\u53e6\u4e00\u4e2a\u4e1a\u52a1\u51fd\u6570\uff0c\u6240\u4ee5\u4f18\u5316\u7684\u65b9\u6cd5\u8ddf\u4e0a\u9762\u7c7b\u4f3c\uff0c\u8fd8\u662f\u4f18\u5316map\u7684\u4f7f\u7528\uff0c\u66ff\u6362\u4e3alist\u3002\u8fd9\u91cc\u6709\u4e00\u70b9\u7279\u6b8a\u7684\u662f\uff0c\u66ff\u6362\u6210list\u4e4b\u540e\uff0cmap\u7684\u8ffd\u52a0\u9700\u8981\u6539\u4e00\u4e0b\uff0c\u52a0\u4e00\u4e2a\u5224\u65ad\u91cd\u590d\u7684\u903b\u8f91\uff0c\u6240\u4ee5\u65b0\u52a0\u4e86\u4e00\u4e2aappendUnique\u65b9\u6cd5\u3002\u518d\u6b21\u7f16\u8bd1\uff0c\u7248\u672cH4\uff0c\u67e5\u770b\u8017\u65f6\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082939\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u8fd9\u65f6\u7a0b\u5e8f\u8017\u65f6\u964d\u5230\u4e865.6s\u3002\u518d\u6b21\u67e5\u770b\u5185\u5b58\u5206\u914d\uff0c\u786e\u8ba4\u4f18\u5316\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082940\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cFindLoops\u51fd\u6570\u5df2\u7ecf\u4e0d\u5728\u9ad8\u4f4d\uff0c\u5185\u5b58\u6d88\u8017\u964d\u4e0b\u53bb\u4e86\u3002\u518d\u770b\u4e00\u4e0b\u6b64\u65f6\u7684cpu\u8017\u65f6\u5206\u5e03\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082941\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cFindLoops\u6210\u4e3atop1\uff0cscanobject\u51fd\u6570\u6210\u4e86\u7b2c\u4e8c\u4f4d\u3002\u8fd9\u5c31\u5bf9\u4e86\uff0c\u56e0\u4e3a<strong>\u6211\u4eec\u5c31\u662f\u8981\u8ba9cpu\u66f4\u591a\u7684\u53bb\u8fd0\u884c\u4e1a\u52a1\u4ee3\u7801\uff0c\u628a\u65f6\u95f4\u82b1\u5230\u771f\u6b63\u9700\u8981\u7684\u5730\u65b9<\/strong>\u3002<\/p>\n<p>\u8fd9\u65f6\u5c31\u53ef\u4ee5\u8fdb\u884c\u4e0b\u4e00\u8f6e\u7684\u6027\u80fd\u4f18\u5316\u4e86\uff08\u8fd9\u5c31\u662f\u6027\u80fd\u8c03\u4f18\uff0c\u4e00\u904d\u4e00\u904d\u7684\u6392\u67e5\u8017\u65f6\uff0c\u538b\u7f29\u4e0d\u5fc5\u8981\u7684cpu\u65f6\u95f4\uff0c\u538b\u69a8\u8ba1\u7b97\u6027\u80fd\uff09\u3002\u7ee7\u7eed\u770b\u4e00\u4e0b\u6b64\u65f6FindLoops\u5230\u5e95\u5728\u54ea\u513f\u5316\u7684\u65f6\u95f4\u591a\uff0c\u662f\u5426\u5408\u7406\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082942\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u4ece\u5404\u4e2a\u8bed\u53e5\u7684\u8017\u65f6\u6765\u770b\uff0c\u597d\u50cf\u6ca1\u5565\u5927\u95ee\u9898\uff0c\u6ca1\u6709\u5f88\u79bb\u8c31\u7684\u8017\u65f6\uff08360ms\u90a3\u884c\u662f\u56e0\u4e3a\u5faa\u73af\u7684\u7f18\u6545\uff09\u3002\u8fd9\u8bf4\u660e\u7f16\u7801\u4e0a\u6ca1\u6709\u5927\u95ee\u9898\uff0c\u6309\u7167\u6211\u4eec\u524d\u8fb9\u4e00\u5f00\u59cb\u8bf4\u7684\u7a0b\u5e8f\u4f18\u5316\u7684\u6b65\u9aa4\uff0c\u5c31\u9700\u8981\u5f80\u4e0a\u627e\u4e86\uff0c\u770b\u80fd\u4e0d\u80fd\u4f18\u5316\u903b\u8f91\u6765\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\uff0c\u4ee5\u8fbe\u5230\u63d0\u5347\u6027\u80fd\u7684\u76ee\u7684\uff08\u5373\uff0c\u6bcf\u4e00\u4e2a\u8ba1\u7b97\u6b65\u9aa4\u5904\u7406\u7684\u90fd\u6ca1\u5565\u5927\u95ee\u9898\u4e86\uff0c\u90a3\u80fd\u4e0d\u80fd\u5c11\u7b97\u70b9\u513f\uff09\u3002\u8fd9\u91ccFindLoops\u5728\u7a0b\u5e8f\u5165\u53e3\uff0c\u82b1\u4e86\u4e0d\u5c11\u65f6\u95f4\u6765\u521d\u59cb\u5316\u4e00\u7cfb\u5217\u4e34\u65f6\u53d8\u91cf\uff0c\u52a0\u8d77\u6765\u6709180ms\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u6bcf\u6b21\u8c03\u7528FindLoops\u51fd\u6570\uff0c\u90fd\u8981\u5148\u82b1180ms\u7684\u51c6\u5907\u5de5\u4f5c\u3002\u8fd9\u90e8\u5206\u4e34\u65f6\u53d8\u91cf\u7684\u591a\u6b21\u521b\u5efa+\u521d\u59cb\u5316\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a0\u5185\u5b58\u7f13\u5b58\u7684\u65b9\u5f0f\u6765\u51cf\u5c11\u91cd\u590d\u521b\u5efa\u548c\u7533\u8bf7\uff0c\u8fd9\u91cc\u6d89\u53ca\u5230\u7684\u6807\u51c6\u89e3\u6cd5\u5176\u5b9e\u5c31\u662f\u5bf9\u8c61\u6c60\uff08\u50cfGo\u521b\u5efa\u7684web\u670d\u52a1\uff0c\u5728\u5e76\u53d1\u91cf\u5f88\u9ad8\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e00\u6b21http\u8bf7\u6c42\u7684\u89e3\u6790\u90fd\u9700\u8981\u521b\u5efa\u5bf9\u8c61+\u7533\u8bf7\u5185\u5b58+\u53cd\u5e8f\u5217\u8fd9\u4e00\u7cfb\u5217\u7684\u521d\u59cb\u52a8\u4f5c\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u501f\u52a9sync.Pool\u6765\u51cf\u5c11\u8fd9\u79cd\u91cd\u590d\u5de5\u4f5c\uff0c\u63d0\u5347\u6027\u80fd\uff09\u3002\u540c\u7406\uff0c\u8fd9\u91cc\u4e5f\u662f\u52a0\u5165\u4e86\u4e00\u4e2a\u5185\u5b58\u7f13\u5b58\u5bf9\u8c61cache\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082943\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u628a\u539f\u672c\u7684\u5185\u5b58\u7533\u8bf7\u521d\u59cb\u5316\u8fc7\u7a0b\u505a\u4e86\u66ff\u6362\u3002\u5728\u539f\u6709\u7f13\u5b58\u4e0d\u591f\u7684\u60c5\u51b5\u4e0b\uff0c\u7533\u8bf7\u65b0\u7684\u53d8\u91cf\uff0c\u5426\u8005\u622a\u53d6\u539f\u6709\u7f13\u5b58\u4f7f\u7528\uff0c\u51cf\u5c11\u5185\u5b58\u7533\u8bf7\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082944\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u8c03\u6574\u5b8c\u6bd5\u540e\uff0c\u7f16\u8bd1\u65b0\u7248\u672cH5\uff0c\u518d\u770b\u4e0b\u8017\u65f6\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082945\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u8fd9\u65f6\u5019\u7a0b\u5e8f\u7684\u8017\u65f6\u5df2\u7ecf\u964d\u52304.1s\uff0c\u76f8\u6bd4\u4e00\u5f00\u59cb\u768413.7s\uff0c\u5df2\u7ecf\u63d0\u5347\u4e86\u4e24\u500d\u591a\u3002\u770b\u4e0b\u73b0\u5728\u7684cpu\u8017\u65f6\u5206\u5e03\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043082946\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u76f8\u6bd4\u4e0a\u6b21\u7684\u5206\u5e03\uff0c\u524d\u4e24\u4f4d\u90fd\u662f\u4e1a\u52a1\u4ee3\u7801\u51fd\u6570\u4e86\uff0c\u8bf4\u660e\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u4e1a\u52a1\u4ee3\u7801\u7684\u8017\u65f6\u5360\u6bd4\uff0c\u964d\u4f4e\u4e86\u65e0\u5173\u7cfb\u7edf\u51fd\u6570\u7684\u8d1f\u8f7d\u3002\u8fd9\u79cd\u76f4\u63a5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u5f0f\u52a0cache\u4e0d\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff0c\u4f46\u662f\u56e0\u4e3a\u8fd9\u91cc\u7a0b\u5e8f\u7684\u903b\u8f91\u672c\u8eab\u4e5f\u4e0d\u662f\u5e76\u53d1\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u6ca1\u95ee\u9898\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u5b9e\u64cd\u7684\u4f18\u5316\u8fc7\u7a0b\u5c31\u8d70\u5b8c\u4e86\u3002\u63d0\u70bc\u603b\u7ed3\u4e00\u4e0b\u4f18\u5316\u7684\u6b65\u9aa4\u548c\u4f7f\u7528\u7684\u4e3b\u8981\u65b9\u6cd5\u547d\u4ee4\u6709\uff1a<\/p>\n<p>1.\u901a\u8fc7top5\u67e5\u770bcpu\u8017\u65f6\uff1a\u786e\u5b9a\u662f\u5b57\u5178\u6570\u636e\u64cd\u4f5c\u5360\u6bd4\u6700\u5927\uff1b<\/p>\n<p>2.\u901a\u8fc7web\u547d\u4ee4\u67e5\u770b\u662f\u8c01\u4e3b\u8981\u8c03\u7528\u4e86\u5b57\u5178\u64cd\u4f5c\u51fd\u6570\uff1a\u786e\u5b9a\u6709DFS\u548cFindLoops\uff1b<\/p>\n<p>3.\u4f7f\u7528list \u547d\u4ee4\u67e5\u770bDFS\u51fd\u6570\u4e2d\u6bcf\u884c\u4ee3\u7801\u7684\u8017\u65f6\uff0c\u627e\u5230\u8ddfmap\u64cd\u4f5c\u76f8\u5173\u7684\u90e8\u5206\uff0c\u786e\u5b9a\u4f18\u5316\u65b9\u6cd5\uff1a\u628amap\u6362\u6210list\uff1b<\/p>\n<p>4.\u91cd\u65b0\u8fd0\u884c\uff0c\u518d\u7528top\u547d\u4ee4\u67e5\u770bcpu\u8017\u65f6\u5206\u5e03\uff1a\u627e\u5230\u8017\u65f6\u6700\u5927\u7684\u662f\u5185\u5b58\u5bf9\u8c61\u626b\u63cf\u51fd\u6570scanobject\uff1b<\/p>\n<p>5.\u52a0\u5165\u5185\u5b58\u5256\u6790\u4ee3\u7801\uff0c\u8fd0\u884c\u67e5\u770b\u5185\u5b58\u5206\u914d\u7684\u5927\u5c0f\u5206\u5e03\uff1a\u786e\u5b9aFindLoops\u5360\u7528\u8f83\u5927\uff1b<\/p>\n<p>6.\u4f7f\u7528list\u547d\u4ee4\u67e5\u770bFindLoops\u51fd\u6570\u7684\u6bcf\u884c\u5185\u5b58\u5f00\u9500\uff1a\u786e\u5b9a\u662fmap\u521b\u5efa\u5360\u7528\u5185\u5b58\u8f83\u5927\uff1b<\/p>\n<p>7.\u540c\u6837\u4f7f\u7528list\u6570\u636e\u7ed3\u6784\u66ff\u6362map\uff0c\u91cd\u65b0\u8fd0\u884c\uff0c\u518d\u770b\u5185\u5b58\u5927\u5c0f\u5206\u5e03\uff1a\u786e\u5b9aFindLoops\u5df2\u4e0d\u5728\u9ad8\u4f4d\uff0c\u540c\u65f6\u518d\u770bCPU\u8017\u65f6\u65f6\uff0cscanobject\u5df2\u7ecf\u964d\u4e0b\u53bb\u4e86\uff0c\u76ee\u7684\u8fbe\u5230\uff1b<\/p>\n<p>8.\u6b64\u65f6\u53c8\u5f00\u59cb\u65b0\u7684\u4e00\u8f6e\u6392\u67e5\uff0c\u7ee7\u7eed\u67e5\u770bcpu\u8017\u65f6\u6392\u884c\u699c\uff0cFindLoops\u8017\u65f6\u5c45\u9996\uff1b<\/p>\n<p>9.\u7ee7\u7eed\u4f7f\u7528list\u65b9\u6cd5\u770bFindLoops\u51fd\u6570\u6bcf\u884c\u7684\u8017\u65f6\uff0c\u6ca1\u6709\u660e\u663e\u7684\u95ee\u9898\u3002\u90a3\u5c31\u8981\u6362\u601d\u8def\uff0c\u4ece\u6392\u67e5\u7f16\u7801\u95ee\u9898\u8f6c\u6362\u4e3a\u6392\u67e5\u903b\u8f91\u95ee\u9898\uff0c\u51cf\u5c11\u8ba1\u7b97\u8fc7\u7a0b\uff1a\u8fd9\u91cc\u662f\u52a0\u7f13\u5b58\uff1b<\/p>\n<p>10.\u52a0\u5b8c\u7f13\u5b58\u770b\u5230\u6027\u80fd\u660e\u663e\u63d0\u5347\u4e86\uff0c\u8bf4\u660e\u4f18\u5316\u5bf9\u4e86\u3002\u8fd9\u65f6\u53c8\u8be5\u5faa\u73af\u8fdb\u5165\u4e0b\u4e00\u8f6e\u7684\u6392\u67e5\u7684\u4f18\u5316\u4e86\uff0c\u4ee5\u6b64\u5f80\u590d\u3002\u3002\u3002\u76f4\u5230\u538b\u69a8\u51fa\u6211\u4eec\u80fd\u8fbe\u5230\u7684\u6700\u5927\u6027\u80fd\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6b21\u7a0b\u5e8f\u4f18\u5316\u7684\u6574\u4f53\u6b65\u9aa4\u548c\u601d\u8def\uff0c\u8fc7\u7a0b\u4e2d\u79c9\u6301\u7684\u601d\u8def\u65b9\u6cd5\u662f\u4e00\u8d2f\u7684\uff0c<strong>\u5c31\u662f\u4e0d\u65ad\u7684\u7528pprof\u6392\u67e5top\u7ea7\u7684\u51fd\u6570\u8017\u65f6\u548c\u5185\u5b58\u5360\u7528\uff0c\u901a\u8fc7\u4f18\u5316\u6570\u636e\u7ed3\u6784\u3001\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3001\u964d\u4f4e\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u7684\u8d1f\u62c5\u7b49\u65b9\u5f0f\u6765\u63d0\u5347\u6027\u80fd\uff0c\u8fd9\u4e00\u70b9\u5bf9\u6240\u6709\u7684\u7a0b\u5e8f\u90fd\u9002\u7528\uff0c\u4e5f\u662f\u540e\u7eed\u53ef\u4ee5\u501f\u9274\u7684\u65b9\u6cd5\u8bba<\/strong>\u3002<\/p>\n<h2>\u4e24\u70b9\u611f\u609f<\/h2>\n<p>1.<strong>\u4f18\u5316\u7a0b\u5e8f\u7684\u5927\u524d\u63d0\u662f\u4f60\u4e00\u5b9a\u8981\u5bf9\u7a0b\u5e8f\u6709\u8db3\u591f\u6df1\u5165\u7684\u7406\u89e3\uff01<\/strong> \uff08\u6216\u8005\u8bf4\u6211\u4eec\u4e0d\u80fd\u4f18\u5316\u7a0b\u5e8f\u4f18\u5316\u51fabug\u6765\u554a\u3002\u3002\u3002\uff09\u3002\u6700\u540e\u751f\u4ea7H6\u7248\u672c\uff0c\u4e4b\u6240\u4ee5\u53c8\u5bf9\u6027\u80fd\u63d0\u5347\u4e86\u4e00\u500d\uff0c\u5168\u662f\u5efa\u7acb\u5728\u4f5c\u8005\u5bf9\u7a0b\u5e8f\u5b8c\u5168\u7406\u89e3\u7684\u57fa\u7840\u4e4b\u4e0a\u7684\uff0c\u6240\u4ee5\u4ed6\u624d\u53ef\u4ee5\u8c03\u6574\u5faa\u73af\u903b\u8f91\uff0c\u590d\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u8c03\u6574\u8c03\u7528\u903b\u8f91\u7b49\u7b49\u3002\u8fd9\u4e00\u5c42\u518d\u5f80\u4e0a\u5c42\u601d\u8003\uff0c\u5c31\u5230\u4e86\u7a0b\u5e8f\u903b\u8f91\uff0c\u7cfb\u7edf\u67b6\u6784\u5185\u7f6e\u6574\u4f53\u7684\u4e1a\u52a1\u67b6\u6784\u5c42\u9762\u4e86\u3002\u8fd9\u5176\u5b9e\u5c31\u53c8\u56de\u5230\u4e86\u4e00\u5f00\u59cb\u6211\u4eec\u8bf4\u7684\u7a0b\u5e8f\u4f18\u5316\u7531\u5927\u5230\u5c0f\u7684\u603b\u4f53\u601d\u8def\u4e0a\u4e86\u3002<\/p>\n<p>2.<strong>\u5e26GC\u7684\u8bed\u8a00\u76f8\u6bd4\u4e0d\u5e26\u7684\uff0c\u53cd\u800c\u66f4\u8003\u9a8c\u5185\u5b58\u7ba1\u7406\u7684\u6280\u672f<\/strong>\u3002Go\u8bed\u8a00\u5728\u5f00\u53d1\u6548\u7387\u4e0a\u7684\u63d0\u5347\uff0c\u662f\u628a\u5783\u573e\u56de\u6536\u4ea4\u7ed9\u4e86\u7cfb\u7edf\u6765\u5904\u7406\uff0c\u4f46\u522b\u5fd8\u4e86\uff0c\u6d88\u8017\u7684\u4f9d\u7136\u662f\u6211\u4eec\u81ea\u5df1\u7684cpu\u65f6\u95f4\uff08\u7f8a\u6bdb\uff0c\u90a3\u4e0d\u5f97\u4ece\u7f8a\u8eab\u4e0a\u6765&#8230;\uff09\u3002\u6240\u4ee5\u6211\u4eec\u5728\u4f7f\u7528\u6bcf\u79cd\u7ed3\u6784\u4f53\uff0c\u8fdb\u884c\u6bcf\u79cd\u8ba1\u7b97\u64cd\u4f5c\u65f6\uff0c\u90fd\u8981\u660e\u767d\u5176\u80cc\u540e\u6d89\u53ca\u7684\u5185\u5b58\u5f00\u9500\uff0c\u8981\u628a\u5185\u5b58\u53d8\u5316\u653e\u5230\u6f5c\u610f\u8bc6\u91cc\u53bb\u7ba1\u7406\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6b21pprof\u4f18\u5316Go\u7a0b\u5e8f\u7684\u5b9e\u64cd\u8fc7\u7a0b\u53ca\u603b\u7ed3\u611f\u60f3\uff0c\u4f9b\u53c2\u8003\uff0c\u611f\u8c22~<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043082929\">https:\/\/segmentfault.com\/a\/1190000043082929<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u803f\u5b97\u6770 \u524d\u8a00 \u5173\u4e8epprof\u7684\u6587\u7ae0\u5728\u7f51\u4e0a\u5df2\u662f\u6c57\u725b\u5145\u680b\uff0c\u5374\u662f\u5343\u7bc7\u4e00\u5f8b\u7684\u547d\u4ee4\u4ecb\u7ecd\uff0c\u9c9c\u6709\u771f\u6b63\u5b9e\u64cd\u7684\uff0c\u672c\u6587\u5c06\u53c2\u8003 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006974","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006974"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006974\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}