]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: remove uncommonType.name
authorDavid Crawshaw <crawshaw@golang.org>
Wed, 17 Feb 2016 18:03:21 +0000 (13:03 -0500)
committerDavid Crawshaw <crawshaw@golang.org>
Fri, 26 Feb 2016 12:02:39 +0000 (12:02 +0000)
Reduces binary size of cmd/go by 0.5%.
For #6853.

Change-Id: I5a4b814049580ab5098ad252d979f80b70d8a5f9
Reviewed-on: https://go-review.googlesource.com/19694
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/reflect.go
src/reflect/all_test.go
src/reflect/type.go
src/runtime/heapdump.go
src/runtime/mfinal.go
src/runtime/type.go

index 4fdbbd6967fe7cadbcac1234bb47559193b46abc..b3f6b6ac55eade4bff2c2cc91daa31f07f4d795b 100644 (file)
@@ -492,16 +492,10 @@ func dextratype(sym *Sym, off int, t *Type, ptroff int) int {
 
        ot := off
        s := sym
-       if t.Sym != nil {
-               ot = dgostringptr(s, ot, t.Sym.Name)
-               if t != Types[t.Etype] && t != errortype {
-                       ot = dgopkgpath(s, ot, t.Sym.Pkg)
-               } else {
-                       ot = dgostringptr(s, ot, "")
-               }
+       if t.Sym != nil && t != Types[t.Etype] && t != errortype {
+               ot = dgopkgpath(s, ot, t.Sym.Pkg)
        } else {
                ot = dgostringptr(s, ot, "")
-               ot = dgostringptr(s, ot, "")
        }
 
        // slice header
index 58090cde70d3fb3b810255e1c6ad192ca313f32c..2eb4f343f9808ea63ce4f9f9271b322fb40e9a77 100644 (file)
@@ -5007,3 +5007,24 @@ func TestChanAlloc(t *testing.T) {
        // a limitation of escape analysis.  If that is ever fixed the
        // allocs < 0.5 condition will trigger and this test should be fixed.
 }
+
+type nameTest struct {
+       v    interface{}
+       want string
+}
+
+var nameTests = []nameTest{
+       {int32(0), "int32"},
+       {D1{}, "D1"},
+       {[]D1{}, ""},
+       {(chan D1)(nil), ""},
+       {(func() D1)(nil), ""},
+}
+
+func TestNames(t *testing.T) {
+       for _, test := range nameTests {
+               if got := TypeOf(test.v).Name(); got != test.want {
+                       t.Errorf("%T Name()=%q, want %q", test.v, got, test.want)
+               }
+       }
+}
index 2c7c1fb30bba5c3dcd8f94448fdf5952fc0ebbd0..1367ba34e7fb130bc96b267001dcfef0cd591455 100644 (file)
@@ -283,7 +283,6 @@ type method struct {
 // Using a pointer to this struct reduces the overall size required
 // to describe an unnamed type with no methods.
 type uncommonType struct {
-       name    *string  // name of type
        pkgPath *string  // import path; nil for built-in types like int, string
        methods []method // methods associated with type
 }
@@ -452,13 +451,6 @@ func (t *uncommonType) PkgPath() string {
        return *t.pkgPath
 }
 
-func (t *uncommonType) Name() string {
-       if t == nil || t.name == nil {
-               return ""
-       }
-       return *t.name
-}
-
 func (t *rtype) String() string { return t.string }
 
 func (t *rtype) Size() uintptr { return t.size }
@@ -557,8 +549,34 @@ func (t *rtype) PkgPath() string {
        return t.uncommonType.PkgPath()
 }
 
+func hasPrefix(s, prefix string) bool {
+       return len(s) >= len(prefix) && s[:len(prefix)] == prefix
+}
+
 func (t *rtype) Name() string {
-       return t.uncommonType.Name()
+       if hasPrefix(t.string, "map[") {
+               return ""
+       }
+       if hasPrefix(t.string, "struct {") {
+               return ""
+       }
+       if hasPrefix(t.string, "chan ") {
+               return ""
+       }
+       if hasPrefix(t.string, "func(") {
+               return ""
+       }
+       if t.string[0] == '[' || t.string[0] == '*' {
+               return ""
+       }
+       i := len(t.string) - 1
+       for i >= 0 {
+               if t.string[i] == '.' {
+                       break
+               }
+               i--
+       }
+       return t.string[i+1:]
 }
 
 func (t *rtype) ChanDir() ChanDir {
index 1e1157d0548b634b6c17994a39be8f8fb05d18b5..3bff36bd78788ddf511572c18908d5879b8a9a46 100644 (file)
@@ -183,11 +183,12 @@ func dumptype(t *_type) {
        dumpint(tagType)
        dumpint(uint64(uintptr(unsafe.Pointer(t))))
        dumpint(uint64(t.size))
-       if t.x == nil || t.x.pkgpath == nil || t.x.name == nil {
+       if t.x == nil || t.x.pkgpath == nil {
                dumpstr(t._string)
        } else {
                pkgpath := stringStructOf(t.x.pkgpath)
-               name := stringStructOf(t.x.name)
+               namestr := t.name()
+               name := stringStructOf(&namestr)
                dumpint(uint64(uintptr(pkgpath.len) + 1 + uintptr(name.len)))
                dwrite(pkgpath.str, uintptr(pkgpath.len))
                dwritebyte('.')
index 778cbcb416e04f68e36e11c06d7cc0d9f90fcf27..7d458625217a47333f2456b2266999e7d4946db6 100644 (file)
@@ -340,7 +340,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {
                // ok - same type
                goto okarg
        case fint.kind&kindMask == kindPtr:
-               if (fint.x == nil || fint.x.name == nil || etyp.x == nil || etyp.x.name == nil) && (*ptrtype)(unsafe.Pointer(fint)).elem == ot.elem {
+               if (fint.x == nil || etyp.x == nil) && (*ptrtype)(unsafe.Pointer(fint)).elem == ot.elem {
                        // ok - not same type, but both pointers,
                        // one or the other is unnamed, and same element type, so assignable.
                        goto okarg
index 8304ad7b7731671449309541434800ef9d3d48a5..18c6a32ecba710cbbbbb8b53bc9f52adc39a6a1b 100644 (file)
@@ -28,6 +28,36 @@ type _type struct {
        x       *uncommontype
 }
 
+func hasPrefix(s, prefix string) bool {
+       return len(s) >= len(prefix) && s[:len(prefix)] == prefix
+}
+
+func (t *_type) name() string {
+       if hasPrefix(t._string, "map[") {
+               return ""
+       }
+       if hasPrefix(t._string, "struct {") {
+               return ""
+       }
+       if hasPrefix(t._string, "chan ") {
+               return ""
+       }
+       if hasPrefix(t._string, "func(") {
+               return ""
+       }
+       if t._string[0] == '[' || t._string[0] == '*' {
+               return ""
+       }
+       i := len(t._string) - 1
+       for i >= 0 {
+               if t._string[i] == '.' {
+                       break
+               }
+               i--
+       }
+       return t._string[i+1:]
+}
+
 type method struct {
        name    *string
        pkgpath *string
@@ -38,7 +68,6 @@ type method struct {
 }
 
 type uncommontype struct {
-       name    *string
        pkgpath *string
        mhdr    []method
 }