From 31043628d8349d52e81d3fc4deaf556c299299ae Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 27 Mar 2023 18:55:46 +0200 Subject: [PATCH] reflect: use direct calls to runtime string functions The runtime.stringFromBytesTyped and runtime.stringToBytesTyped functions aren't really necessary, because they have the same LLVM IR signature. Therefore, remove them and link directly to the functions that the compiler uses internally. --- src/reflect/value.go | 4 ++-- src/runtime/string.go | 16 ---------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/reflect/value.go b/src/reflect/value.go index 487e0d82b..ed4d01cdf 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -1209,7 +1209,7 @@ func cvtFloat(v Value, t *rawType) Value { return makeFloat(v.flags, v.Float(), t) } -//go:linkname stringToBytes runtime.stringToBytesTyped +//go:linkname stringToBytes runtime.stringToBytes func stringToBytes(x string) []byte func cvtStringBytes(v Value, t *rawType) Value { @@ -1221,7 +1221,7 @@ func cvtStringBytes(v Value, t *rawType) Value { } } -//go:linkname stringFromBytes runtime.stringFromBytesTyped +//go:linkname stringFromBytes runtime.stringFromBytes func stringFromBytes(x []byte) string func cvtBytesString(v Value, t *rawType) Value { diff --git a/src/runtime/string.go b/src/runtime/string.go index b08b96f4a..13bfcd0ed 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -77,16 +77,6 @@ func stringFromBytes(x struct { return _string{ptr: (*byte)(buf), length: x.len} } -func stringFromBytesTyped(x []byte) string { - slice := *(*struct { - ptr *byte - len uintptr - cap uintptr - })(unsafe.Pointer(&x)) - s := stringFromBytes(slice) - return *(*string)(unsafe.Pointer(&s)) -} - // Convert a string to a []byte slice. func stringToBytes(x _string) (slice struct { ptr *byte @@ -101,12 +91,6 @@ func stringToBytes(x _string) (slice struct { return } -func stringToBytesTyped(x string) []byte { - s := *(*_string)(unsafe.Pointer(&x)) - slice := stringToBytes(s) - return *(*[]byte)(unsafe.Pointer(&slice)) -} - // Convert a []rune slice to a string. func stringFromRunes(runeSlice []rune) (s _string) { // Count the number of characters that will be in the string.