aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/type.go
diff options
context:
space:
mode:
authorDavid Crawshaw <crawshaw@golang.org>2016-03-31 10:02:10 -0400
committerDavid Crawshaw <crawshaw@golang.org>2016-04-13 20:48:26 +0000
commitf120936dfffa3ac935730699587e6957f2d5ea61 (patch)
treec7415fc534e0ef9bbf4ac4afb1a562b6255adadc /src/runtime/type.go
parent73e2ad20220050f88b1ea79bf5a2e4c4fbee0533 (diff)
downloadgo-f120936dfffa3ac935730699587e6957f2d5ea61.tar.xz
cmd/compile, etc: use name for type pkgPath
By replacing the *string used to represent pkgPath with a reflect.name everywhere, the embedded *string for package paths inside the reflect.name can be replaced by an offset, nameOff. This reduces the number of pointers in the type information. This also moves all reflect.name types into the same section, making it possible to use nameOff more widely in later CLs. No significant binary size change for normal binaries, but: linux/amd64 PIE: cmd/go: -440KB (3.7%) jujud: -2.6MB (3.2%) For #6853. Change-Id: I3890b132a784a1090b1b72b32febfe0bea77eaee Reviewed-on: https://go-review.googlesource.com/21395 Run-TryBot: David Crawshaw <crawshaw@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/type.go')
-rw-r--r--src/runtime/type.go72
1 files changed, 46 insertions, 26 deletions
diff --git a/src/runtime/type.go b/src/runtime/type.go
index 86131d3ff3..711753bab5 100644
--- a/src/runtime/type.go
+++ b/src/runtime/type.go
@@ -6,10 +6,7 @@
package runtime
-import (
- "runtime/internal/sys"
- "unsafe"
-)
+import "unsafe"
// tflag is documented in ../reflect/type.go.
type tflag uint8
@@ -151,6 +148,33 @@ var reflectOffs struct {
minv map[unsafe.Pointer]int32
}
+func resolveNameOff(ptrInModule unsafe.Pointer, off nameOff) name {
+ if off == 0 {
+ return name{}
+ }
+ base := uintptr(ptrInModule)
+ var md *moduledata
+ for next := &firstmoduledata; next != nil; next = next.next {
+ if base >= next.types && base < next.etypes {
+ md = next
+ break
+ }
+ }
+ if md == nil {
+ println("runtime: nameOff", hex(off), "base", hex(base), "not in ranges:")
+ for next := &firstmoduledata; next != nil; next = next.next {
+ println("\ttypes", hex(next.types), "etypes", hex(next.etypes))
+ }
+ throw("runtime: name offset base pointer out of range")
+ }
+ res := md.types + uintptr(off)
+ if res > md.etypes {
+ println("runtime: nameOff", hex(off), "out of range", hex(md.types), "-", hex(md.etypes))
+ throw("runtime: name offset out of range")
+ }
+ return name{(*byte)(unsafe.Pointer(res))}
+}
+
func (t *_type) typeOff(off typeOff) *_type {
if off == 0 {
return nil
@@ -240,6 +264,7 @@ func (t *functype) dotdotdot() bool {
return t.outCount&(1<<15) != 0
}
+type nameOff int32
type typeOff int32
type textOff int32
@@ -251,7 +276,7 @@ type method struct {
}
type uncommontype struct {
- pkgpath *string
+ pkgpath name
mcount uint16 // number of methods
moff uint16 // offset from this uncommontype to [mcount]method
}
@@ -263,7 +288,7 @@ type imethod struct {
type interfacetype struct {
typ _type
- pkgpath *string
+ pkgpath name
mhdr []imethod
}
@@ -319,7 +344,7 @@ type structfield struct {
type structtype struct {
typ _type
- pkgPath *string
+ pkgPath name
fields []structfield
}
@@ -350,6 +375,9 @@ func (n *name) tagLen() int {
}
func (n *name) name() (s string) {
+ if n.bytes == nil {
+ return ""
+ }
nl := n.nameLen()
if nl == 0 {
return ""
@@ -372,16 +400,18 @@ func (n *name) tag() (s string) {
return s
}
-func (n *name) pkgPath() *string {
- if *n.data(0)&(1<<2) == 0 {
- return nil
+func (n *name) pkgPath() string {
+ if n.bytes == nil || *n.data(0)&(1<<2) == 0 {
+ return ""
}
off := 3 + n.nameLen()
if tl := n.tagLen(); tl > 0 {
off += 2 + tl
}
- off = int(round(uintptr(off), sys.PtrSize))
- return *(**string)(unsafe.Pointer(n.data(off)))
+ var nameOff nameOff
+ copy((*[4]byte)(unsafe.Pointer(&nameOff))[:], (*[4]byte)(unsafe.Pointer(n.data(off)))[:])
+ pkgPathName := resolveNameOff(unsafe.Pointer(n.bytes), nameOff)
+ return pkgPathName.name()
}
// typelinksinit scans the types from extra modules and builds the
@@ -466,7 +496,7 @@ func typesEqual(t, v *_type) bool {
if ut == nil || uv == nil {
return false
}
- if !pkgPathEqual(ut.pkgpath, uv.pkgpath) {
+ if ut.pkgpath.name() != uv.pkgpath.name() {
return false
}
}
@@ -506,7 +536,7 @@ func typesEqual(t, v *_type) bool {
case kindInterface:
it := (*interfacetype)(unsafe.Pointer(t))
iv := (*interfacetype)(unsafe.Pointer(v))
- if !pkgPathEqual(it.pkgpath, iv.pkgpath) {
+ if it.pkgpath.name() != iv.pkgpath.name() {
return false
}
if len(it.mhdr) != len(iv.mhdr) {
@@ -518,7 +548,7 @@ func typesEqual(t, v *_type) bool {
if tm.name.name() != vm.name.name() {
return false
}
- if !pkgPathEqual(tm.name.pkgPath(), vm.name.pkgPath()) {
+ if tm.name.pkgPath() != vm.name.pkgPath() {
return false
}
if !typesEqual(tm._type, vm._type) {
@@ -550,7 +580,7 @@ func typesEqual(t, v *_type) bool {
if tf.name.name() != vf.name.name() {
return false
}
- if !pkgPathEqual(tf.name.pkgPath(), vf.name.pkgPath()) {
+ if tf.name.pkgPath() != vf.name.pkgPath() {
return false
}
if !typesEqual(tf.typ, vf.typ) {
@@ -570,13 +600,3 @@ func typesEqual(t, v *_type) bool {
return false
}
}
-
-func pkgPathEqual(p, q *string) bool {
- if p == q {
- return true
- }
- if p == nil || q == nil {
- return false
- }
- return *p == *q
-}