aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/iface.go
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2016-10-28 11:37:45 -0700
committerKeith Randall <khr@golang.org>2016-11-02 21:33:03 +0000
commit688995d1e91fcc36ac33bf34d6303e935d1b0a70 (patch)
tree55ea9a066877ee5a8e80b5ea3f80135f75d6d6b2 /src/runtime/iface.go
parent761443edd56832cc1b62f9193f157ca822dfa09e (diff)
downloadgo-688995d1e91fcc36ac33bf34d6303e935d1b0a70.tar.gz
go-688995d1e91fcc36ac33bf34d6303e935d1b0a70.zip
cmd/compile: do more type conversion inline
The code to do the conversion is smaller than the call to the runtime. The 1-result asserts need to call panic if they fail, but that code is out of line. The only conversions left in the runtime are those which might allocate and those which might need to generate an itab. Given the following types: type E interface{} type I interface { foo() } type I2 iterface { foo(); bar() } type Big [10]int func (b Big) foo() { ... } This CL inlines the following conversions: was assertE2T var e E = ... b := i.(Big) was assertE2T2 var e E = ... b, ok := i.(Big) was assertI2T var i I = ... b := i.(Big) was assertI2T2 var i I = ... b, ok := i.(Big) was assertI2E var i I = ... e := i.(E) was assertI2E2 var i I = ... e, ok := i.(E) These are the remaining runtime calls: convT2E: var b Big = ... var e E = b convT2I: var b Big = ... var i I = b convI2I: var i2 I2 = ... var i I = i2 assertE2I: var e E = ... i := e.(I) assertE2I2: var e E = ... i, ok := e.(I) assertI2I: var i I = ... i2 := i.(I2) assertI2I2: var i I = ... i2, ok := i.(I2) Fixes #17405 Fixes #8422 Change-Id: Ida2367bf8ce3cd2c6bb599a1814f1d275afabe21 Reviewed-on: https://go-review.googlesource.com/32313 Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/runtime/iface.go')
-rw-r--r--src/runtime/iface.go200
1 files changed, 49 insertions, 151 deletions
diff --git a/src/runtime/iface.go b/src/runtime/iface.go
index 26e2956eea..c932e149dd 100644
--- a/src/runtime/iface.go
+++ b/src/runtime/iface.go
@@ -156,6 +156,34 @@ func itabsinit() {
unlock(&ifaceLock)
}
+// panicdottype is called when doing an i.(T) conversion and the conversion fails.
+// have = the dynamic type we have.
+// want = the static type we're trying to convert to.
+// iface = the static type we're converting from.
+func panicdottype(have, want, iface *_type) {
+ haveString := ""
+ if have != nil {
+ haveString = have.string()
+ }
+ panic(&TypeAssertionError{iface.string(), haveString, want.string(), ""})
+}
+
+// panicnildottype is called when doing a i.(T) conversion and the interface i is nil.
+// want = the static type we're trying to convert to.
+func panicnildottype(want *_type) {
+ panic(&TypeAssertionError{"", "", want.string(), ""})
+ // TODO: Add the static type we're converting from as well.
+ // It might generate a better error message.
+ // Just to match other nil conversion errors, we don't for now.
+}
+
+// The conv and assert functions below do very similar things.
+// The convXXX functions are guaranteed by the compiler to succeed.
+// The assertXXX functions may fail (either panicing or returning false,
+// depending on whether they are 1-result or 2-result).
+// The convXXX functions succeed on a nil input, whereas the assertXXX
+// functions fail on a nil input.
+
func convT2E(t *_type, elem unsafe.Pointer) (e eface) {
if raceenabled {
raceReadObjectPC(t, elem, getcallerpc(unsafe.Pointer(&t)), funcPC(convT2E))
@@ -164,6 +192,7 @@ func convT2E(t *_type, elem unsafe.Pointer) (e eface) {
msanread(elem, t.size)
}
if isDirectIface(t) {
+ // This case is implemented directly by the compiler.
throw("direct convT2E")
}
x := newobject(t)
@@ -184,6 +213,7 @@ func convT2I(tab *itab, elem unsafe.Pointer) (i iface) {
msanread(elem, t.size)
}
if isDirectIface(t) {
+ // This case is implemented directly by the compiler.
throw("direct convT2I")
}
x := newobject(t)
@@ -193,103 +223,6 @@ func convT2I(tab *itab, elem unsafe.Pointer) (i iface) {
return
}
-func panicdottype(have, want, iface *_type) {
- haveString := ""
- if have != nil {
- haveString = have.string()
- }
- panic(&TypeAssertionError{iface.string(), haveString, want.string(), ""})
-}
-
-func assertI2T(t *_type, i iface, r unsafe.Pointer) {
- tab := i.tab
- if tab == nil {
- panic(&TypeAssertionError{"", "", t.string(), ""})
- }
- if tab._type != t {
- panic(&TypeAssertionError{tab.inter.typ.string(), tab._type.string(), t.string(), ""})
- }
- if r != nil {
- if isDirectIface(t) {
- writebarrierptr((*uintptr)(r), uintptr(i.data))
- } else {
- typedmemmove(t, r, i.data)
- }
- }
-}
-
-// The compiler ensures that r is non-nil.
-func assertI2T2(t *_type, i iface, r unsafe.Pointer) bool {
- tab := i.tab
- if tab == nil || tab._type != t {
- typedmemclr(t, r)
- return false
- }
- if isDirectIface(t) {
- writebarrierptr((*uintptr)(r), uintptr(i.data))
- } else {
- typedmemmove(t, r, i.data)
- }
- return true
-}
-
-func assertE2T(t *_type, e eface, r unsafe.Pointer) {
- if e._type == nil {
- panic(&TypeAssertionError{"", "", t.string(), ""})
- }
- if e._type != t {
- panic(&TypeAssertionError{"", e._type.string(), t.string(), ""})
- }
- if r != nil {
- if isDirectIface(t) {
- writebarrierptr((*uintptr)(r), uintptr(e.data))
- } else {
- typedmemmove(t, r, e.data)
- }
- }
-}
-
-var testingAssertE2T2GC bool
-
-// The compiler ensures that r is non-nil.
-func assertE2T2(t *_type, e eface, r unsafe.Pointer) bool {
- if testingAssertE2T2GC {
- GC()
- }
- if e._type != t {
- typedmemclr(t, r)
- return false
- }
- if isDirectIface(t) {
- writebarrierptr((*uintptr)(r), uintptr(e.data))
- } else {
- typedmemmove(t, r, e.data)
- }
- return true
-}
-
-func assertI2E(inter *interfacetype, i iface, r *eface) {
- tab := i.tab
- if tab == nil {
- // explicit conversions require non-nil interface value.
- panic(&TypeAssertionError{"", "", inter.typ.string(), ""})
- }
- r._type = tab._type
- r.data = i.data
- return
-}
-
-// The compiler ensures that r is non-nil.
-func assertI2E2(inter *interfacetype, i iface, r *eface) bool {
- tab := i.tab
- if tab == nil {
- return false
- }
- r._type = tab._type
- r.data = i.data
- return true
-}
-
func convI2I(inter *interfacetype, i iface) (r iface) {
tab := i.tab
if tab == nil {
@@ -305,7 +238,7 @@ func convI2I(inter *interfacetype, i iface) (r iface) {
return
}
-func assertI2I(inter *interfacetype, i iface, r *iface) {
+func assertI2I(inter *interfacetype, i iface) (r iface) {
tab := i.tab
if tab == nil {
// explicit conversions require non-nil interface value.
@@ -318,33 +251,27 @@ func assertI2I(inter *interfacetype, i iface, r *iface) {
}
r.tab = getitab(inter, tab._type, false)
r.data = i.data
+ return
}
-func assertI2I2(inter *interfacetype, i iface, r *iface) bool {
+func assertI2I2(inter *interfacetype, i iface) (r iface, b bool) {
tab := i.tab
if tab == nil {
- if r != nil {
- *r = iface{}
- }
- return false
+ return
}
if tab.inter != inter {
tab = getitab(inter, tab._type, true)
if tab == nil {
- if r != nil {
- *r = iface{}
- }
- return false
+ return
}
}
- if r != nil {
- r.tab = tab
- r.data = i.data
- }
- return true
+ r.tab = tab
+ r.data = i.data
+ b = true
+ return
}
-func assertE2I(inter *interfacetype, e eface, r *iface) {
+func assertE2I(inter *interfacetype, e eface) (r iface) {
t := e._type
if t == nil {
// explicit conversions require non-nil interface value.
@@ -352,56 +279,27 @@ func assertE2I(inter *interfacetype, e eface, r *iface) {
}
r.tab = getitab(inter, t, false)
r.data = e.data
+ return
}
-var testingAssertE2I2GC bool
-
-func assertE2I2(inter *interfacetype, e eface, r *iface) bool {
- if testingAssertE2I2GC {
- GC()
- }
+func assertE2I2(inter *interfacetype, e eface) (r iface, b bool) {
t := e._type
if t == nil {
- if r != nil {
- *r = iface{}
- }
- return false
+ return
}
tab := getitab(inter, t, true)
if tab == nil {
- if r != nil {
- *r = iface{}
- }
- return false
- }
- if r != nil {
- r.tab = tab
- r.data = e.data
+ return
}
- return true
+ r.tab = tab
+ r.data = e.data
+ b = true
+ return
}
//go:linkname reflect_ifaceE2I reflect.ifaceE2I
func reflect_ifaceE2I(inter *interfacetype, e eface, dst *iface) {
- assertE2I(inter, e, dst)
-}
-
-func assertE2E(inter *interfacetype, e eface, r *eface) {
- if e._type == nil {
- // explicit conversions require non-nil interface value.
- panic(&TypeAssertionError{"", "", inter.typ.string(), ""})
- }
- *r = e
-}
-
-// The compiler ensures that r is non-nil.
-func assertE2E2(inter *interfacetype, e eface, r *eface) bool {
- if e._type == nil {
- *r = eface{}
- return false
- }
- *r = e
- return true
+ *dst = assertE2I(inter, e)
}
func iterate_itabs(fn func(*itab)) {