Reuse slices when possible

When a slice is already allocated we can just reuse it by resetting it
instead of creating a new slice.
This commit is contained in:
Erik Dubbelboer
2017-01-31 17:02:53 +08:00
parent edd3ef1dab
commit 8b5cbcbffd
+8 -4
View File
@@ -106,10 +106,14 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
fmt.Fprintln(g.out, ws+" "+out+" = nil")
fmt.Fprintln(g.out, ws+"} else {")
fmt.Fprintln(g.out, ws+" in.Delim('[')")
fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {")
fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")")
fmt.Fprintln(g.out, ws+" } else {")
fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}")
fmt.Fprintln(g.out, ws+" if "+out+" == nil {")
fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {")
fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")")
fmt.Fprintln(g.out, ws+" } else {")
fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}")
fmt.Fprintln(g.out, ws+" }")
fmt.Fprintln(g.out, ws+" } else { ")
fmt.Fprintln(g.out, ws+" "+out+" = ("+out+")[:0]")
fmt.Fprintln(g.out, ws+" }")
fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {")
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem))