1
0
mirror of https://github.com/golang/go synced 2024-11-18 02:04:45 -07:00

cmd/compile: eliminate use of Trecur in formatting routines

CL 38147 eliminated package gc globals in formatting routines.
However, tconv still used the Type field Trecur
to avoid infinite recursion when formatting recursive
interfaces types such as (test/fixedbugs398.go):

type i1 interface {
	F() interface {
		i1
	}
}

type i2 interface {
	F() interface {
		i2
	}
}

This CL changes the recursion prevention to use a parameter,
and threads it through the formatting routines.
Because this fundamentally limits the embedding depth
of all types, it sets the depth limit to be much higher.
In practice, it is unlikely to impact any code at all,
one way or the other.

The remaining uses of Type.Trecur are boolean in nature.
A future CL will change Type.Trecur to be a boolean flag.

The removal of a couple of mode.Sprintf calls
makes this a very minor net performance improvement:

name       old alloc/op    new alloc/op    delta
Template      40.0MB ± 0%     40.0MB ± 0%  -0.13%  (p=0.032 n=5+5)
Unicode       30.0MB ± 0%     29.9MB ± 0%    ~     (p=0.310 n=5+5)
GoTypes        114MB ± 0%      113MB ± 0%  -0.25%  (p=0.008 n=5+5)
SSA            856MB ± 0%      855MB ± 0%  -0.04%  (p=0.008 n=5+5)
Flate         25.5MB ± 0%     25.4MB ± 0%  -0.27%  (p=0.008 n=5+5)
GoParser      31.9MB ± 0%     31.9MB ± 0%    ~     (p=0.222 n=5+5)
Reflect       79.0MB ± 0%     78.6MB ± 0%  -0.45%  (p=0.008 n=5+5)
Tar           26.8MB ± 0%     26.7MB ± 0%  -0.25%  (p=0.032 n=5+5)
XML           42.4MB ± 0%     42.4MB ± 0%    ~     (p=0.151 n=5+5)

name       old allocs/op   new allocs/op   delta
Template        395k ± 0%       391k ± 0%  -1.00%  (p=0.008 n=5+5)
Unicode         321k ± 1%       319k ± 0%  -0.56%  (p=0.008 n=5+5)
GoTypes        1.16M ± 0%      1.14M ± 0%  -1.61%  (p=0.008 n=5+5)
SSA            7.63M ± 0%      7.60M ± 0%  -0.30%  (p=0.008 n=5+5)
Flate           239k ± 0%       234k ± 0%  -1.94%  (p=0.008 n=5+5)
GoParser        320k ± 0%       317k ± 1%  -0.86%  (p=0.008 n=5+5)
Reflect        1.00M ± 0%      0.98M ± 0%  -2.17%  (p=0.016 n=4+5)
Tar             255k ± 1%       251k ± 0%  -1.35%  (p=0.008 n=5+5)
XML             398k ± 0%       395k ± 0%  -0.89%  (p=0.008 n=5+5)

Updates #15756

Change-Id: Id23e647d347aa841f9a69d51f7d2d7d27b259239
Reviewed-on: https://go-review.googlesource.com/38797
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2017-03-29 16:28:51 -07:00
parent 18e77673d8
commit efc47819c0

View File

