From: Matt Joiner Date: Sun, 17 Jun 2018 06:21:04 +0000 (+1000) Subject: bencode: Remove string allocation when parsing strings X-Git-Tag: v1.0.0~127^2~6 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c5cc570e4237cab19f4d0c9c02fe36d10adeecc9;p=btrtrc.git bencode: Remove string allocation when parsing strings --- diff --git a/bencode/decode.go b/bencode/decode.go index 2905aec9..798ad15e 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -10,6 +10,7 @@ import ( "runtime" "strconv" "strings" + "unsafe" ) type Decoder struct { @@ -404,7 +405,11 @@ func (d *Decoder) readOneValue() bool { if b >= '0' && b <= '9' { start := d.buf.Len() - 1 d.readUntil(':') - length, err := strconv.ParseInt(d.buf.String()[start:], 10, 64) + s := reflect.StringHeader{ + uintptr(unsafe.Pointer(&d.buf.Bytes()[start])), + d.buf.Len() - start, + } + length, err := strconv.ParseInt(*(*string)(unsafe.Pointer(&s)), 10, 64) checkForIntParseError(err, d.Offset-1) d.buf.WriteString(":")