]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: Avoid Value.Interface call testing for big.Int
authorMatt Joiner <anacrolix@gmail.com>
Tue, 17 Jul 2018 11:25:15 +0000 (21:25 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 17 Jul 2018 11:25:15 +0000 (21:25 +1000)
Was resulting in significant allocation.

bencode/encode.go

index e364a5d317cb958a514466f98182d37fd210bd5a..d6b6334f38331c857d79ed3b7194b3c3906845da 100644 (file)
@@ -105,16 +105,18 @@ func (e *Encoder) reflectMarshaler(v reflect.Value) bool {
        return false
 }
 
+var bigIntType = reflect.TypeOf(big.Int{})
+
 func (e *Encoder) reflectValue(v reflect.Value) {
 
        if e.reflectMarshaler(v) {
                return
        }
 
-       switch t := v.Interface().(type) {
-       case big.Int:
+       if v.Type() == bigIntType {
                e.writeString("i")
-               e.writeString(t.String())
+               bi := v.Interface().(big.Int)
+               e.writeString(bi.String())
                e.writeString("e")
                return
        }