Skip to content

Commit 99a6183

Browse files
ydnardeadprogram
authored andcommitted
runtime: use package reflectlite
1 parent 9e143ef commit 99a6183

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/runtime/hashmap.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package runtime
66
// https://golang.org/src/runtime/map.go
77

88
import (
9-
"reflect"
9+
"internal/reflectlite"
1010
"tinygo"
1111
"unsafe"
1212
)
@@ -539,8 +539,8 @@ func hashmapStringDelete(m *hashmap, key string) {
539539
// a field is exported and thus allows circumventing the type system.
540540
// The hash function needs it as it also needs to hash unexported struct fields.
541541
//
542-
//go:linkname valueInterfaceUnsafe reflect.valueInterfaceUnsafe
543-
func valueInterfaceUnsafe(v reflect.Value) interface{}
542+
//go:linkname valueInterfaceUnsafe internal/reflectlite.valueInterfaceUnsafe
543+
func valueInterfaceUnsafe(v reflectlite.Value) interface{}
544544

545545
func hashmapFloat32Hash(ptr unsafe.Pointer, seed uintptr) uint32 {
546546
f := *(*uint32)(ptr)
@@ -561,7 +561,7 @@ func hashmapFloat64Hash(ptr unsafe.Pointer, seed uintptr) uint32 {
561561
}
562562

563563
func hashmapInterfaceHash(itf interface{}, seed uintptr) uint32 {
564-
x := reflect.ValueOf(itf)
564+
x := reflectlite.ValueOf(itf)
565565
if x.RawType() == nil {
566566
return 0 // nil interface
567567
}
@@ -574,39 +574,39 @@ func hashmapInterfaceHash(itf interface{}, seed uintptr) uint32 {
574574
}
575575

576576
switch x.RawType().Kind() {
577-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
577+
case reflectlite.Int, reflectlite.Int8, reflectlite.Int16, reflectlite.Int32, reflectlite.Int64:
578578
return hash32(ptr, x.RawType().Size(), seed)
579-
case reflect.Bool, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
579+
case reflectlite.Bool, reflectlite.Uint, reflectlite.Uint8, reflectlite.Uint16, reflectlite.Uint32, reflectlite.Uint64, reflectlite.Uintptr:
580580
return hash32(ptr, x.RawType().Size(), seed)
581-
case reflect.Float32:
581+
case reflectlite.Float32:
582582
// It should be possible to just has the contents. However, NaN != NaN
583583
// so if you're using lots of NaNs as map keys (you shouldn't) then hash
584584
// time may become exponential. To fix that, it would be better to
585585
// return a random number instead:
586586
// https://research.swtch.com/randhash
587587
return hashmapFloat32Hash(ptr, seed)
588-
case reflect.Float64:
588+
case reflectlite.Float64:
589589
return hashmapFloat64Hash(ptr, seed)
590-
case reflect.Complex64:
590+
case reflectlite.Complex64:
591591
rptr, iptr := ptr, unsafe.Add(ptr, 4)
592592
return hashmapFloat32Hash(rptr, seed) ^ hashmapFloat32Hash(iptr, seed)
593-
case reflect.Complex128:
593+
case reflectlite.Complex128:
594594
rptr, iptr := ptr, unsafe.Add(ptr, 8)
595595
return hashmapFloat64Hash(rptr, seed) ^ hashmapFloat64Hash(iptr, seed)
596-
case reflect.String:
596+
case reflectlite.String:
597597
return hashmapStringHash(x.String(), seed)
598-
case reflect.Chan, reflect.Ptr, reflect.UnsafePointer:
598+
case reflectlite.Chan, reflectlite.Ptr, reflectlite.UnsafePointer:
599599
// It might seem better to just return the pointer, but that won't
600600
// result in an evenly distributed hashmap. Instead, hash the pointer
601601
// like most other types.
602602
return hash32(ptr, x.RawType().Size(), seed)
603-
case reflect.Array:
603+
case reflectlite.Array:
604604
var hash uint32
605605
for i := 0; i < x.Len(); i++ {
606606
hash ^= hashmapInterfaceHash(valueInterfaceUnsafe(x.Index(i)), seed)
607607
}
608608
return hash
609-
case reflect.Struct:
609+
case reflectlite.Struct:
610610
var hash uint32
611611
for i := 0; i < x.NumField(); i++ {
612612
hash ^= hashmapInterfaceHash(valueInterfaceUnsafe(x.Field(i)), seed)

src/runtime/interface.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package runtime
66
// anything (including non-pointers).
77

88
import (
9-
"reflect"
9+
"internal/reflectlite"
1010
"unsafe"
1111
)
1212

@@ -27,12 +27,12 @@ func decomposeInterface(i _interface) (unsafe.Pointer, unsafe.Pointer) {
2727

2828
// Return true iff both interfaces are equal.
2929
func interfaceEqual(x, y interface{}) bool {
30-
return reflectValueEqual(reflect.ValueOf(x), reflect.ValueOf(y))
30+
return reflectValueEqual(reflectlite.ValueOf(x), reflectlite.ValueOf(y))
3131
}
3232

33-
func reflectValueEqual(x, y reflect.Value) bool {
33+
func reflectValueEqual(x, y reflectlite.Value) bool {
3434
// Note: doing a x.Type() == y.Type() comparison would not work here as that
35-
// would introduce an infinite recursion: comparing two reflect.Type values
35+
// would introduce an infinite recursion: comparing two reflectlite.Type values
3636
// is done with this reflectValueEqual runtime call.
3737
if x.RawType() == nil || y.RawType() == nil {
3838
// One of them is nil.
@@ -46,35 +46,35 @@ func reflectValueEqual(x, y reflect.Value) bool {
4646
}
4747

4848
switch x.RawType().Kind() {
49-
case reflect.Bool:
49+
case reflectlite.Bool:
5050
return x.Bool() == y.Bool()
51-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
51+
case reflectlite.Int, reflectlite.Int8, reflectlite.Int16, reflectlite.Int32, reflectlite.Int64:
5252
return x.Int() == y.Int()
53-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
53+
case reflectlite.Uint, reflectlite.Uint8, reflectlite.Uint16, reflectlite.Uint32, reflectlite.Uint64, reflectlite.Uintptr:
5454
return x.Uint() == y.Uint()
55-
case reflect.Float32, reflect.Float64:
55+
case reflectlite.Float32, reflectlite.Float64:
5656
return x.Float() == y.Float()
57-
case reflect.Complex64, reflect.Complex128:
57+
case reflectlite.Complex64, reflectlite.Complex128:
5858
return x.Complex() == y.Complex()
59-
case reflect.String:
59+
case reflectlite.String:
6060
return x.String() == y.String()
61-
case reflect.Chan, reflect.Ptr, reflect.UnsafePointer:
61+
case reflectlite.Chan, reflectlite.Ptr, reflectlite.UnsafePointer:
6262
return x.UnsafePointer() == y.UnsafePointer()
63-
case reflect.Array:
63+
case reflectlite.Array:
6464
for i := 0; i < x.Len(); i++ {
6565
if !reflectValueEqual(x.Index(i), y.Index(i)) {
6666
return false
6767
}
6868
}
6969
return true
70-
case reflect.Struct:
70+
case reflectlite.Struct:
7171
for i := 0; i < x.NumField(); i++ {
7272
if !reflectValueEqual(x.Field(i), y.Field(i)) {
7373
return false
7474
}
7575
}
7676
return true
77-
case reflect.Interface:
77+
case reflectlite.Interface:
7878
return reflectValueEqual(x.Elem(), y.Elem())
7979
default:
8080
runtimePanic("comparing un-comparable type")

0 commit comments

Comments
 (0)