]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: Remove string allocation when parsing strings
authorMatt Joiner <anacrolix@gmail.com>
Sun, 17 Jun 2018 06:21:04 +0000 (16:21 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 17 Jun 2018 06:21:04 +0000 (16:21 +1000)
bencode/decode.go

index 2905aec94554dc56a59f3ef1211322fe3d016281..798ad15e9c8176d038fb23fdbddc87292d4c6e76 100644 (file)
@@ -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(":")