title: How to optimize golang program that spends most time in runtime.osyield and runtime.usleep
body: <p>I've been working on optimizing code that analyzes social graph data (with lots of help from <a href="https://blog.golang.org/profiling-go-programs" rel="noreferrer">https://blog.golang.org/profiling-go-programs</a>) and I've successfully reworked a lot of slow code.</p>
  
  <p>All data is loaded into memory from db first, and the data analysis from there appears CPU bound (max memory consumption &lt; 10MB, CPU1 @ 100%)</p>
  
  <p>But now most of my program's time seems to be in runtime.osyield and runtime.usleep. What's the way to prevent that?</p>
  
  <p>I've set GOMAXPROCS=1 and the code does not spawn any goroutines (other than what the golang libraries may call).</p>
  
  <p>This is my top10 output from pprof</p>
  
  <pre><code>(pprof) top10
  62550ms of 72360ms total (86.44%)
  Dropped 208 nodes (cum &lt;= 361.80ms)
  Showing top 10 nodes out of 77 (cum &gt;= 1040ms)
        flat  flat%   sum%        cum   cum%
     20760ms 28.69% 28.69%    20850ms 28.81%  runtime.osyield
     14070ms 19.44% 48.13%    14080ms 19.46%  runtime.usleep
     11740ms 16.22% 64.36%    23100ms 31.92%  _/C_/code/sc_proto/cloudgraph.(*Graph).LeafProb
      6170ms  8.53% 72.89%     6170ms  8.53%  runtime.memmove
      4740ms  6.55% 79.44%    10660ms 14.73%  runtime.typedslicecopy
      2040ms  2.82% 82.26%     2040ms  2.82%  _/C_/code/sc_proto.mAvg
       890ms  1.23% 83.49%     1590ms  2.20%  runtime.scanobject
       770ms  1.06% 84.55%     1420ms  1.96%  runtime.mallocgc
       760ms  1.05% 85.60%      760ms  1.05%  runtime.heapBitsForObject
       610ms  0.84% 86.44%     1040ms  1.44%  _/C_/code/sc_proto/cloudgraph.(*Node).DeepestChildren
  (pprof)
  </code></pre>
  
  <p>The _ /C_/code/sc_proto/* functions are my code.</p>
  
  <p>And the output from web:
  <a href="https://i.stack.imgur.com/Xjbvb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Xjbvb.png" alt="output from web"></a></p>
  
  <p>(better, SVG version of graph here: <a href="https://goo.gl/Tyc6X4" rel="noreferrer">https://goo.gl/Tyc6X4</a>)</p>