Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Speed up array/slice decoding

  • Loading branch information...
commit 2ee704a94834f63de1fb58503d48e9f29817c971 1 parent a7e1b68
@edsrzf authored
Showing with 25 additions and 5 deletions.
  1. +25 −5 decode.go
View
30 decode.go
@@ -134,15 +134,24 @@ func unmarshal(data string, v reflect.Value) (int, error) {
func unmarshalArray(data string, v reflect.Value, kind reflect.Kind) error {
i := 0
- elType := v.Type().Elem()
- elVal := reflect.Zero(elType)
+ vtype := v.Type()
+ l := v.Cap()
+ if v.Len() < l {
+ v.SetLen(l)
+ }
for len(data) > 0 {
- if i >= v.Len() {
+ if i >= l {
if kind == reflect.Array {
break
- } else {
- v.Set(reflect.Append(v, elVal))
}
+ newl := l + l/2
+ if newl < 4 {
+ newl = 4
+ }
+ newv := reflect.MakeSlice(vtype, newl, newl)
+ reflect.Copy(newv, v)
+ v.Set(newv)
+ l = newl
}
el := v.Index(i)
i++
@@ -152,6 +161,17 @@ func unmarshalArray(data string, v reflect.Value, kind reflect.Kind) error {
return err
}
}
+ if i < l {
+ if kind == reflect.Array {
+ // zero out the rest
+ z := reflect.Zero(vtype.Elem())
+ for ; i < l; i++ {
+ v.Index(i).Set(z)
+ }
+ } else {
+ v.SetLen(i)
+ }
+ }
return nil
}
Please sign in to comment.
Something went wrong with that request. Please try again.