From c5cc570e4237cab19f4d0c9c02fe36d10adeecc9 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 17 Jun 2018 16:21:04 +1000 Subject: [PATCH] bencode: Remove string allocation when parsing strings --- bencode/decode.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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(":") -- 2.48.1