Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jul 5, 2020
1 parent 155fcfc commit 8bccafe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
6 changes: 4 additions & 2 deletions bechmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func kindFromGoReflect(v interface{}) goreflect.Kind {
func f(_ interface{}) {}

func valueFromReflect(v interface{}) {
f(reflect.ValueOf(v))
f(reflect.ValueOf(v).Elem())
}

func valueFromGoReflect(v interface{}) {
f(goreflect.ValueNoEscapeOf(v))
f(goreflect.ValueNoEscapeOf(v).Elem())
}

func Benchmark_TypeOf_Reflect(b *testing.B) {
Expand Down Expand Up @@ -50,6 +50,7 @@ func Benchmark_ValueOf_Reflect(b *testing.B) {
for n := 0; n < b.N; n++ {
valueFromReflect(&struct {
I int
F float64
}{I: 10})
}
}
Expand All @@ -59,6 +60,7 @@ func Benchmark_ValueOf_GoReflect(b *testing.B) {
for n := 0; n < b.N; n++ {
valueFromGoReflect(&struct {
I int
F float64
}{I: 10})
}
}
26 changes: 19 additions & 7 deletions bridge.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package reflect

import "reflect"

var (
toRT = ToReflectType
toT = ToType
toRV = ToReflectValue
toV = ToValue
import (
"reflect"
"unsafe"
)

func toRT(t *Type) reflect.Type {
return type_toType(t)
}

func toRTs(t []*Type) []reflect.Type {
out := make([]reflect.Type, len(t))
for idx, tt := range t {
Expand All @@ -17,6 +17,10 @@ func toRTs(t []*Type) []reflect.Type {
return out
}

func toT(t reflect.Type) *Type {
return (*Type)(((*Value)(unsafe.Pointer(&t))).ptr)
}

func toTs(v []reflect.Value) []Value {
out := make([]Value, len(v))
for idx, vv := range v {
Expand All @@ -25,6 +29,10 @@ func toTs(v []reflect.Value) []Value {
return out
}

func toRV(v Value) reflect.Value {
return *(*reflect.Value)(unsafe.Pointer(&v))
}

func toRVs(v []Value) []reflect.Value {
out := make([]reflect.Value, len(v))
for idx, vv := range v {
Expand All @@ -33,6 +41,10 @@ func toRVs(v []Value) []reflect.Value {
return out
}

func toV(v reflect.Value) Value {
return *(*Value)(unsafe.Pointer(&v))
}

func toVs(v []reflect.Value) []Value {
out := make([]Value, len(v))
for idx, vv := range v {
Expand Down
8 changes: 4 additions & 4 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,22 @@ func ValueNoEscapeOf(v interface{}) Value {

// ToReflectType convert *Type to reflect.Type
func ToReflectType(t *Type) reflect.Type {
return type_toType(t)
return toRT(t)
}

// ToReflectValue convert Value to reflect.Value
func ToReflectValue(v Value) reflect.Value {
return *(*reflect.Value)(unsafe.Pointer(&v))
return toRV(v)
}

// ToType convert reflect.Type to *Type
func ToType(t reflect.Type) *Type {
return (*Type)(((*Value)(unsafe.Pointer(&t))).ptr)
return toT(t)
}

// ToValue convert reflect.Value to Value
func ToValue(v reflect.Value) Value {
return *(*Value)(unsafe.Pointer(&v))
return toV(v)
}

// Copy copies the contents of src into dst until either
Expand Down
2 changes: 1 addition & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func value_Convert(v Value, typ *Type) Value {
}

func value_Elem(v Value) Value {
return ToValue(toRV(v).Elem())
return toV(toRV(v).Elem())
}

func value_Field(v Value, i int) Value {
Expand Down

0 comments on commit 8bccafe

Please sign in to comment.