Skip to content

Commit

Permalink
debug/gosym: use sort.Search in findFunc
Browse files Browse the repository at this point in the history
Use sort.Search instead of open-coding the binary search.
This makes the code a lot easier to work on.

As a bonus, it speeds it up.

name            old time/op    new time/op    delta
115/LineToPC-8    57.4µs ± 5%    59.2µs ± 8%   +3.19%  (p=0.003 n=15+13)
115/PCToLine-8     255ns ± 1%     192ns ± 3%  -24.63%  (p=0.000 n=15+15)

Change-Id: I41da18bfb0e745c40d24e5b96e50dfdd0c3b79f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/353879
Trust: Josh Bleecher Snyder <[email protected]>
Run-TryBot: Josh Bleecher Snyder <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
josharian committed Oct 4, 2021
1 parent 7c79e8e commit 199ec42
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/debug/gosym/pclntab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package gosym
import (
"bytes"
"encoding/binary"
"sort"
"sync"
)

Expand Down Expand Up @@ -319,25 +320,13 @@ func (t *LineTable) findFunc(pc uint64) funcData {
if pc < t.uintptr(t.functab) || pc >= t.uintptr(t.functab[len(t.functab)-int(t.ptrsize):]) {
return funcData{}
}

// The function table is a list of 2*nfunctab+1 uintptrs,
// alternating program counters and offsets to func structures.
f := t.functab
nf := t.nfunctab
for nf > 0 {
m := nf / 2
fm := f[2*t.ptrsize*m:]
if t.uintptr(fm) <= pc && pc < t.uintptr(fm[2*t.ptrsize:]) {
data := t.funcdata[t.uintptr(fm[t.ptrsize:]):]
return funcData{t: t, data: data}
} else if pc < t.uintptr(fm) {
nf = m
} else {
f = f[(m+1)*2*t.ptrsize:]
nf -= m + 1
}
}
return funcData{}
idx := sort.Search(int(t.nfunctab), func(i int) bool {
return t.uintptr(t.functab[2*i*int(t.ptrsize):]) > pc
})
idx--
return t.funcData(uint32(idx))
}

// readvarint reads, removes, and returns a varint from *pp.
Expand Down

0 comments on commit 199ec42

Please sign in to comment.