Преглед изворни кода

Merge pull request #137 from liggitt/reset-vars

Reset var name suffix before each method

This helps reduce the chance of small changes generating diffs across multiple generated methods,
by keeping variable names scoped to a method.

Nice quick small isolated fix for a measurable gain. I can't think of anything better in the short term.you

Thank you.

Fixes #136
Ugorji Nwoke пре 10 година
родитељ
комит
f4485b318a
1 измењених фајлова са 8 додато и 0 уклоњено
  1. 8 0
      codec/gen.go

+ 8 - 0
codec/gen.go

@@ -268,6 +268,7 @@ func Gen(w io.Writer, buildTags, pkgName, uid string, useUnsafe bool, ti *TypeIn
 	x.line("type " + x.hn + " struct{}")
 	x.line("")
 
+	x.varsfxreset()
 	x.line("func init() {")
 	x.linef("if %sGenVersion != %v {", x.cpfx, GenVersion)
 	x.line("_, file, _, _ := runtime.Caller(0)")
@@ -311,6 +312,7 @@ func Gen(w io.Writer, buildTags, pkgName, uid string, useUnsafe bool, ti *TypeIn
 	for _, t := range x.ts {
 		rtid := reflect.ValueOf(t).Pointer()
 		// generate enc functions for all these slice/map types.
+		x.varsfxreset()
 		x.linef("func (x %s) enc%s(v %s%s, e *%sEncoder) {", x.hn, x.genMethodNameT(t), x.arr2str(t, "*"), x.genTypeName(t), x.cpfx)
 		x.genRequiredMethodVars(true)
 		switch t.Kind() {
@@ -325,6 +327,7 @@ func Gen(w io.Writer, buildTags, pkgName, uid string, useUnsafe bool, ti *TypeIn
 		x.line("")
 
 		// generate dec functions for all these slice/map types.
+		x.varsfxreset()
 		x.linef("func (x %s) dec%s(v *%s, d *%sDecoder) {", x.hn, x.genMethodNameT(t), x.genTypeName(t), x.cpfx)
 		x.genRequiredMethodVars(false)
 		switch t.Kind() {
@@ -410,6 +413,10 @@ func (x *genRunner) varsfx() string {
 	return strconv.FormatUint(x.c, 10)
 }
 
+func (x *genRunner) varsfxreset() {
+	x.c = 0
+}
+
 func (x *genRunner) out(s string) {
 	if _, err := io.WriteString(x.w, s); err != nil {
 		panic(err)
@@ -496,6 +503,7 @@ func (x *genRunner) selfer(encode bool) {
 	// always make decode use a pointer receiver,
 	// and structs always use a ptr receiver (encode|decode)
 	isptr := !encode || t.Kind() == reflect.Struct
+	x.varsfxreset()
 	fnSigPfx := "func (x "
 	if isptr {
 		fnSigPfx += "*"