Skip to content

Commit

Permalink
use binary search to optimize performance
Browse files Browse the repository at this point in the history
Signed-off-by: Lancelot <1984737645@qq.com>
  • Loading branch information
Lan-ce-lot committed Mar 17, 2023
1 parent 9cb020f commit 311b498
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pkg/ksyms/ksyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,23 @@ func (k *Ksyms) GetFnOffset(addr uint64) (*FnOffset, error) {

// GetFnOffset -- retruns the FnOffset for a given address
func (k *Ksyms) getFnOffset(addr uint64) (*FnOffset, error) {

// TODO: we can do binary search here if we care about performance
i := 0
for k.table[i].addr < addr {
i++
l, r := 0, len(k.table)-1

// binary search
for l < r {
m := (l + r + 1) >> 1
if k.table[m].addr < addr {
l = m
} else {
r = m - 1
}
}

if i == 0 {
if l == 0 {
return nil, fmt.Errorf("address %d is before first sumbol %s@%d", addr, k.table[0].name, k.table[0].addr)
}

sym := k.table[i-1]
sym := k.table[l]
if !sym.isFunction() {
return nil, fmt.Errorf("Unable to find function for addr 0x%x", addr)
}
Expand Down

0 comments on commit 311b498

Please sign in to comment.