@ -719,7 +719,7 @@ var basicnames = []string{
TBLANK: "blank", TBLANK: "blank",
} }
func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string { func (t *Type) typefmt(flag FmtFlag, mode fmtMode, depth int) string {
if t == nil { if t == nil {
return "<T>" return "<T>"
} }
@ -770,7 +770,7 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
} }
if mode == FDbg { if mode == FDbg {
return t.Etype.String() + "-" + t.typefmt(flag, 0) return t.Etype.String() + "-" + t.typefmt(flag, 0, depth)
} }
switch t.Etype { switch t.Etype {
@ -778,36 +778,36 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
switch mode { switch mode {
case FTypeId, FTypeIdName: case FTypeId, FTypeIdName:
if flag&FmtShort != 0 { if flag&FmtShort != 0 {
return "*" + t.Elem().tconv(FmtShort, mode) return "*" + t.Elem().tconv(FmtShort, mode, depth)
} }
} }
return "*" + t.Elem().modeString(mode) return "*" + t.Elem().modeString(mode, depth)
case TARRAY: case TARRAY:
if t.isDDDArray() { if t.isDDDArray() {
return "[...]" + t.Elem().modeString(mode) return "[...]" + t.Elem().modeString(mode, depth)
} }
return mode.Sprintf("[%d]%v", t.NumElem(), t.Elem()) return "[" + strconv.FormatInt(t.NumElem(), 10) + "]" + t.Elem().modeString(mode, depth)
case TSLICE: case TSLICE:
return "[]" + t.Elem().modeString(mode) return "[]" + t.Elem().modeString(mode, depth)
case TCHAN: case TCHAN:
switch t.ChanDir() { switch t.ChanDir() {
case Crecv: case Crecv:
return "<-chan " + t.Elem().modeString(mode) return "<-chan " + t.Elem().modeString(mode, depth)
case Csend: case Csend:
return "chan<- " + t.Elem().modeString(mode) return "chan<- " + t.Elem().modeString(mode, depth)
} }
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv { if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv {
return "chan (" + t.Elem().modeString(mode) + ")" return "chan (" + t.Elem().modeString(mode, depth) + ")"
} }
return "chan " + t.Elem().modeString(mode) return "chan " + t.Elem().modeString(mode, depth)
case TMAP: case TMAP:
return "map[" + t.Key().modeString(mode) + "]" + t.Val().modeString(mode) return "map[" + t.Key().modeString(mode, depth) + "]" + t.Val().modeString(mode, depth)
case TINTER: case TINTER:
if t.IsEmptyInterface() { if t.IsEmptyInterface() {
@ -830,7 +830,7 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
default: default:
buf = append(buf, f.Sym.sconv(FmtUnsigned, mode)...) buf = append(buf, f.Sym.sconv(FmtUnsigned, mode)...)
} }
buf = append(buf, f.Type.tconv(FmtShort, mode)...) buf = append(buf, f.Type.tconv(FmtShort, mode, depth)...)
} }
if t.NumFields() != 0 { if t.NumFields() != 0 {
buf = append(buf, ' ') buf = append(buf, ' ')
@ -845,12 +845,12 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
} else { } else {
if t.Recv() != nil { if t.Recv() != nil {
buf = append(buf, "method"...) buf = append(buf, "method"...)
buf = append(buf, t.Recvs().modeString(mode)...) buf = append(buf, t.Recvs().modeString(mode, depth)...)
buf = append(buf, ' ') buf = append(buf, ' ')
} }
buf = append(buf, "func"...) buf = append(buf, "func"...)
} }
buf = append(buf, t.Params().modeString(mode)...) buf = append(buf, t.Params().modeString(mode, depth)...)
switch t.Results().NumFields() { switch t.Results().NumFields() {
case 0: case 0:
@ -858,11 +858,11 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
case 1: case 1:
buf = append(buf, ' ') buf = append(buf, ' ')
buf = append(buf, t.Results().Field(0).Type.modeString(mode)...) // struct->field->field's type buf = append(buf, t.Results().Field(0).Type.modeString(mode, depth)...) // struct->field->field's type
default: default:
buf = append(buf, ' ') buf = append(buf, ' ')
buf = append(buf, t.Results().modeString(mode)...) buf = append(buf, t.Results().modeString(mode, depth)...)
} }
return string(buf) return string(buf)
@ -872,15 +872,15 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
// Format the bucket struct for map[x]y as map.bucket[x]y. // Format the bucket struct for map[x]y as map.bucket[x]y.
// This avoids a recursive print that generates very long names. // This avoids a recursive print that generates very long names.
if mt.Bucket == t { if mt.Bucket == t {
return "map.bucket[" + m.Key().modeString(mode) + "]" + m.Val().modeString(mode) return "map.bucket[" + m.Key().modeString(mode, depth) + "]" + m.Val().modeString(mode, depth)
} }
if mt.Hmap == t { if mt.Hmap == t {
return "map.hdr[" + m.Key().modeString(mode) + "]" + m.Val().modeString(mode) return "map.hdr[" + m.Key().modeString(mode, depth) + "]" + m.Val().modeString(mode, depth)
} }
if mt.Hiter == t { if mt.Hiter == t {
return "map.iter[" + m.Key().modeString(mode) + "]" + m.Val().modeString(mode) return "map.iter[" + m.Key().modeString(mode, depth) + "]" + m.Val().modeString(mode, depth)
} }
Fatalf("unknown internal map type") Fatalf("unknown internal map type")
@ -899,7 +899,7 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
if i != 0 { if i != 0 {
buf = append(buf, ", "...) buf = append(buf, ", "...)
} }
buf = append(buf, fldconv(f, flag1, mode)...) buf = append(buf, fldconv(f, flag1, mode, depth)...)
} }
buf = append(buf, ')') buf = append(buf, ')')
} else { } else {
@ -909,7 +909,7 @@ func (t *Type) typefmt(flag FmtFlag, mode fmtMode) string {
buf = append(buf, ';') buf = append(buf, ';')
} }
buf = append(buf, ' ') buf = append(buf, ' ')
buf = append(buf, fldconv(f, FmtLong, mode)...) buf = append(buf, fldconv(f, FmtLong, mode, depth)...)
} }
if t.NumFields() != 0 { if t.NumFields() != 0 {
buf = append(buf, ' ') buf = append(buf, ' ')
@ -1748,24 +1748,33 @@ func (s *Sym) sconv(flag FmtFlag, mode fmtMode) string {
return s.symfmt(flag, mode) return s.symfmt(flag, mode)
} }
func (t *Type) String() string { return t.tconv(0, FErr) } func (t *Type) String() string {
func (t *Type) modeString(mode fmtMode) string { return t.tconv(0, mode) } // This is an external entry point, so we pass depth 0 to tconv.
// The implementation of tconv (including typefmt and fldconv)
// must take care not to use a type in a formatting string
// to avoid resetting the recursion counter.
return t.tconv(0, FErr, 0)
}
func (t *Type) modeString(mode fmtMode, depth int) string {
return t.tconv(0, mode, depth)
}
// ShortString generates a short description of t. // ShortString generates a short description of t.
// It is used in autogenerated method names, reflection, // It is used in autogenerated method names, reflection,
// and itab names. // and itab names.
func (t *Type) ShortString() string { func (t *Type) ShortString() string {
return t.tconv(FmtLeft, FErr) return t.tconv(FmtLeft, FErr, 0)
} }
// LongString generates a complete description of t. // LongString generates a complete description of t.
// It is useful for reflection, // It is useful for reflection,
// or when a unique fingerprint or hash of a type is required. // or when a unique fingerprint or hash of a type is required.
func (t *Type) LongString() string { func (t *Type) LongString() string {
return t.tconv(FmtLeft|FmtUnsigned, FErr) return t.tconv(FmtLeft|FmtUnsigned, FErr, 0)
} }
func fldconv(f *Field, flag FmtFlag, mode fmtMode) string { func fldconv(f *Field, flag FmtFlag, mode fmtMode, depth int) string {
if f == nil { if f == nil {
return "<T>" return "<T>"
} }
@ -1812,9 +1821,9 @@ func fldconv(f *Field, flag FmtFlag, mode fmtMode) string {
var typ string var typ string
if f.Isddd() { if f.Isddd() {
typ = mode.Sprintf("...%v", f.Type.Elem()) typ = "..." + f.Type.Elem().modeString(mode, depth)
} else { } else {
typ = mode.Sprintf("%v", f.Type) typ = f.Type.modeString(mode, depth)
} }
str := typ str := typ
@ -1834,7 +1843,9 @@ func fldconv(f *Field, flag FmtFlag, mode fmtMode) string {
func (t *Type) format(s fmt.State, verb rune, mode fmtMode) { func (t *Type) format(s fmt.State, verb rune, mode fmtMode) {
switch verb { switch verb {
case 'v', 'S', 'L': case 'v', 'S', 'L':
fmt.Fprint(s, t.tconv(fmtFlag(s, verb), mode)) // This is an external entry point, so we pass depth 0 to tconv.
// See comments in Type.String.
fmt.Fprint(s, t.tconv(fmtFlag(s, verb), mode, 0))
default: default:
fmt.Fprintf(s, "%%!%c(*Type=%p)", verb, t) fmt.Fprintf(s, "%%!%c(*Type=%p)", verb, t)
@ -1842,24 +1853,22 @@ func (t *Type) format(s fmt.State, verb rune, mode fmtMode) {
} }
// See #16897 before changing the implementation of tconv. // See #16897 before changing the implementation of tconv.
func (t *Type) tconv(flag FmtFlag, mode fmtMode) string { func (t *Type) tconv(flag FmtFlag, mode fmtMode, depth int) string {
if t == nil { if t == nil {
return "<T>" return "<T>"
} }
if t.Trecur > 4 { if depth > 100 {
return "<...>" return "<...>"
} }
t.Trecur++
flag, mode = flag.update(mode) flag, mode = flag.update(mode)
if mode == FTypeIdName { if mode == FTypeIdName {
flag |= FmtUnsigned flag |= FmtUnsigned
} }
str := t.typefmt(flag, mode) str := t.typefmt(flag, mode, depth+1)
t.Trecur--
return str return str
} }