From 8b5cbcbffd9c8f5b6d5f86705da195a1da7719d2 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Tue, 31 Jan 2017 17:02:53 +0800 Subject: [PATCH] Reuse slices when possible When a slice is already allocated we can just reuse it by resetting it instead of creating a new slice. --- gen/decoder.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gen/decoder.go b/gen/decoder.go index 8a7afbd..0ee2626 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -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))