mirror of
https://github.com/golang/go
synced 2024-11-18 12:04:57 -07:00
debug/dwarf: optimize buf.string
(*buf).string previously manually searched through its underlying byte slice until we encountered a '0'. This change instead uses bytes.IndexByte that results in a speed up: $ benchstat before.txt after.txt name old time/op new time/op delta BufString-8 257ns ± 1% 174ns ± 1% -32.37% (p=0.000 n=9+8) name old speed new speed delta BufString-8 495MB/s ± 1% 732MB/s ± 1% +47.76% (p=0.000 n=10+8) name old alloc/op new alloc/op delta BufString-8 162B ± 0% 162B ± 0% ~ (all equal) name old allocs/op new allocs/op delta BufString-8 3.00 ± 0% 3.00 ± 0% ~ (all equal) Change-Id: I7cf241742cc091d5d30d987a168b02d83955b1cf Reviewed-on: https://go-review.googlesource.com/c/go/+/196657 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
parent
bb0a8fd38f
commit
0d9981bccb
@ -7,6 +7,7 @@
|
||||
package dwarf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
@ -79,16 +80,16 @@ func (b *buf) bytes(n int) []byte {
|
||||
func (b *buf) skip(n int) { b.bytes(n) }
|
||||
|
||||
func (b *buf) string() string {
|
||||
for i := 0; i < len(b.data); i++ {
|
||||
if b.data[i] == 0 {
|
||||
s := string(b.data[0:i])
|
||||
b.data = b.data[i+1:]
|
||||
b.off += Offset(i + 1)
|
||||
return s
|
||||
}
|
||||
i := bytes.IndexByte(b.data, 0)
|
||||
if i < 0 {
|
||||
b.error("underflow")
|
||||
return ""
|
||||
}
|
||||
b.error("underflow")
|
||||
return ""
|
||||
|
||||
s := string(b.data[0:i])
|
||||
b.data = b.data[i+1:]
|
||||
b.off += Offset(i + 1)
|
||||
return s
|
||||
}
|
||||
|
||||
func (b *buf) uint16() uint16 {
|
||||
|
Loading…
Reference in New Issue
Block a user