Skip to content

Instantly share code, notes, and snippets.

@quux00
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quux00/16131fa51ba26a748d0b to your computer and use it in GitHub Desktop.
Save quux00/16131fa51ba26a748d0b to your computer and use it in GitHub Desktop.
Go pprof listing for ReadVarIntToUint
(pprof) list varint.ReadVarIntToUint
Total: 4.27mins
ROUTINE ======================== g/q/o/o/b/varint.ReadVarIntToUint
19.20s 1.86mins (flat, cum) 43.51% of Total
370ms 370ms 60:func ReadVarIntToUint(r io.Reader) (uint64, error) {
. . 61: var (
130ms 130ms 62: varbs []byte
230ms 4.65s 63: ba [1]byte
190ms 6.51s 64: u uint64
. . 65: n int
90ms 90ms 66: err error
. . 67: )
. . 68:
380ms 380ms 69: varbs = make([]byte, 0, 10)
. . 70:
. . 71: /* ---[ read in all varint bytes ]--- */
. . 72: for {
-> 3.74s 16.83s 73: n, err = r.Read(ba[:])
740ms 740ms 74: if err != nil {
. . 75: return 0, oerror.NewTrace(err)
. . 76: }
20ms 20ms 77: if n != 1 {
. . 78: return 0, oerror.IncorrectNetworkRead{Expected: 1, Actual: n}
. . 79: }
3.83s 3.83s 80: varbs = append(varbs, ba[0])
930ms 930ms 81: if IsFinalVarIntByte(ba[0]) {
520ms 520ms 82: varbs = append(varbs, byte(0x0))
. . 83: break
. . 84: }
. . 85: }
. . 86:
. . 87: /* ---[ decode ]--- */
-> 280ms 14.82s 88: var buf bytes.Buffer
40ms 40ms 89: if len(varbs) == 1 {
. . 90: buf.WriteByte(varbs[0])
. . 91:
. . 92: } else {
. . 93: var right, left uint
750ms 750ms 94: for i := 0; i < len(varbs)-1; i++ {
80ms 80ms 95: right = uint(i) % 8
340ms 340ms 96: left = 7 - right
40ms 40ms 97: if i == 7 {
. . 98: continue
. . 99: }
660ms 660ms 100: vbcurr := varbs[i]
1.16s 1.16s 101: vbnext := varbs[i+1]
. . 102:
10ms 10ms 103: x := vbcurr & byte(0x7f)
510ms 510ms 104: y := x >> right
660ms 660ms 105: z := vbnext << left
-> 1.28s 15.93s 106: buf.WriteByte(y | z)
. . 107: }
. . 108: }
. . 109:
-> 130ms 8.53s 110: padTo8Bytes(&buf)
-> 1.68s 32.41s 111: err = binary.Read(&buf, binary.LittleEndian, &u)
120ms 120ms 112: if err != nil {
. . 113: return 0, err
. . 114: }
290ms 290ms 115: return u, nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment